From 6839d512616db75fe86f8fdc7210398738edc823 Mon Sep 17 00:00:00 2001 From: KevDaDev <65958288+KevinDaGame@users.noreply.github.com> Date: Sat, 5 Aug 2023 13:47:08 +0200 Subject: [PATCH 1/4] Clear worlds and player cache when logical server stops --- .../voxelsniperforge/VoxelSniperForge.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java b/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java index 236b32e5..58f327a8 100644 --- a/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java +++ b/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java @@ -35,6 +35,7 @@ import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.RegisterCommandsEvent; import net.minecraftforge.event.server.ServerStartingEvent; +import net.minecraftforge.event.server.ServerStoppingEvent; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; @@ -67,6 +68,7 @@ public class VoxelSniperForge implements IVoxelsniper { private final Map players = new HashMap<>(); private final Map worlds = new HashMap<>(); + public static VoxelSniperForge getInstance() { return instance; } @@ -86,6 +88,7 @@ public VoxelSniperForge() { // Register ourselves for server and other game events we are interested in MinecraftForge.EVENT_BUS.register(this); + MinecraftForge.EVENT_BUS.register(new ForgeVoxelSniperListener(this)); } private void commonSetup(final FMLCommonSetupEvent event) { @@ -107,19 +110,20 @@ public final void registerPermissionNodes(final PermissionGatherEvent.Nodes even public void onServerStarting(ServerStartingEvent event) { this.fileHandler = new ForgeFileHandler(this); SchematicReader.initialize(); - MinecraftForge.EVENT_BUS.register(new ForgeVoxelSniperListener(this)); Messages.load(this); voxelSniperConfiguration = new VoxelSniperConfiguration(this); -// Bukkit.getPluginManager().registerEvents(this.voxelSniperListener, this); -// Bukkit.getPluginManager().registerEvents(this, this); -// getLogger().info("Registered Sniper Listener."); var level = ServerLifecycleHooks.getCurrentServer().getAllLevels().iterator().next(); this.biomeRegistry = level.registryAccess().registryOrThrow(Registries.BIOME); this.featureRegistry = level.registryAccess().registryOrThrow(Registries.CONFIGURED_FEATURE); VoxelCommandManager.getInstance().registerBrushSubcommands(); + } + @SubscribeEvent + public void onServerStopping(ServerStoppingEvent event) { + worlds.clear(); + players.clear(); } // You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent From a9808d4bca94ba83b172122874704c0ea996742a Mon Sep 17 00:00:00 2001 From: KevDaDev <65958288+KevinDaGame@users.noreply.github.com> Date: Sat, 5 Aug 2023 14:03:22 +0200 Subject: [PATCH 2/4] Implement unloadworld listener to prevent a memory leak with many worlds --- .../kevindagame/voxelsniperforge/VoxelSniperForge.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java b/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java index 58f327a8..803c41e5 100644 --- a/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java +++ b/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java @@ -34,6 +34,7 @@ import net.minecraft.world.level.levelgen.feature.configurations.TreeConfiguration; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.RegisterCommandsEvent; +import net.minecraftforge.event.level.LevelEvent; import net.minecraftforge.event.server.ServerStartingEvent; import net.minecraftforge.event.server.ServerStoppingEvent; import net.minecraftforge.eventbus.api.IEventBus; @@ -126,6 +127,13 @@ public void onServerStopping(ServerStoppingEvent event) { players.clear(); } + @SubscribeEvent + public void onLevelUnload(LevelEvent.Unload event) { + if (event.getLevel() instanceof ServerLevel) { + worlds.remove(event.getLevel().toString()); + } + } + // You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent @Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.MOD) public static class ClientModEvents { From 6ded074754ebf52fec3e80059aecdb66c5295442 Mon Sep 17 00:00:00 2001 From: KevDaDev <65958288+KevinDaGame@users.noreply.github.com> Date: Sun, 6 Aug 2023 13:21:00 +0200 Subject: [PATCH 3/4] Implement playerleave listener --- .../kevindagame/voxelsniperforge/VoxelSniperForge.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java b/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java index 803c41e5..c8d2b9b7 100644 --- a/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java +++ b/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java @@ -34,6 +34,7 @@ import net.minecraft.world.level.levelgen.feature.configurations.TreeConfiguration; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.RegisterCommandsEvent; +import net.minecraftforge.event.entity.player.PlayerEvent; import net.minecraftforge.event.level.LevelEvent; import net.minecraftforge.event.server.ServerStartingEvent; import net.minecraftforge.event.server.ServerStoppingEvent; @@ -134,6 +135,11 @@ public void onLevelUnload(LevelEvent.Unload event) { } } + @SubscribeEvent + public void onPlayerLeave(PlayerEvent.PlayerLoggedOutEvent event) { + players.remove(event.getEntity().getUUID()); + } + // You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent @Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.MOD) public static class ClientModEvents { From a742190e084e9ec5755f5fff2a814290034c9f9e Mon Sep 17 00:00:00 2001 From: KevDaDev <65958288+KevinDaGame@users.noreply.github.com> Date: Sun, 6 Aug 2023 13:21:07 +0200 Subject: [PATCH 4/4] Resolve nullable getplayer errors --- .../github/kevindagame/voxelsniperforge/VoxelSniperForge.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java b/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java index c8d2b9b7..6b8bbb3e 100644 --- a/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java +++ b/VoxelSniperForge/src/main/java/com/github/kevindagame/voxelsniperforge/VoxelSniperForge.java @@ -162,7 +162,9 @@ public IPlayer getPlayer(String name) { return getPlayer(ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayerByName(name)); } - public IPlayer getPlayer(@NotNull ServerPlayer p) { + @Nullable + public IPlayer getPlayer(@Nullable ServerPlayer p) { + if (p == null) return null; if (this.players.get(p.getUUID()) != null) return this.players.get(p.getUUID()); ForgePlayer res = new ForgePlayer(p); this.players.put(res.getUniqueId(), res);