From 178013a9cd1cd3bb89900980fa327150e122f27e Mon Sep 17 00:00:00 2001 From: Alexdoru <57050655+Alexdoru@users.noreply.github.com> Date: Tue, 16 Dec 2025 20:39:17 +0100 Subject: [PATCH] [Memory-opti:fix leak] Fix world leak in Event class (#44) Co-authored-by: Nikolay S. --- .../makeo/gadomancy/client/ClientProxy.java | 6 ++ .../makeo/gadomancy/common/CommonProxy.java | 70 +++++++++++-------- .../makeo/gadomancy/common/Gadomancy.java | 13 +++- .../gadomancy/common/aura/AuraEffects.java | 7 +- .../common/events/EventHandlerEntity.java | 4 +- .../common/events/EventHandlerGolem.java | 4 +- .../common/events/EventHandlerWorld.java | 33 +++------ .../IntegrationThaumicHorizions.java | 4 +- .../gadomancy/common/utils/NBTHelper.java | 2 +- 9 files changed, 78 insertions(+), 65 deletions(-) diff --git a/src/main/java/makeo/gadomancy/client/ClientProxy.java b/src/main/java/makeo/gadomancy/client/ClientProxy.java index 9bcebd6..ab21c70 100644 --- a/src/main/java/makeo/gadomancy/client/ClientProxy.java +++ b/src/main/java/makeo/gadomancy/client/ClientProxy.java @@ -22,6 +22,7 @@ import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler; import cpw.mods.fml.client.registry.RenderingRegistry; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLLog; +import cpw.mods.fml.relauncher.Side; import makeo.gadomancy.client.effect.EffectHandler; import makeo.gadomancy.client.events.ClientHandler; import makeo.gadomancy.client.events.RenderEventHandler; @@ -310,4 +311,9 @@ public class ClientProxy extends CommonProxy { public void runDelayedClientSide(Runnable run) { this.clientActions.add(run); } + + @Override + public Side getSide() { + return Side.CLIENT; + } } diff --git a/src/main/java/makeo/gadomancy/common/CommonProxy.java b/src/main/java/makeo/gadomancy/common/CommonProxy.java index 46c9084..1c88bba 100644 --- a/src/main/java/makeo/gadomancy/common/CommonProxy.java +++ b/src/main/java/makeo/gadomancy/common/CommonProxy.java @@ -12,10 +12,11 @@ import net.minecraftforge.common.DimensionManager; import net.minecraftforge.common.MinecraftForge; import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.event.FMLServerAboutToStartEvent; +import cpw.mods.fml.common.event.FMLServerStoppedEvent; import cpw.mods.fml.common.network.IGuiHandler; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.relauncher.Side; -import makeo.gadomancy.client.ClientProxy; import makeo.gadomancy.common.aura.AuraEffects; import makeo.gadomancy.common.containers.ContainerArcanePackager; import makeo.gadomancy.common.containers.ContainerInfusionClaw; @@ -46,22 +47,18 @@ import thaumcraft.common.entities.golems.EntityGolemBase; /** * This class is part of the Gadomancy Mod Gadomancy is Open Source and distributed under the GNU LESSER GENERAL PUBLIC * LICENSE for more read the LICENSE file - * + *

* Created by makeo @ 29.11.2014 14:18 */ public class CommonProxy implements IGuiHandler { public static boolean serverOnlineState; - public static final EventHandlerGolem EVENT_HANDLER_GOLEM = new EventHandlerGolem(); - public void onConstruct() {} public void preInitalize() { PacketHandler.init(); - RegisteredItems.preInit(); - RegisteredBlocks.init(); RegisteredItems.init(); RegisteredGolemStuff.init(); @@ -69,21 +66,10 @@ public class CommonProxy implements IGuiHandler { public void initalize() { NetworkRegistry.INSTANCE.registerGuiHandler(Gadomancy.instance, this); - - MinecraftForge.EVENT_BUS.register(CommonProxy.EVENT_HANDLER_GOLEM); - FMLCommonHandler.instance().bus().register(new EventHandlerNetwork()); - EventHandlerWorld worldEventHandler = new EventHandlerWorld(); - MinecraftForge.EVENT_BUS.register(worldEventHandler); - FMLCommonHandler.instance().bus().register(worldEventHandler); - MinecraftForge.EVENT_BUS.register(new EventHandlerEntity()); - RegisteredEnchantments.init(); RegisteredRecipes.init(); - SyncDataHolder.initialize(); - RegisteredEntities.init(); - DimensionManager.registerProviderType(ModConfig.dimOuterId, WorldProviderTCEldrich.class, true); DimensionManager.registerDimension(ModConfig.dimOuterId, ModConfig.dimOuterId); } @@ -91,14 +77,10 @@ public class CommonProxy implements IGuiHandler { public void postInitalize() { RegisteredPotions.init(); AuraEffects.AER.getTickInterval(); // initalize AuraEffects - RegisteredResearches.init(); RegisteredIntegrations.init(); - RegisteredResearches.postInit(); - RegisteredItems.postInit(); - ModSubstitutions.postInit(); } @@ -125,15 +107,12 @@ public class CommonProxy implements IGuiHandler { @Override public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { - switch (ID) { - case 0: - return new ContainerGolem(player.inventory, ((EntityGolemBase) world.getEntityByID(x)).inventory); - case 1: - return new ContainerInfusionClaw(player.inventory, (IInventory) world.getTileEntity(x, y, z)); - case 2: - return new ContainerArcanePackager(player.inventory, (IInventory) world.getTileEntity(x, y, z)); - } - return null; + return switch (ID) { + case 0 -> new ContainerGolem(player.inventory, ((EntityGolemBase) world.getEntityByID(x)).inventory); + case 1 -> new ContainerInfusionClaw(player.inventory, (IInventory) world.getTileEntity(x, y, z)); + case 2 -> new ContainerArcanePackager(player.inventory, (IInventory) world.getTileEntity(x, y, z)); + default -> null; + }; } @Override @@ -144,6 +123,35 @@ public class CommonProxy implements IGuiHandler { public void runDelayedClientSide(Runnable run) {} public Side getSide() { - return this instanceof ClientProxy ? Side.CLIENT : Side.SERVER; + return Side.SERVER; + } + + public EventHandlerGolem EVENT_HANDLER_GOLEM; + public EventHandlerNetwork EVENT_HANDLER_NETWORK; + public EventHandlerWorld EVENT_HANDLER_WORLD; + public EventHandlerEntity EVENT_HANDLER_ENTITY; + + public void onServerAboutToStart(FMLServerAboutToStartEvent event) { + EVENT_HANDLER_GOLEM = new EventHandlerGolem(); + MinecraftForge.EVENT_BUS.register(EVENT_HANDLER_GOLEM); + EVENT_HANDLER_NETWORK = new EventHandlerNetwork(); + FMLCommonHandler.instance().bus().register(EVENT_HANDLER_NETWORK); + EVENT_HANDLER_WORLD = new EventHandlerWorld(); + MinecraftForge.EVENT_BUS.register(EVENT_HANDLER_WORLD); + FMLCommonHandler.instance().bus().register(EVENT_HANDLER_WORLD); + EVENT_HANDLER_ENTITY = new EventHandlerEntity(); + MinecraftForge.EVENT_BUS.register(EVENT_HANDLER_ENTITY); + } + + public void onServerStopped(FMLServerStoppedEvent event) { + MinecraftForge.EVENT_BUS.unregister(EVENT_HANDLER_GOLEM); + EVENT_HANDLER_GOLEM = null; + FMLCommonHandler.instance().bus().unregister(EVENT_HANDLER_NETWORK); + EVENT_HANDLER_NETWORK = null; + MinecraftForge.EVENT_BUS.unregister(EVENT_HANDLER_WORLD); + FMLCommonHandler.instance().bus().unregister(EVENT_HANDLER_WORLD); + EVENT_HANDLER_WORLD = null; + MinecraftForge.EVENT_BUS.unregister(EVENT_HANDLER_ENTITY); + EVENT_HANDLER_ENTITY = null; } } diff --git a/src/main/java/makeo/gadomancy/common/Gadomancy.java b/src/main/java/makeo/gadomancy/common/Gadomancy.java index b918d50..8780218 100644 --- a/src/main/java/makeo/gadomancy/common/Gadomancy.java +++ b/src/main/java/makeo/gadomancy/common/Gadomancy.java @@ -14,6 +14,8 @@ import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLInterModComms; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.event.FMLServerAboutToStartEvent; +import cpw.mods.fml.common.event.FMLServerStoppedEvent; import makeo.gadomancy.api.GadomancyApi; import makeo.gadomancy.common.api.DefaultApiHandler; import makeo.gadomancy.common.data.config.ModConfig; @@ -83,7 +85,6 @@ public class Gadomancy { @Mod.EventHandler public void init(FMLInitializationEvent event) { Gadomancy.proxy.initalize(); - FMLInterModComms.sendMessage(Thaumcraft.MODID, "dimensionBlacklist", ModConfig.dimOuterId + ":0"); } @@ -91,4 +92,14 @@ public class Gadomancy { public void postInit(FMLPostInitializationEvent event) { Gadomancy.proxy.postInitalize(); } + + @Mod.EventHandler + public void onServerAboutToStart(FMLServerAboutToStartEvent event) { + Gadomancy.proxy.onServerAboutToStart(event); + } + + @Mod.EventHandler + public void onServerStopped(FMLServerStoppedEvent event) { + Gadomancy.proxy.onServerStopped(event); + } } diff --git a/src/main/java/makeo/gadomancy/common/aura/AuraEffects.java b/src/main/java/makeo/gadomancy/common/aura/AuraEffects.java index eb19cb6..19550c0 100644 --- a/src/main/java/makeo/gadomancy/common/aura/AuraEffects.java +++ b/src/main/java/makeo/gadomancy/common/aura/AuraEffects.java @@ -33,7 +33,7 @@ import net.minecraft.world.EnumDifficulty; import net.minecraft.world.World; import makeo.gadomancy.api.AuraEffect; -import makeo.gadomancy.common.events.EventHandlerEntity; +import makeo.gadomancy.common.Gadomancy; import makeo.gadomancy.common.integration.IntegrationThaumicTinkerer; import makeo.gadomancy.common.registration.RegisteredIntegrations; import makeo.gadomancy.common.registration.RegisteredPotions; @@ -201,8 +201,9 @@ public class AuraEffects { @Override public void doEntityEffect(ChunkCoordinates originTile, Entity e) { - if (!EventHandlerEntity.registeredLuxPylons.contains(originTile)) - EventHandlerEntity.registeredLuxPylons.add(originTile); + if (!Gadomancy.proxy.EVENT_HANDLER_ENTITY.registeredLuxPylons.contains(originTile)) { + Gadomancy.proxy.EVENT_HANDLER_ENTITY.registeredLuxPylons.add(originTile); + } } @Override diff --git a/src/main/java/makeo/gadomancy/common/events/EventHandlerEntity.java b/src/main/java/makeo/gadomancy/common/events/EventHandlerEntity.java index 1e68f09..176fece 100644 --- a/src/main/java/makeo/gadomancy/common/events/EventHandlerEntity.java +++ b/src/main/java/makeo/gadomancy/common/events/EventHandlerEntity.java @@ -38,7 +38,7 @@ import thaumcraft.common.items.armor.Hover; */ public class EventHandlerEntity { - public static List registeredLuxPylons = new ArrayList(); + public final List registeredLuxPylons = new ArrayList<>(); /* * @SubscribeEvent(priority = EventPriority.LOWEST) public void on(EntityEvent.EntityConstructing e) { if (e.entity @@ -51,7 +51,7 @@ public class EventHandlerEntity { if (event.entityLiving.isCreatureType(EnumCreatureType.monster, false)) { double rangeSq = AuraEffects.LUX.getRange() * AuraEffects.LUX.getRange(); Vector3 entityPos = MiscUtils.getPositionVector(event.entity); - for (ChunkCoordinates luxPylons : EventHandlerEntity.registeredLuxPylons) { + for (ChunkCoordinates luxPylons : registeredLuxPylons) { Vector3 pylon = Vector3.fromCC(luxPylons); if (entityPos.distanceSquared(pylon) <= rangeSq) { event.setResult(Event.Result.DENY); diff --git a/src/main/java/makeo/gadomancy/common/events/EventHandlerGolem.java b/src/main/java/makeo/gadomancy/common/events/EventHandlerGolem.java index 9512a41..b547a5a 100644 --- a/src/main/java/makeo/gadomancy/common/events/EventHandlerGolem.java +++ b/src/main/java/makeo/gadomancy/common/events/EventHandlerGolem.java @@ -50,6 +50,8 @@ import thaumcraft.common.items.wands.ItemWandCasting; */ public class EventHandlerGolem { + private final Map markedGolems = new HashMap<>(); + @SubscribeEvent(priority = EventPriority.LOWEST) public void on(EntityEvent.EntityConstructing e) { if (e.entity instanceof EntityGolemBase) { @@ -147,8 +149,6 @@ public class EventHandlerGolem { } } - private final Map markedGolems = new HashMap(); - @SubscribeEvent(priority = EventPriority.HIGHEST, receiveCanceled = true) public void on(PlaySoundAtEntityEvent event) { if (!event.entity.worldObj.isRemote && event.entity instanceof EntityGolemBase diff --git a/src/main/java/makeo/gadomancy/common/events/EventHandlerWorld.java b/src/main/java/makeo/gadomancy/common/events/EventHandlerWorld.java index 01ed213..7e4fd74 100644 --- a/src/main/java/makeo/gadomancy/common/events/EventHandlerWorld.java +++ b/src/main/java/makeo/gadomancy/common/events/EventHandlerWorld.java @@ -4,7 +4,6 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import net.minecraft.entity.Entity; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; @@ -18,7 +17,6 @@ import net.minecraft.util.StatCollector; import net.minecraft.world.Explosion; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.event.entity.EntityJoinWorldEvent; -import net.minecraftforge.event.entity.living.LivingEvent; import net.minecraftforge.event.entity.player.ItemTooltipEvent; import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.event.world.BlockEvent; @@ -56,7 +54,9 @@ import thaumcraft.common.tiles.TileJarFillable; */ public class EventHandlerWorld { - public Map trackedItems = new HashMap(); + private final Map trackedItems = new HashMap<>(); + private Map interacts; + private int serverTick; @SubscribeEvent(priority = EventPriority.LOW) public void on(EntityJoinWorldEvent event) { @@ -146,16 +146,6 @@ public class EventHandlerWorld { return NBTHelper.hasPersistentData(stack) && NBTHelper.getPersistentData(stack).hasKey("disguise"); } - private int serverTick; - private Entity lastUpdated; - - @SubscribeEvent(priority = EventPriority.LOWEST) - public void on(LivingEvent.LivingUpdateEvent e) { - if (!e.entityLiving.worldObj.isRemote) { - this.lastUpdated = e.entityLiving; - } - } - @SubscribeEvent(priority = EventPriority.NORMAL) public void on(WorldEvent.Load e) { if (!e.world.isRemote && e.world.provider.dimensionId == 0) { @@ -200,12 +190,10 @@ public class EventHandlerWorld { SyncDataHolder.doNecessaryUpdates(); this.serverTick++; if ((this.serverTick & 15) == 0) { - EventHandlerEntity.registeredLuxPylons.clear(); + Gadomancy.proxy.EVENT_HANDLER_ENTITY.registeredLuxPylons.clear(); } } - private Map interacts; - @SubscribeEvent(priority = EventPriority.LOWEST, receiveCanceled = true) public void on(BlockEvent.PlaceEvent e) { if (e.isCanceled()) { @@ -237,7 +225,7 @@ public class EventHandlerWorld { if (!event.world.isRemote) { if (event.block == RegisteredBlocks.blockNodeManipulator) { TileEntity te = event.world.getTileEntity(event.x, event.y, event.z); - if (te != null && te instanceof TileNodeManipulator) { + if (te instanceof TileNodeManipulator) { if (((TileNodeManipulator) te).isInMultiblock()) ((TileNodeManipulator) te).breakMultiblock(); } } @@ -280,20 +268,19 @@ public class EventHandlerWorld { } private boolean isStickyJar(ItemStack stack) { - return stack != null && RegisteredItems.isStickyableJar(stack) - && stack.hasTagCompound() + return RegisteredItems.isStickyableJar(stack) && stack.hasTagCompound() && stack.stackTagCompound.getBoolean("isStickyJar"); } @SubscribeEvent(priority = EventPriority.NORMAL) public void on(ItemTooltipEvent e) { - if (e.toolTip.size() > 0 && e.itemStack.hasTagCompound()) { + if (!e.toolTip.isEmpty() && e.itemStack.hasTagCompound()) { if (e.itemStack.stackTagCompound.getBoolean("isStickyJar")) { - e.toolTip.add(1, "\u00a7a" + StatCollector.translateToLocal("gadomancy.lore.stickyjar")); + e.toolTip.add(1, EnumChatFormatting.GREEN + StatCollector.translateToLocal("gadomancy.lore.stickyjar")); } } - if (e.toolTip.size() > 0 && NBTHelper.hasPersistentData(e.itemStack)) { + if (!e.toolTip.isEmpty() && NBTHelper.hasPersistentData(e.itemStack)) { NBTTagCompound compound = NBTHelper.getPersistentData(e.itemStack); if (compound.hasKey("disguise")) { NBTBase base = compound.getTag("disguise"); @@ -306,7 +293,7 @@ public class EventHandlerWorld { } else { lore = StatCollector.translateToLocal("gadomancy.lore.disguise.none"); } - e.toolTip.add("\u00a7a" + lore); + e.toolTip.add(EnumChatFormatting.GREEN + lore); } } } diff --git a/src/main/java/makeo/gadomancy/common/integration/thaumichorizions/IntegrationThaumicHorizions.java b/src/main/java/makeo/gadomancy/common/integration/thaumichorizions/IntegrationThaumicHorizions.java index 8051181..a1b8b99 100644 --- a/src/main/java/makeo/gadomancy/common/integration/thaumichorizions/IntegrationThaumicHorizions.java +++ b/src/main/java/makeo/gadomancy/common/integration/thaumichorizions/IntegrationThaumicHorizions.java @@ -19,7 +19,6 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.relauncher.Side; import makeo.gadomancy.api.ClickBehavior; import makeo.gadomancy.client.ClientProxy; -import makeo.gadomancy.common.CommonProxy; import makeo.gadomancy.common.Gadomancy; import makeo.gadomancy.common.integration.IntegrationMod; import makeo.gadomancy.common.registration.RegisteredBlocks; @@ -87,7 +86,8 @@ public class IntegrationThaumicHorizions extends IntegrationMod { ItemStack stack = player.getCurrentEquippedItem(); if (stack != null && stack.getItem().onLeftClickEntity(stack, player, e.target) && e.target.isDead) { - CommonProxy.EVENT_HANDLER_GOLEM.on(new PlaySoundAtEntityEvent(e.target, "thaumcraft:zap", 0.5f, 1.0f)); + Gadomancy.proxy.EVENT_HANDLER_GOLEM + .on(new PlaySoundAtEntityEvent(e.target, "thaumcraft:zap", 0.5f, 1.0f)); } } } diff --git a/src/main/java/makeo/gadomancy/common/utils/NBTHelper.java b/src/main/java/makeo/gadomancy/common/utils/NBTHelper.java index 0345ce6..31770b4 100644 --- a/src/main/java/makeo/gadomancy/common/utils/NBTHelper.java +++ b/src/main/java/makeo/gadomancy/common/utils/NBTHelper.java @@ -50,7 +50,7 @@ public class NBTHelper { public static boolean hasPersistentData(NBTTagCompound base) { NBTBase modData = base.getTag(Gadomancy.MODID); - return modData != null && modData instanceof NBTTagCompound; + return modData instanceof NBTTagCompound; } public static void removePersistentData(Entity entity) {