diff options
18 files changed, 1431 insertions, 0 deletions
diff --git a/settings.gradle b/settings.gradle index ffa36cd..aa7e7f3 100644 --- a/settings.gradle +++ b/settings.gradle @@ -8,10 +8,18 @@ pluginManagement { name = 'Forge' url = 'https://maven.minecraftforge.net/' } + maven { + name = 'NeoForge' + url = 'https://maven.neoforged.net/releases' + } gradlePluginPortal() } } +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.5.0' +} + rootProject.name = 'spark' include ( 'spark-api', @@ -23,6 +31,7 @@ include ( 'spark-sponge7', 'spark-sponge8', 'spark-forge', + 'spark-neoforge', 'spark-fabric', 'spark-nukkit', 'spark-waterdog', diff --git a/spark-neoforge/build.gradle b/spark-neoforge/build.gradle new file mode 100644 index 0000000..177449f --- /dev/null +++ b/spark-neoforge/build.gradle @@ -0,0 +1,64 @@ +plugins { + id 'com.github.johnrengelman.shadow' version '8.1.1' + id 'net.neoforged.gradle.userdev' version '7.0.97' +} + +java.toolchain.languageVersion = JavaLanguageVersion.of(17) + +tasks.withType(JavaCompile) { + // override, compile targeting J17 + options.release = 17 +} + +minecraft { + accessTransformers { + file('src/main/resources/META-INF/accesstransformer.cfg') + } +} + +configurations { + shade + implementation.extendsFrom shade +} + +dependencies { + implementation "net.neoforged:neoforge:20.4.223" + shade project(':spark-common') +} + +processResources { + from(sourceSets.main.resources.srcDirs) { + include 'META-INF/mods.toml' + expand ( + 'pluginVersion': project.pluginVersion, + 'pluginDescription': project.pluginDescription + ) + } + + from(sourceSets.main.resources.srcDirs) { + exclude 'META-INF/mods.toml' + } +} + +shadowJar { + archiveFileName = "spark-${project.pluginVersion}-neoforge.jar" + configurations = [project.configurations.shade] + + relocate 'net.kyori.adventure', 'me.lucko.spark.lib.adventure' + relocate 'net.kyori.examination', 'me.lucko.spark.lib.adventure.examination' + relocate 'net.bytebuddy', 'me.lucko.spark.lib.bytebuddy' + relocate 'com.google.protobuf', 'me.lucko.spark.lib.protobuf' + relocate 'org.objectweb.asm', 'me.lucko.spark.lib.asm' + relocate 'one.profiler', 'me.lucko.spark.lib.asyncprofiler' + relocate 'me.lucko.bytesocks.client', 'me.lucko.spark.lib.bytesocks' + relocate 'org.java_websocket', 'me.lucko.spark.lib.bytesocks.ws' + + exclude 'module-info.class' + exclude 'META-INF/maven/**' + exclude 'META-INF/proguard/**' +} + +artifacts { + archives shadowJar + shadow shadowJar +} diff --git a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeClassSourceLookup.java b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeClassSourceLookup.java new file mode 100644 index 0000000..5e60ee7 --- /dev/null +++ b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeClassSourceLookup.java @@ -0,0 +1,36 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.neoforge; + +import cpw.mods.modlauncher.TransformingClassLoader; +import me.lucko.spark.common.sampler.source.ClassSourceLookup; + +public class NeoForgeClassSourceLookup implements ClassSourceLookup { + + @Override + public String identify(Class<?> clazz) { + if (clazz.getClassLoader() instanceof TransformingClassLoader) { + String name = clazz.getModule().getName(); + return name.equals("forge") || name.equals("minecraft") ? null : name; + } + return null; + } +} diff --git a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeCommandSender.java b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeCommandSender.java new file mode 100644 index 0000000..2f6a411 --- /dev/null +++ b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeCommandSender.java @@ -0,0 +1,77 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.neoforge; + +import me.lucko.spark.common.command.sender.AbstractCommandSender; +import me.lucko.spark.neoforge.plugin.NeoForgeSparkPlugin; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import net.minecraft.commands.CommandSource; +import net.minecraft.network.chat.Component.Serializer; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.rcon.RconConsoleSource; +import net.minecraft.world.entity.player.Player; + +import java.util.Objects; +import java.util.UUID; + +public class NeoForgeCommandSender extends AbstractCommandSender<CommandSource> { + private final NeoForgeSparkPlugin plugin; + + public NeoForgeCommandSender(CommandSource source, NeoForgeSparkPlugin plugin) { + super(source); + this.plugin = plugin; + } + + @Override + public String getName() { + if (super.delegate instanceof Player) { + return ((Player) super.delegate).getGameProfile().getName(); + } else if (super.delegate instanceof MinecraftServer) { + return "Console"; + } else if (super.delegate instanceof RconConsoleSource) { + return "RCON Console"; + } else { + return "unknown:" + super.delegate.getClass().getSimpleName(); + } + } + + @Override + public UUID getUniqueId() { + if (super.delegate instanceof Player) { + return ((Player) super.delegate).getUUID(); + } + return null; + } + + @Override + public void sendMessage(Component message) { + MutableComponent component = Serializer.fromJson(GsonComponentSerializer.gson().serializeToTree(message)); + Objects.requireNonNull(component, "component"); + super.delegate.sendSystemMessage(component); + } + + @Override + public boolean hasPermission(String permission) { + return this.plugin.hasPermission(super.delegate, permission); + } +} diff --git a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeExtraMetadataProvider.java b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeExtraMetadataProvider.java new file mode 100644 index 0000000..e18b3d3 --- /dev/null +++ b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeExtraMetadataProvider.java @@ -0,0 +1,73 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.neoforge; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import me.lucko.spark.common.platform.MetadataProvider; +import net.minecraft.server.packs.repository.Pack; +import net.minecraft.server.packs.repository.PackRepository; +import net.minecraft.server.packs.repository.PackSource; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class NeoForgeExtraMetadataProvider implements MetadataProvider { + + private final PackRepository resourcePackManager; + + public NeoForgeExtraMetadataProvider(PackRepository resourcePackManager) { + this.resourcePackManager = resourcePackManager; + } + + @Override + public Map<String, JsonElement> get() { + Map<String, JsonElement> metadata = new LinkedHashMap<>(); + metadata.put("datapacks", datapackMetadata()); + return metadata; + } + + private JsonElement datapackMetadata() { + JsonObject datapacks = new JsonObject(); + for (Pack profile : this.resourcePackManager.getSelectedPacks()) { + JsonObject obj = new JsonObject(); + obj.addProperty("name", profile.getTitle().getString()); + obj.addProperty("description", profile.getDescription().getString()); + obj.addProperty("source", resourcePackSource(profile.getPackSource())); + datapacks.add(profile.getId(), obj); + } + return datapacks; + } + + private static String resourcePackSource(PackSource source) { + if (source == PackSource.DEFAULT) { + return "none"; + } else if (source == PackSource.BUILT_IN) { + return "builtin"; + } else if (source == PackSource.WORLD) { + return "world"; + } else if (source == PackSource.SERVER) { + return "server"; + } else { + return "unknown"; + } + } +} diff --git a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgePlatformInfo.java b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgePlatformInfo.java new file mode 100644 index 0000000..ea0141e --- /dev/null +++ b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgePlatformInfo.java @@ -0,0 +1,53 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.neoforge; + +import me.lucko.spark.common.platform.PlatformInfo; +import net.neoforged.neoforge.internal.versions.neoforge.NeoForgeVersion; +import net.neoforged.neoforge.internal.versions.neoform.NeoFormVersion; + +public class NeoForgePlatformInfo implements PlatformInfo { + private final Type type; + + public NeoForgePlatformInfo(Type type) { + this.type = type; + } + + @Override + public Type getType() { + return this.type; + } + + @Override + public String getName() { + return "NeoForge"; + } + + @Override + public String getVersion() { + return NeoForgeVersion.getVersion(); + } + + @Override + public String getMinecraftVersion() { + return NeoFormVersion.getMCVersion(); + } +} diff --git a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgePlayerPingProvider.java b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgePlayerPingProvider.java new file mode 100644 index 0000000..191b60e --- /dev/null +++ b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgePlayerPingProvider.java @@ -0,0 +1,45 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.neoforge; + +import com.google.common.collect.ImmutableMap; +import me.lucko.spark.common.monitor.ping.PlayerPingProvider; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerPlayer; + +import java.util.Map; + +public class NeoForgePlayerPingProvider implements PlayerPingProvider { + private final MinecraftServer server; + + public NeoForgePlayerPingProvider(MinecraftServer server) { + this.server = server; + } + + @Override + public Map<String, Integer> poll() { + ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder(); + for (ServerPlayer player : this.server.getPlayerList().getPlayers()) { + builder.put(player.getGameProfile().getName(), player.connection.latency()); + } + return builder.build(); + } +} diff --git a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeServerConfigProvider.java b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeServerConfigProvider.java new file mode 100644 index 0000000..813683d --- /dev/null +++ b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeServerConfigProvider.java @@ -0,0 +1,56 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.neoforge; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import me.lucko.spark.common.platform.serverconfig.ConfigParser; +import me.lucko.spark.common.platform.serverconfig.PropertiesConfigParser; +import me.lucko.spark.common.platform.serverconfig.ServerConfigProvider; + +import java.util.Collection; +import java.util.Map; + +public class NeoForgeServerConfigProvider extends ServerConfigProvider { + + /** A map of provided files and their type */ + private static final Map<String, ConfigParser> FILES; + /** A collection of paths to be excluded from the files */ + private static final Collection<String> HIDDEN_PATHS; + + public NeoForgeServerConfigProvider() { + super(FILES, HIDDEN_PATHS); + } + + static { + ImmutableSet.Builder<String> hiddenPaths = ImmutableSet.<String>builder() + .add("server-ip") + .add("motd") + .add("resource-pack") + .add("rcon<dot>password") + .add("level-seed") + .addAll(getSystemPropertyList("spark.serverconfigs.hiddenpaths")); + + FILES = ImmutableMap.of("server.properties", PropertiesConfigParser.INSTANCE); + HIDDEN_PATHS = hiddenPaths.build(); + } + +} diff --git a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeSparkMod.java b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeSparkMod.java new file mode 100644 index 0000000..16c19ec --- /dev/null +++ b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeSparkMod.java @@ -0,0 +1,80 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.neoforge; + +import me.lucko.spark.neoforge.plugin.NeoForgeClientSparkPlugin; +import me.lucko.spark.neoforge.plugin.NeoForgeServerSparkPlugin; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.IExtensionPoint; +import net.neoforged.fml.ModContainer; +import net.neoforged.fml.ModLoadingContext; +import net.neoforged.fml.common.Mod; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; +import net.neoforged.fml.loading.FMLPaths; +import net.neoforged.neoforge.common.NeoForge; +import net.neoforged.neoforge.event.server.ServerAboutToStartEvent; + +import java.nio.file.Path; + +@Mod("spark") +public class NeoForgeSparkMod { + + private ModContainer container; + private Path configDirectory; + + private IEventBus eventBus; + + public NeoForgeSparkMod(IEventBus eventBus) { + this.eventBus = eventBus; + eventBus.addListener(this::setup); + eventBus.addListener(this::clientInit); + NeoForge.EVENT_BUS.register(this); + + ModLoadingContext.get().registerExtensionPoint(IExtensionPoint.DisplayTest.class, () -> new IExtensionPoint.DisplayTest(() -> IExtensionPoint.DisplayTest.IGNORESERVERONLY, (a, b) -> true)); + } + + public String getVersion() { + return this.container.getModInfo().getVersion().toString(); + } + + public void setup(FMLCommonSetupEvent e) { + this.container = ModLoadingContext.get().getActiveContainer(); + this.configDirectory = FMLPaths.CONFIGDIR.get().resolve(this.container.getModId()); + } + + public void clientInit(FMLClientSetupEvent e) { + NeoForgeClientSparkPlugin.register(this, e); + } + + @SubscribeEvent + public void serverInit(ServerAboutToStartEvent e) { + NeoForgeServerSparkPlugin.register(this, e); + } + + public Path getConfigDirectory() { + if (this.configDirectory == null) { + throw new IllegalStateException("Config directory not set"); + } + return this.configDirectory; + } +} diff --git a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeTickHook.java b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeTickHook.java new file mode 100644 index 0000000..84e1aff --- /dev/null +++ b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeTickHook.java @@ -0,0 +1,73 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.neoforge; + +import me.lucko.spark.common.tick.AbstractTickHook; +import me.lucko.spark.common.tick.TickHook; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.neoforge.common.NeoForge; +import net.neoforged.neoforge.event.TickEvent; + +public class NeoForgeTickHook extends AbstractTickHook implements TickHook { + private final TickEvent.Type type; + + public NeoForgeTickHook(TickEvent.Type type) { + this.type = type; + } + + @SubscribeEvent + public void onTick(TickEvent.ServerTickEvent e) { + if (e.phase != TickEvent.Phase.START) { + return; + } + + if (e.type != this.type) { + return; + } + + onTick(); + } + + @SubscribeEvent + public void onTick(TickEvent.ClientTickEvent e) { + if (e.phase != TickEvent.Phase.START) { + return; + } + + if (e.type != this.type) { + return; + } + + onTick(); + } + + + @Override + public void start() { + NeoForge.EVENT_BUS.register(this); + } + + @Override + public void close() { + NeoForge.EVENT_BUS.unregister(this); + } + +} diff --git a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeTickReporter.java b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeTickReporter.java new file mode 100644 index 0000000..be61d3e --- /dev/null +++ b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeTickReporter.java @@ -0,0 +1,72 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.neoforge; + +import me.lucko.spark.common.tick.SimpleTickReporter; +import me.lucko.spark.common.tick.TickReporter; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.neoforge.common.NeoForge; +import net.neoforged.neoforge.event.TickEvent; + +public class NeoForgeTickReporter extends SimpleTickReporter implements TickReporter { + private final TickEvent.Type type; + + public NeoForgeTickReporter(TickEvent.Type type) { + this.type = type; + } + + @SubscribeEvent + public void onTick(TickEvent.ServerTickEvent e) { + if (e.type != this.type) { + return; + } + + switch (e.phase) { + case START -> onStart(); + case END -> onEnd(); + default -> throw new AssertionError(e.phase); + } + } + @SubscribeEvent + public void onTick(TickEvent.ClientTickEvent e) { + if (e.type != this.type) { + return; + } + + switch (e.phase) { + case START -> onStart(); + case END -> onEnd(); + default -> throw new AssertionError(e.phase); + } + } + + @Override + public void start() { + NeoForge.EVENT_BUS.register(this); + } + + @Override + public void close() { + NeoForge.EVENT_BUS.unregister(this); + super.close(); + } + +} diff --git a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeWorldInfoProvider.java b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeWorldInfoProvider.java new file mode 100644 index 0000000..ef76646 --- /dev/null +++ b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeWorldInfoProvider.java @@ -0,0 +1,172 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.neoforge; + +import it.unimi.dsi.fastutil.longs.LongIterator; +import it.unimi.dsi.fastutil.longs.LongSet; +import me.lucko.spark.common.platform.world.AbstractChunkInfo; +import me.lucko.spark.common.platform.world.CountMap; +import me.lucko.spark.common.platform.world.WorldInfoProvider; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientLevel; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.entity.EntityLookup; +import net.minecraft.world.level.entity.EntitySection; +import net.minecraft.world.level.entity.EntitySectionStorage; +import net.minecraft.world.level.entity.PersistentEntitySectionManager; +import net.minecraft.world.level.entity.TransientEntitySectionManager; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Stream; + +public abstract class NeoForgeWorldInfoProvider implements WorldInfoProvider { + + protected List<ForgeChunkInfo> getChunksFromCache(EntitySectionStorage<Entity> cache) { + LongSet loadedChunks = cache.getAllChunksWithExistingSections(); + List<ForgeChunkInfo> list = new ArrayList<>(loadedChunks.size()); + + for (LongIterator iterator = loadedChunks.iterator(); iterator.hasNext(); ) { + long chunkPos = iterator.nextLong(); + Stream<EntitySection<Entity>> sections = cache.getExistingSectionsInChunk(chunkPos); + + list.add(new ForgeChunkInfo(chunkPos, sections)); + } + + return list; + } + + public static final class Server extends NeoForgeWorldInfoProvider { + private final MinecraftServer server; + + public Server(MinecraftServer server) { + this.server = server; + } + + @Override + public CountsResult pollCounts() { + int players = this.server.getPlayerCount(); + int entities = 0; + int chunks = 0; + + for (ServerLevel level : this.server.getAllLevels()) { + PersistentEntitySectionManager<Entity> entityManager = level.entityManager; + EntityLookup<Entity> entityIndex = entityManager.visibleEntityStorage; + + entities += entityIndex.count(); + chunks += level.getChunkSource().getLoadedChunksCount(); + } + + return new CountsResult(players, entities, -1, chunks); + } + + @Override + public ChunksResult<ForgeChunkInfo> pollChunks() { + ChunksResult<ForgeChunkInfo> data = new ChunksResult<>(); + + for (ServerLevel level : this.server.getAllLevels()) { + PersistentEntitySectionManager<Entity> entityManager = level.entityManager; + EntitySectionStorage<Entity> cache = entityManager.sectionStorage; + + List<ForgeChunkInfo> list = getChunksFromCache(cache); + data.put(level.dimension().location().getPath(), list); + } + + return data; + } + } + + public static final class Client extends NeoForgeWorldInfoProvider { + private final Minecraft client; + + public Client(Minecraft client) { + this.client = client; + } + + @Override + public CountsResult pollCounts() { + ClientLevel level = this.client.level; + if (level == null) { + return null; + } + + TransientEntitySectionManager<Entity> entityManager = level.entityStorage; + EntityLookup<Entity> entityIndex = entityManager.entityStorage; + + int entities = entityIndex.count(); + int chunks = level.getChunkSource().getLoadedChunksCount(); + + return new CountsResult(-1, entities, -1, chunks); + } + + @Override + public ChunksResult<ForgeChunkInfo> pollChunks() { + ChunksResult<ForgeChunkInfo> data = new ChunksResult<>(); + + ClientLevel level = this.client.level; + if (level == null) { + return null; + } + + TransientEntitySectionManager<Entity> entityManager = level.entityStorage; + EntitySectionStorage<Entity> cache = entityManager.sectionStorage; + + List<ForgeChunkInfo> list = getChunksFromCache(cache); + data.put(level.dimension().location().getPath(), list); + + return data; + } + } + + public static final class ForgeChunkInfo extends AbstractChunkInfo<EntityType<?>> { + private final CountMap<EntityType<?>> entityCounts; + + ForgeChunkInfo(long chunkPos, Stream<EntitySection<Entity>> entities) { + super(ChunkPos.getX(chunkPos), ChunkPos.getZ(chunkPos)); + + this.entityCounts = new CountMap.Simple<>(new HashMap<>()); + entities.forEach(section -> { + |
