From 1972b9ab2bc8ae8ad591cf3dbeb8dd82ea26dab0 Mon Sep 17 00:00:00 2001 From: Alexdoru <57050655+Alexdoru@users.noreply.github.com> Date: Fri, 17 Apr 2026 19:40:51 +0200 Subject: [PATCH] Fix memory leak (#91) --- .../client/core/proxy/TTClientProxy.java | 28 ++- .../tinkerer/common/ThaumicTinkerer.java | 49 ++--- .../common/core/proxy/TTCommonProxy.java | 90 ++++---- .../tinkerer/common/potion/ModPotions.java | 4 - .../common/potion/PotionEffectHandler.java | 193 ------------------ .../potion/PotionEffectHandlerClient.java | 122 +++++++++++ .../potion/PotionEffectHandlerServer.java | 114 +++++++++++ 7 files changed, 337 insertions(+), 263 deletions(-) delete mode 100644 src/main/java/thaumic/tinkerer/common/potion/PotionEffectHandler.java create mode 100644 src/main/java/thaumic/tinkerer/common/potion/PotionEffectHandlerClient.java create mode 100644 src/main/java/thaumic/tinkerer/common/potion/PotionEffectHandlerServer.java diff --git a/src/main/java/thaumic/tinkerer/client/core/proxy/TTClientProxy.java b/src/main/java/thaumic/tinkerer/client/core/proxy/TTClientProxy.java index 9fc54a85..049e206d 100644 --- a/src/main/java/thaumic/tinkerer/client/core/proxy/TTClientProxy.java +++ b/src/main/java/thaumic/tinkerer/client/core/proxy/TTClientProxy.java @@ -26,6 +26,8 @@ import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.network.FMLNetworkEvent; import cpw.mods.fml.relauncher.Side; import thaumcraft.client.fx.ParticleEngine; import thaumic.tinkerer.client.core.handler.ClientTickHandler; @@ -64,8 +66,11 @@ import thaumic.tinkerer.common.item.ItemInfusedSeeds; import thaumic.tinkerer.common.item.ItemMobDisplay; import thaumic.tinkerer.common.item.kami.ItemPlacementMirror; import thaumic.tinkerer.common.item.kami.foci.ItemFocusShadowbeam; +import thaumic.tinkerer.common.potion.PotionEffectHandlerClient; -public class TTClientProxy extends TTCommonProxy { +public final class TTClientProxy extends TTCommonProxy { + + private PotionEffectHandlerClient potionHandlerClient; public static EntityPlayer getPlayer() { return Minecraft.getMinecraft().thePlayer; @@ -74,18 +79,21 @@ public class TTClientProxy extends TTCommonProxy { @Override public void preInit(FMLPreInitializationEvent event) { super.preInit(event); + FMLCommonHandler.instance().bus().register(this); if (Loader.isModLoaded("ComputerCraft")) { MinecraftForge.EVENT_BUS.register(new FumeTool()); } - if (ConfigHandler.enableKami) + if (ConfigHandler.enableKami) { // kamiRarity = EnumHelperClient.addRarity("KAMI", 0x6, "Kami"); + kamiRarity = EnumHelperClient.addEnum( new Class[][] { { EnumRarity.class, EnumChatFormatting.class, String.class } }, EnumRarity.class, "KAMI", EnumChatFormatting.LIGHT_PURPLE, "Kami"); + } } @Override @@ -108,6 +116,22 @@ public class TTClientProxy extends TTCommonProxy { } } + @SubscribeEvent + public void onClientConnect(FMLNetworkEvent.ClientConnectedToServerEvent event) { + this.potionHandlerClient = new PotionEffectHandlerClient(); + MinecraftForge.EVENT_BUS.register(potionHandlerClient); + FMLCommonHandler.instance().bus().register(potionHandlerClient); + } + + @SubscribeEvent + public void onClientDisconnect(FMLNetworkEvent.ClientDisconnectionFromServerEvent event) { + if (this.potionHandlerClient != null) { + MinecraftForge.EVENT_BUS.unregister(potionHandlerClient); + FMLCommonHandler.instance().bus().unregister(potionHandlerClient); + this.potionHandlerClient = null; + } + } + private void registerTiles() { ClientRegistry.bindTileEntitySpecialRenderer(TileAnimationTablet.class, new RenderTileAnimationTablet()); ClientRegistry.bindTileEntitySpecialRenderer(TileMagnet.class, new RenderTileMagnet()); diff --git a/src/main/java/thaumic/tinkerer/common/ThaumicTinkerer.java b/src/main/java/thaumic/tinkerer/common/ThaumicTinkerer.java index 1425b513..03026f60 100644 --- a/src/main/java/thaumic/tinkerer/common/ThaumicTinkerer.java +++ b/src/main/java/thaumic/tinkerer/common/ThaumicTinkerer.java @@ -13,9 +13,6 @@ package thaumic.tinkerer.common; import java.util.Arrays; -import net.minecraft.command.ICommandManager; -import net.minecraft.command.ServerCommandManager; -import net.minecraft.server.MinecraftServer; import net.minecraftforge.common.DimensionManager; import org.apache.logging.log4j.Logger; @@ -31,14 +28,12 @@ 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.FMLServerStartingEvent; +import cpw.mods.fml.common.event.FMLServerStoppedEvent; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; import thaumcraft.common.CommonProxy; import thaumcraft.common.Thaumcraft; import thaumic.tinkerer.api.InterModCommsOperations; -import thaumic.tinkerer.common.core.commands.KamiUnlockedCommand; -import thaumic.tinkerer.common.core.commands.MaxResearchCommand; -import thaumic.tinkerer.common.core.commands.SetTendencyCommand; import thaumic.tinkerer.common.core.handler.ConfigHandler; import thaumic.tinkerer.common.core.proxy.TTCommonProxy; import thaumic.tinkerer.common.dim.WorldProviderBedrock; @@ -85,13 +80,27 @@ public class ThaumicTinkerer { } @EventHandler - public void serverStart(FMLServerStartingEvent event) { - MinecraftServer server = MinecraftServer.getServer(); - ICommandManager command = server.getCommandManager(); - ServerCommandManager manager = (ServerCommandManager) command; - manager.registerCommand(new SetTendencyCommand()); - manager.registerCommand(new MaxResearchCommand()); - manager.registerCommand(new KamiUnlockedCommand()); + public void init(FMLInitializationEvent event) { + proxy.init(event); + if (ConfigHandler.enableKami && ConfigHandler.bedrockDimensionID != 0) { + DimensionManager.registerProviderType(ConfigHandler.bedrockDimensionID, WorldProviderBedrock.class, false); + DimensionManager.registerDimension(ConfigHandler.bedrockDimensionID, ConfigHandler.bedrockDimensionID); + } + } + + @EventHandler + public void postInit(FMLPostInitializationEvent event) { + proxy.postInit(event); + } + + @EventHandler + public void serverStarting(FMLServerStartingEvent event) { + proxy.serverStarting(event); + } + + @EventHandler + public void serverStopped(FMLServerStoppedEvent event) { + proxy.serverStopped(event); } @EventHandler @@ -111,18 +120,4 @@ public class ThaumicTinkerer { public void blackListCCDevices(String classname) { PeripheralHandler.Blacklist.add(classname); } - - @EventHandler - public void init(FMLInitializationEvent event) { - proxy.init(event); - if (ConfigHandler.enableKami && ConfigHandler.bedrockDimensionID != 0) { - DimensionManager.registerProviderType(ConfigHandler.bedrockDimensionID, WorldProviderBedrock.class, false); - DimensionManager.registerDimension(ConfigHandler.bedrockDimensionID, ConfigHandler.bedrockDimensionID); - } - } - - @EventHandler - public void postInit(FMLPostInitializationEvent event) { - proxy.postInit(event); - } } diff --git a/src/main/java/thaumic/tinkerer/common/core/proxy/TTCommonProxy.java b/src/main/java/thaumic/tinkerer/common/core/proxy/TTCommonProxy.java index c973cb00..5c4cd31a 100644 --- a/src/main/java/thaumic/tinkerer/common/core/proxy/TTCommonProxy.java +++ b/src/main/java/thaumic/tinkerer/common/core/proxy/TTCommonProxy.java @@ -1,20 +1,23 @@ /** * This class was created by . It's distributed as part of the ThaumicTinkerer Mod. - * + *

* ThaumicTinkerer is Open Source and distributed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 * License (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB) - * + *

* ThaumicTinkerer is a Derivative Work on Thaumcraft 4. Thaumcraft 4 (c) Azanor 2012 * (http://www.minecraftforum.net/topic/1585216-) - * + *

* File Created @ [4 Sep 2013, 16:29:41 (GMT)] */ package thaumic.tinkerer.common.core.proxy; +import net.minecraft.command.ICommandManager; +import net.minecraft.command.ServerCommandManager; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.EnumRarity; import net.minecraft.item.Item; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.server.MinecraftServer; import net.minecraft.util.EnumChatFormatting; import net.minecraft.world.World; import net.minecraftforge.common.MinecraftForge; @@ -27,6 +30,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.FMLServerStartingEvent; +import cpw.mods.fml.common.event.FMLServerStoppedEvent; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.relauncher.Side; import dan200.computercraft.api.ComputerCraftAPI; @@ -54,6 +59,9 @@ import thaumic.tinkerer.common.block.tile.transvector.TileTransvectorInterface; import thaumic.tinkerer.common.compat.EMTCompat; import thaumic.tinkerer.common.compat.EnderIO; import thaumic.tinkerer.common.compat.FumeTool; +import thaumic.tinkerer.common.core.commands.KamiUnlockedCommand; +import thaumic.tinkerer.common.core.commands.MaxResearchCommand; +import thaumic.tinkerer.common.core.commands.SetTendencyCommand; import thaumic.tinkerer.common.core.handler.ConfigHandler; import thaumic.tinkerer.common.core.handler.ModCreativeTab; import thaumic.tinkerer.common.core.handler.kami.DimensionalShardDropHandler; @@ -89,6 +97,7 @@ import thaumic.tinkerer.common.peripheral.OpenComputers.DriverEssentiaTransport; import thaumic.tinkerer.common.peripheral.OpenComputers.DriverIAspectContainer; import thaumic.tinkerer.common.peripheral.PeripheralHandler; import thaumic.tinkerer.common.potion.ModPotions; +import thaumic.tinkerer.common.potion.PotionEffectHandlerServer; import thaumic.tinkerer.common.research.ResearchHelper; public class TTCommonProxy { @@ -97,6 +106,7 @@ public class TTCommonProxy { public WandCap capIchor; public WandRod rodIchor; public Item.ToolMaterial toolMaterialIchor; + private PotionEffectHandlerServer potionHandlerServer; public void preInit(FMLPreInitializationEvent event) { toolMaterialIchor = EnumHelper.addToolMaterial("ICHOR", 4, -1, 10F, 5F, 25); @@ -163,35 +173,48 @@ public class TTCommonProxy { EMTCompat.init(); } - protected void registerPackets() { - ThaumicTinkerer.netHandler - .registerMessage(PacketSoulHearts.class, PacketSoulHearts.class, 142 + 0, Side.CLIENT); - ThaumicTinkerer.netHandler - .registerMessage(PacketToggleArmor.class, PacketToggleArmor.class, 142 + 1, Side.CLIENT); - ThaumicTinkerer.netHandler - .registerMessage(PacketToggleArmor.class, PacketToggleArmor.class, 142 + 2, Side.SERVER); - ThaumicTinkerer.netHandler - .registerMessage(PacketWarpGateButton.class, PacketWarpGateButton.class, 142 + 3, Side.SERVER); - ThaumicTinkerer.netHandler - .registerMessage(PacketWarpGateTeleport.class, PacketWarpGateTeleport.class, 142 + 4, Side.SERVER); - ThaumicTinkerer.netHandler.registerMessage( - PacketEnchanterAddEnchant.class, - PacketEnchanterAddEnchant.class, - 142 + 5, - Side.SERVER); - ThaumicTinkerer.netHandler.registerMessage( - PacketEnchanterStartWorking.class, - PacketEnchanterStartWorking.class, - 142 + 6, - Side.SERVER); - ThaumicTinkerer.netHandler - .registerMessage(PacketMobMagnetButton.class, PacketMobMagnetButton.class, 142 + 7, Side.SERVER); - ThaumicTinkerer.netHandler - .registerMessage(PacketTabletButton.class, PacketTabletButton.class, 142 + 8, Side.SERVER); - ThaumicTinkerer.netHandler - .registerMessage(PacketPlacerButton.class, PacketPlacerButton.class, 142 + 9, Side.SERVER); + public void postInit(FMLPostInitializationEvent event) { + ResearchHelper.initResearch(); + ThaumicTinkerer.registry.postInit(); + AspectCropLootManager.populateLootMap(); + ItemFocusDeflect.setupBlackList(); } + public void serverStarting(FMLServerStartingEvent event) { + MinecraftServer server = MinecraftServer.getServer(); + ICommandManager command = server.getCommandManager(); + ServerCommandManager manager = (ServerCommandManager) command; + manager.registerCommand(new SetTendencyCommand()); + manager.registerCommand(new MaxResearchCommand()); + manager.registerCommand(new KamiUnlockedCommand()); + this.potionHandlerServer = new PotionEffectHandlerServer(); + MinecraftForge.EVENT_BUS.register(potionHandlerServer); + FMLCommonHandler.instance().bus().register(potionHandlerServer); + } + + public void serverStopped(FMLServerStoppedEvent event) { + if (this.potionHandlerServer != null) { + MinecraftForge.EVENT_BUS.unregister(potionHandlerServer); + FMLCommonHandler.instance().bus().unregister(potionHandlerServer); + this.potionHandlerServer = null; + } + } + + // spotless:off + protected void registerPackets() { + ThaumicTinkerer.netHandler.registerMessage(PacketSoulHearts.class, PacketSoulHearts.class, 142 + 0, Side.CLIENT); + ThaumicTinkerer.netHandler.registerMessage(PacketToggleArmor.class, PacketToggleArmor.class, 142 + 1, Side.CLIENT); + ThaumicTinkerer.netHandler.registerMessage(PacketToggleArmor.class, PacketToggleArmor.class, 142 + 2, Side.SERVER); + ThaumicTinkerer.netHandler.registerMessage(PacketWarpGateButton.class, PacketWarpGateButton.class, 142 + 3, Side.SERVER); + ThaumicTinkerer.netHandler.registerMessage(PacketWarpGateTeleport.class, PacketWarpGateTeleport.class, 142 + 4, Side.SERVER); + ThaumicTinkerer.netHandler.registerMessage(PacketEnchanterAddEnchant.class, PacketEnchanterAddEnchant.class, 142 + 5, Side.SERVER); + ThaumicTinkerer.netHandler.registerMessage(PacketEnchanterStartWorking.class, PacketEnchanterStartWorking.class, 142 + 6, Side.SERVER); + ThaumicTinkerer.netHandler.registerMessage(PacketMobMagnetButton.class, PacketMobMagnetButton.class, 142 + 7, Side.SERVER); + ThaumicTinkerer.netHandler.registerMessage(PacketTabletButton.class, PacketTabletButton.class, 142 + 8, Side.SERVER); + ThaumicTinkerer.netHandler.registerMessage(PacketPlacerButton.class, PacketPlacerButton.class, 142 + 9, Side.SERVER); + } + // spotless:on + public void registerVersionChecker() { NBTTagCompound compound = new NBTTagCompound(); compound.setString("curseProjectName", "75598-thaumic-tinkerer"); @@ -200,13 +223,6 @@ public class TTCommonProxy { FMLInterModComms.sendRuntimeMessage(LibMisc.MOD_ID, "VersionChecker", "addCurseCheck", compound); } - public void postInit(FMLPostInitializationEvent event) { - ResearchHelper.initResearch(); - ThaumicTinkerer.registry.postInit(); - AspectCropLootManager.populateLootMap(); - ItemFocusDeflect.setupBlackList(); - } - @Optional.Method(modid = "EnderIO") protected void InitEnderIO() { EnderIO.registerPlanters(); diff --git a/src/main/java/thaumic/tinkerer/common/potion/ModPotions.java b/src/main/java/thaumic/tinkerer/common/potion/ModPotions.java index eebada3d..f517503f 100644 --- a/src/main/java/thaumic/tinkerer/common/potion/ModPotions.java +++ b/src/main/java/thaumic/tinkerer/common/potion/ModPotions.java @@ -15,9 +15,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import net.minecraft.potion.Potion; -import net.minecraftforge.common.MinecraftForge; -import cpw.mods.fml.common.FMLCommonHandler; import thaumic.tinkerer.common.core.handler.ConfigHandler; public final class ModPotions { @@ -28,8 +26,6 @@ public final class ModPotions { public static Potion potionAir; public static void initPotions() { - MinecraftForge.EVENT_BUS.register(new PotionEffectHandler()); - FMLCommonHandler.instance().bus().register(new PotionEffectHandler()); // Code based on potion code from WayOfTime Potion[] potionTypes = null; diff --git a/src/main/java/thaumic/tinkerer/common/potion/PotionEffectHandler.java b/src/main/java/thaumic/tinkerer/common/potion/PotionEffectHandler.java deleted file mode 100644 index 8f66fde9..00000000 --- a/src/main/java/thaumic/tinkerer/common/potion/PotionEffectHandler.java +++ /dev/null @@ -1,193 +0,0 @@ -package thaumic.tinkerer.common.potion; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Random; - -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Blocks; -import net.minecraftforge.event.entity.living.LivingAttackEvent; - -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.common.gameevent.TickEvent; -import thaumic.tinkerer.common.ThaumicTinkerer; -import thaumic.tinkerer.common.block.BlockForcefield; - -/** - * Created by pixlepix on 4/19/14. - */ -public class PotionEffectHandler { - - public static HashMap airPotionHitClient = new HashMap<>(); - public static HashMap airPotionHitServer = new HashMap<>(); - public static HashMap firePotionHitClient = new HashMap<>(); - public static HashMap firePotionHitServer = new HashMap<>(); - - @SubscribeEvent - public void onLivingHurt(LivingAttackEvent e) { - if (e.source.getSourceOfDamage() instanceof EntityPlayer) { - EntityPlayer p = (EntityPlayer) e.source.getSourceOfDamage(); - if (p.isPotionActive(ModPotions.potionAir)) { - if (p.worldObj.isRemote) { - airPotionHitClient.put(e.entity, e.entity.worldObj.getTotalWorldTime()); - } else { - airPotionHitServer.put(e.entity, e.entity.worldObj.getTotalWorldTime()); - } - } - if (p.isPotionActive(ModPotions.potionFire)) { - if (p.worldObj.isRemote) { - firePotionHitClient.put(e.entity, e.entity.worldObj.getTotalWorldTime()); - } else { - firePotionHitServer.put(e.entity, e.entity.worldObj.getTotalWorldTime()); - } - } - if (p.isPotionActive(ModPotions.potionEarth)) { - boolean xAxis = Math.abs(e.entity.posZ - p.posZ) < Math.abs(e.entity.posX - p.posX); - int centerX = (int) ((e.entity.posX + p.posX) / 2); - - int centerY = (int) (p.posY + 2); - int centerZ = (int) ((e.entity.posZ + p.posZ) / 2); - - for (int i = -2; i < 3; i++) { - for (int j = -2; j < 3; j++) { - if (xAxis) { - if (!p.worldObj.isRemote) { - if (p.worldObj.isAirBlock(centerX, centerY + i, centerZ + j)) { - p.worldObj.setBlock( - centerX, - centerY + i, - centerZ + j, - ThaumicTinkerer.registry.getFirstBlockFromClass(BlockForcefield.class)); - } - } else { - ThaumicTinkerer.tcProxy - .blockSparkle(p.worldObj, centerX, centerY + i, centerZ + j, 100, 100); - } - - } else { - if (!p.worldObj.isRemote) { - if (p.worldObj.isAirBlock(centerX + j, centerY + i, centerZ)) { - p.worldObj.setBlock( - centerX + j, - centerY + i, - centerZ, - ThaumicTinkerer.registry.getFirstBlockFromClass(BlockForcefield.class)); - } - } else { - ThaumicTinkerer.tcProxy - .blockSparkle(p.worldObj, centerX + j, centerY + i, centerZ, 100, 100); - } - - } - } - } - } - } - } - - @SubscribeEvent - public void onPlayerTick(TickEvent.PlayerTickEvent e) { - if (e.player.isPotionActive(ModPotions.potionWater)) { - for (int x = (int) (e.player.posX - 2); x < e.player.posX + 2; x++) { - for (int y = (int) (e.player.posY - 2); y < e.player.posY + 2; y++) { - for (int z = (int) (e.player.posZ - 2); z < e.player.posZ + 2; z++) { - if (e.player.worldObj.getBlock(x, y, z) == Blocks.lava - || e.player.worldObj.getBlock(x, y, z) == Blocks.flowing_lava) { - e.player.worldObj.setBlock(x, y, z, Blocks.obsidian); - if (e.player.worldObj.isRemote) { - ThaumicTinkerer.tcProxy.burst(e.player.worldObj, x + .5, y + .5, z + .5, 1.2F); - } - } - } - } - } - } - } - - @SubscribeEvent - public void onTickClient(TickEvent.ClientTickEvent e) { - - Iterator iter = airPotionHitClient.keySet().iterator(); - while (iter.hasNext()) { - Entity target = iter.next(); - if (target.isEntityAlive()) { - if (target.worldObj.getTotalWorldTime() % 5 == 0) { - ThaumicTinkerer.tcProxy.burst(target.worldObj, target.posX, target.posY, target.posZ, .5F); - } - } - if (target.worldObj.getTotalWorldTime() > airPotionHitClient.get(target) + 20) { - iter.remove(); - } - } - - iter = firePotionHitClient.keySet().iterator(); - while (iter.hasNext()) { - Entity target = iter.next(); - if (target.isEntityAlive()) { - if (target.worldObj.getTotalWorldTime() % 5 == 0) { - Random rand = new Random(); - - for (int i = 0; i < 30; i++) { - double theta = rand.nextFloat() * 2 * Math.PI; - - double phi = rand.nextFloat() * 2 * Math.PI; - double r = 2.5; - - double x = r * Math.sin(theta) * Math.cos(phi); - - double y = r * Math.sin(theta) * Math.sin(phi); - - double z = r * Math.cos(theta); - - ThaumicTinkerer.tcProxy.wispFX2( - target.worldObj, - target.posX + x, - target.posY + y + 1, - target.posZ + z, - .1F, - 4, - true, - true, - 1F); - } - } - } - if (target.worldObj.getTotalWorldTime() > firePotionHitClient.get(target) + 6000) { - iter.remove(); - } - } - } - - @SubscribeEvent - public void onTickServer(TickEvent.ServerTickEvent e) { - - Iterator iter = airPotionHitServer.keySet().iterator(); - while (iter.hasNext()) { - Entity target = iter.next(); - if (target.isEntityAlive()) { - if (target.worldObj.getTotalWorldTime() % 5 == 0) { - Random rand = new Random(); - target.setVelocity(rand.nextFloat() - .5, rand.nextFloat(), rand.nextFloat() - .5); - } - } - if (target.worldObj.getTotalWorldTime() > airPotionHitServer.get(target) + 20) { - iter.remove(); - } - } - - // Fire Potion - iter = firePotionHitServer.keySet().iterator(); - while (iter.hasNext()) { - Entity target = iter.next(); - if (target.isEntityAlive()) { - if (target.worldObj.getTotalWorldTime() % 5 == 0) { - target.setFire(6); - } - } - if (target.worldObj.getTotalWorldTime() > firePotionHitServer.get(target) + 6000) { - iter.remove(); - } - } - } -} diff --git a/src/main/java/thaumic/tinkerer/common/potion/PotionEffectHandlerClient.java b/src/main/java/thaumic/tinkerer/common/potion/PotionEffectHandlerClient.java new file mode 100644 index 00000000..061a0144 --- /dev/null +++ b/src/main/java/thaumic/tinkerer/common/potion/PotionEffectHandlerClient.java @@ -0,0 +1,122 @@ +package thaumic.tinkerer.common.potion; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Random; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Blocks; +import net.minecraftforge.event.entity.living.LivingAttackEvent; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.TickEvent; +import cpw.mods.fml.relauncher.Side; +import thaumic.tinkerer.common.ThaumicTinkerer; + +public final class PotionEffectHandlerClient { + + private final HashMap airPotionHitClient = new HashMap<>(); + private final HashMap firePotionHitClient = new HashMap<>(); + + @SubscribeEvent + public void onLivingHurt(LivingAttackEvent e) { + if (e.source.getSourceOfDamage() instanceof EntityPlayer p && p.worldObj.isRemote) { + if (p.isPotionActive(ModPotions.potionAir)) { + airPotionHitClient.put(e.entity, e.entity.worldObj.getTotalWorldTime()); + } + if (p.isPotionActive(ModPotions.potionFire)) { + firePotionHitClient.put(e.entity, e.entity.worldObj.getTotalWorldTime()); + } + if (p.isPotionActive(ModPotions.potionEarth)) { + boolean xAxis = Math.abs(e.entity.posZ - p.posZ) < Math.abs(e.entity.posX - p.posX); + int centerX = (int) ((e.entity.posX + p.posX) / 2); + + int centerY = (int) (p.posY + 2); + int centerZ = (int) ((e.entity.posZ + p.posZ) / 2); + + for (int i = -2; i < 3; i++) { + for (int j = -2; j < 3; j++) { + if (xAxis) { + ThaumicTinkerer.tcProxy + .blockSparkle(p.worldObj, centerX, centerY + i, centerZ + j, 100, 100); + } else { + ThaumicTinkerer.tcProxy + .blockSparkle(p.worldObj, centerX + j, centerY + i, centerZ, 100, 100); + } + } + } + } + } + } + + @SubscribeEvent + public void onPlayerTick(TickEvent.PlayerTickEvent e) { + if (e.side == Side.CLIENT && e.player.isPotionActive(ModPotions.potionWater)) { + for (int x = (int) (e.player.posX - 2); x < e.player.posX + 2; x++) { + for (int y = (int) (e.player.posY - 2); y < e.player.posY + 2; y++) { + for (int z = (int) (e.player.posZ - 2); z < e.player.posZ + 2; z++) { + if (e.player.worldObj.getBlock(x, y, z) == Blocks.lava + || e.player.worldObj.getBlock(x, y, z) == Blocks.flowing_lava) { + ThaumicTinkerer.tcProxy.burst(e.player.worldObj, x + .5, y + .5, z + .5, 1.2F); + } + } + } + } + } + } + + @SubscribeEvent + public void onTickClient(TickEvent.ClientTickEvent e) { + + Iterator iter = airPotionHitClient.keySet().iterator(); + while (iter.hasNext()) { + Entity target = iter.next(); + if (target.isEntityAlive()) { + if (target.worldObj.getTotalWorldTime() % 5 == 0) { + ThaumicTinkerer.tcProxy.burst(target.worldObj, target.posX, target.posY, target.posZ, .5F); + } + } + if (target.worldObj.getTotalWorldTime() > airPotionHitClient.get(target) + 20) { + iter.remove(); + } + } + + iter = firePotionHitClient.keySet().iterator(); + while (iter.hasNext()) { + Entity target = iter.next(); + if (target.isEntityAlive()) { + if (target.worldObj.getTotalWorldTime() % 5 == 0) { + Random rand = new Random(); + + for (int i = 0; i < 30; i++) { + double theta = rand.nextFloat() * 2 * Math.PI; + + double phi = rand.nextFloat() * 2 * Math.PI; + double r = 2.5; + + double x = r * Math.sin(theta) * Math.cos(phi); + + double y = r * Math.sin(theta) * Math.sin(phi); + + double z = r * Math.cos(theta); + + ThaumicTinkerer.tcProxy.wispFX2( + target.worldObj, + target.posX + x, + target.posY + y + 1, + target.posZ + z, + .1F, + 4, + true, + true, + 1F); + } + } + } + if (target.worldObj.getTotalWorldTime() > firePotionHitClient.get(target) + 6000) { + iter.remove(); + } + } + } +} diff --git a/src/main/java/thaumic/tinkerer/common/potion/PotionEffectHandlerServer.java b/src/main/java/thaumic/tinkerer/common/potion/PotionEffectHandlerServer.java new file mode 100644 index 00000000..46b84522 --- /dev/null +++ b/src/main/java/thaumic/tinkerer/common/potion/PotionEffectHandlerServer.java @@ -0,0 +1,114 @@ +package thaumic.tinkerer.common.potion; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Random; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Blocks; +import net.minecraftforge.event.entity.living.LivingAttackEvent; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.TickEvent; +import cpw.mods.fml.relauncher.Side; +import thaumic.tinkerer.common.ThaumicTinkerer; +import thaumic.tinkerer.common.block.BlockForcefield; + +/** + * Created by pixlepix on 4/19/14. + */ +public final class PotionEffectHandlerServer { + + private final HashMap airPotionHitServer = new HashMap<>(); + private final HashMap firePotionHitServer = new HashMap<>(); + + @SubscribeEvent + public void onLivingHurt(LivingAttackEvent e) { + if (e.source.getSourceOfDamage() instanceof EntityPlayer p && !p.worldObj.isRemote) { + if (p.isPotionActive(ModPotions.potionAir)) { + airPotionHitServer.put(e.entity, e.entity.worldObj.getTotalWorldTime()); + } + if (p.isPotionActive(ModPotions.potionFire)) { + firePotionHitServer.put(e.entity, e.entity.worldObj.getTotalWorldTime()); + } + if (p.isPotionActive(ModPotions.potionEarth)) { + boolean xAxis = Math.abs(e.entity.posZ - p.posZ) < Math.abs(e.entity.posX - p.posX); + int centerX = (int) ((e.entity.posX + p.posX) / 2); + + int centerY = (int) (p.posY + 2); + int centerZ = (int) ((e.entity.posZ + p.posZ) / 2); + + for (int i = -2; i < 3; i++) { + for (int j = -2; j < 3; j++) { + if (xAxis) { + if (p.worldObj.isAirBlock(centerX, centerY + i, centerZ + j)) { + p.worldObj.setBlock( + centerX, + centerY + i, + centerZ + j, + ThaumicTinkerer.registry.getFirstBlockFromClass(BlockForcefield.class)); + } + } else { + if (p.worldObj.isAirBlock(centerX + j, centerY + i, centerZ)) { + p.worldObj.setBlock( + centerX + j, + centerY + i, + centerZ, + ThaumicTinkerer.registry.getFirstBlockFromClass(BlockForcefield.class)); + } + } + } + } + } + } + } + + @SubscribeEvent + public void onPlayerTick(TickEvent.PlayerTickEvent e) { + if (e.side == Side.SERVER && e.player.isPotionActive(ModPotions.potionWater)) { + for (int x = (int) (e.player.posX - 2); x < e.player.posX + 2; x++) { + for (int y = (int) (e.player.posY - 2); y < e.player.posY + 2; y++) { + for (int z = (int) (e.player.posZ - 2); z < e.player.posZ + 2; z++) { + if (e.player.worldObj.getBlock(x, y, z) == Blocks.lava + || e.player.worldObj.getBlock(x, y, z) == Blocks.flowing_lava) { + e.player.worldObj.setBlock(x, y, z, Blocks.obsidian); + } + } + } + } + } + } + + @SubscribeEvent + public void onTickServer(TickEvent.ServerTickEvent e) { + + Iterator iter = airPotionHitServer.keySet().iterator(); + while (iter.hasNext()) { + Entity target = iter.next(); + if (target.isEntityAlive()) { + if (target.worldObj.getTotalWorldTime() % 5 == 0) { + Random rand = new Random(); + target.setVelocity(rand.nextFloat() - .5, rand.nextFloat(), rand.nextFloat() - .5); + } + } + if (target.worldObj.getTotalWorldTime() > airPotionHitServer.get(target) + 20) { + iter.remove(); + } + } + + // Fire Potion + iter = firePotionHitServer.keySet().iterator(); + while (iter.hasNext()) { + Entity target = iter.next(); + if (target.isEntityAlive()) { + if (target.worldObj.getTotalWorldTime() % 5 == 0) { + target.setFire(6); + } + } + if (target.worldObj.getTotalWorldTime() > firePotionHitServer.get(target) + 6000) { + iter.remove(); + } + } + } +}