From c7b02735f94aff96bff89e06acbecc297f497fa4 Mon Sep 17 00:00:00 2001 From: mts2200 Date: Wed, 16 Oct 2024 13:04:11 +0300 Subject: [PATCH] Use IEEP to store enchantment seeds --- .../core/handlers/EntityEventHandler.java | 27 +++++++ .../core/handlers/ServerEventHandler.java | 64 ++++++--------- .../etfuturum/core/proxy/CommonProxy.java | 3 + .../inventory/ContainerEnchantment.java | 16 ++-- .../etfuturum/storage/EtFuturumPlayer.java | 78 +++++++++++++++++++ 5 files changed, 137 insertions(+), 51 deletions(-) create mode 100644 src/main/java/ganymedes01/etfuturum/core/handlers/EntityEventHandler.java create mode 100644 src/main/java/ganymedes01/etfuturum/storage/EtFuturumPlayer.java diff --git a/src/main/java/ganymedes01/etfuturum/core/handlers/EntityEventHandler.java b/src/main/java/ganymedes01/etfuturum/core/handlers/EntityEventHandler.java new file mode 100644 index 00000000..cc251eff --- /dev/null +++ b/src/main/java/ganymedes01/etfuturum/core/handlers/EntityEventHandler.java @@ -0,0 +1,27 @@ +package ganymedes01.etfuturum.core.handlers; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import ganymedes01.etfuturum.storage.EtFuturumPlayer; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraftforge.event.entity.EntityEvent; +import net.minecraftforge.event.entity.player.PlayerEvent; + +public final class EntityEventHandler { + public static final EntityEventHandler INSTANCE = new EntityEventHandler(); + + private EntityEventHandler() { + // NO-OP + } + + @SubscribeEvent + public void onEntityConstruct(EntityEvent.EntityConstructing event) { + if (event.entity instanceof EntityPlayer) { + EtFuturumPlayer.register(((EntityPlayer) event.entity)); + } + } + + @SubscribeEvent + public void onPlayerClone(PlayerEvent.Clone event) { + EtFuturumPlayer.clone(event.original, event.entityPlayer); + } +} diff --git a/src/main/java/ganymedes01/etfuturum/core/handlers/ServerEventHandler.java b/src/main/java/ganymedes01/etfuturum/core/handlers/ServerEventHandler.java index 166d0cd9..cb3ba84a 100644 --- a/src/main/java/ganymedes01/etfuturum/core/handlers/ServerEventHandler.java +++ b/src/main/java/ganymedes01/etfuturum/core/handlers/ServerEventHandler.java @@ -33,13 +33,13 @@ import ganymedes01.etfuturum.entities.ai.EntityAIOpenCustomDoor; import ganymedes01.etfuturum.gamerule.DoWeatherCycle; import ganymedes01.etfuturum.gamerule.RandomTickSpeed; -import ganymedes01.etfuturum.inventory.ContainerEnchantment; import ganymedes01.etfuturum.items.ItemArrowTipped; import ganymedes01.etfuturum.lib.Reference; import ganymedes01.etfuturum.network.AttackYawMessage; import ganymedes01.etfuturum.network.BlackHeartParticlesMessage; import ganymedes01.etfuturum.recipes.ModRecipes; import ganymedes01.etfuturum.spectator.SpectatorMode; +import ganymedes01.etfuturum.storage.EtFuturumPlayer; import ganymedes01.etfuturum.tileentities.TileEntityGateway; import ganymedes01.etfuturum.world.EtFuturumWorldListener; import ganymedes01.etfuturum.world.nether.biome.utils.NetherBiomeManager; @@ -101,9 +101,13 @@ import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.mutable.MutableFloat; -import java.io.*; +import java.io.BufferedReader; +import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -425,45 +429,25 @@ public void arrowLoose(ArrowLooseEvent event) { @SubscribeEvent public void onPlayerLoadFromFileEvent(PlayerEvent.LoadFromFile event) { - if (!ConfigBlocksItems.enableEnchantingTable) - return; - try { - File file = event.getPlayerFile(Reference.MOD_ID); - if (!file.exists()) { - file.createNewFile(); - return; - } - - BufferedReader br = new BufferedReader(new FileReader(file)); - String line = br.readLine(); - if (line != null) { - int seed = Integer.parseInt(line); - ContainerEnchantment.seeds.put(event.playerUUID, seed); - } - br.close(); - } catch (Exception ignored) { - } - } - - @SubscribeEvent - public void onPlayerSaveFromFileEvent(PlayerEvent.SaveToFile event) { - if (!ConfigBlocksItems.enableEnchantingTable) - return; - try { - File file = event.getPlayerFile(Reference.MOD_ID); - if (!file.exists()) { - file.createNewFile(); - return; - } - - Integer seed = ContainerEnchantment.seeds.get(event.playerUUID); - if (seed != null) { - BufferedWriter bw = new BufferedWriter(new FileWriter(file)); - bw.write(seed.toString()); - bw.close(); + if (!ConfigBlocksItems.enableEnchantingTable) return; + Path file = event.getPlayerFile(Reference.MOD_ID).toPath(); + + if (Files.exists(file)) { + EtFuturumPlayer storage = EtFuturumPlayer.get(event.entityPlayer); + + try { + try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) { + int seed = Integer.parseInt(reader.readLine()); + storage.setEnchantmentSeed(seed); + } catch (NumberFormatException ignore) { + // NO-OP + } finally { + Files.delete(file); + } + } catch (IOException ignore) { + // NO-OP } - } catch (IOException e) { - } + } } @SubscribeEvent(priority = EventPriority.HIGHEST) diff --git a/src/main/java/ganymedes01/etfuturum/core/proxy/CommonProxy.java b/src/main/java/ganymedes01/etfuturum/core/proxy/CommonProxy.java index 4032d797..9ebe4d6e 100644 --- a/src/main/java/ganymedes01/etfuturum/core/proxy/CommonProxy.java +++ b/src/main/java/ganymedes01/etfuturum/core/proxy/CommonProxy.java @@ -11,6 +11,7 @@ import ganymedes01.etfuturum.ModItems; import ganymedes01.etfuturum.client.gui.inventory.*; import ganymedes01.etfuturum.configuration.configs.*; +import ganymedes01.etfuturum.core.handlers.EntityEventHandler; import ganymedes01.etfuturum.core.handlers.SculkEventHandler; import ganymedes01.etfuturum.core.handlers.ServerEventHandler; import ganymedes01.etfuturum.core.handlers.WorldEventHandler; @@ -43,6 +44,8 @@ public class CommonProxy implements IGuiHandler { public void registerEvents() { + MinecraftForge.EVENT_BUS.register(EntityEventHandler.INSTANCE); + FMLCommonHandler.instance().bus().register(ServerEventHandler.INSTANCE); MinecraftForge.EVENT_BUS.register(ServerEventHandler.INSTANCE); diff --git a/src/main/java/ganymedes01/etfuturum/inventory/ContainerEnchantment.java b/src/main/java/ganymedes01/etfuturum/inventory/ContainerEnchantment.java index 9207bb1d..9e83f120 100644 --- a/src/main/java/ganymedes01/etfuturum/inventory/ContainerEnchantment.java +++ b/src/main/java/ganymedes01/etfuturum/inventory/ContainerEnchantment.java @@ -3,6 +3,7 @@ import ganymedes01.etfuturum.ModBlocks; import ganymedes01.etfuturum.api.EnchantingFuelRegistry; import ganymedes01.etfuturum.lib.Reference; +import ganymedes01.etfuturum.storage.EtFuturumPlayer; import net.minecraft.enchantment.EnchantmentData; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.player.EntityPlayer; @@ -13,15 +14,11 @@ import net.minecraft.world.World; import net.minecraftforge.common.ForgeHooks; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Random; public class ContainerEnchantment extends Container { - public static final Map seeds = new HashMap<>(); - /** * SlotEnchantmentTable object with ItemStack to be enchanted */ @@ -93,16 +90,13 @@ public boolean isItemValid(ItemStack stack) { } private static void setEnchantSeed(EntityPlayer player, int seed) { - seeds.put(player.getUniqueID().toString(), seed); + EtFuturumPlayer storage = EtFuturumPlayer.get(player); + storage.setEnchantmentSeed(seed); } private static int getEnchantSeed(EntityPlayer player) { - Integer seed = seeds.get(player.getUniqueID().toString()); - if (seed == null) { - seed = player.worldObj.rand.nextInt(); - setEnchantSeed(player, seed); - } - return seed; + EtFuturumPlayer storage = EtFuturumPlayer.get(player); + return storage.getEnchantmentSeed(); } private static void chargeForEnchant(EntityPlayer player, Random rand, int amount) { diff --git a/src/main/java/ganymedes01/etfuturum/storage/EtFuturumPlayer.java b/src/main/java/ganymedes01/etfuturum/storage/EtFuturumPlayer.java new file mode 100644 index 00000000..d6ce5090 --- /dev/null +++ b/src/main/java/ganymedes01/etfuturum/storage/EtFuturumPlayer.java @@ -0,0 +1,78 @@ +package ganymedes01.etfuturum.storage; + +import ganymedes01.etfuturum.configuration.configs.ConfigBlocksItems; +import ganymedes01.etfuturum.lib.Reference; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; +import net.minecraftforge.common.IExtendedEntityProperties; +import net.minecraftforge.common.util.Constants; + +public final class EtFuturumPlayer implements IExtendedEntityProperties { + private static final String PROPERTIES_NAME = Reference.MOD_ID; + + private static final String TAG_ENCHANTMENT_SEED = "EnchantmentSeed"; + + private int enchantmentSeed = 0; + + private EtFuturumPlayer() { + // NO-OP + } + + @Override + public void saveNBTData(NBTTagCompound compound) { + NBTTagCompound tag = new NBTTagCompound(); + + if (ConfigBlocksItems.enableEnchantingTable) { + tag.setInteger(TAG_ENCHANTMENT_SEED, enchantmentSeed); + } + + if (!tag.hasNoTags()) { + compound.setTag(PROPERTIES_NAME, tag); + } + } + + @Override + public void loadNBTData(NBTTagCompound compound) { + if (!compound.hasKey(PROPERTIES_NAME, Constants.NBT.TAG_COMPOUND)) return; + NBTTagCompound tag = compound.getCompoundTag(PROPERTIES_NAME); + + if (tag.hasKey(TAG_ENCHANTMENT_SEED, Constants.NBT.TAG_INT)) { + enchantmentSeed = tag.getInteger(TAG_ENCHANTMENT_SEED); + } + } + + @Override + public void init(Entity entity, World world) { + enchantmentSeed = world.rand.nextInt(); + } + + public static EtFuturumPlayer get(EntityPlayer player) { + EtFuturumPlayer data = (EtFuturumPlayer) player.getExtendedProperties(PROPERTIES_NAME); + return data == null ? register(player) : data; + } + + public static EtFuturumPlayer register(EntityPlayer player) { + EtFuturumPlayer data = new EtFuturumPlayer(); + player.registerExtendedProperties(PROPERTIES_NAME, data); + return data; + } + + public static void clone(EntityPlayer original, EntityPlayer current) { + if (original != null && current != null) { + NBTTagCompound nbt = new NBTTagCompound(); + get(original).saveNBTData(nbt); + get(current).loadNBTData(nbt); + } + } + + // Generated shit... + public int getEnchantmentSeed() { + return enchantmentSeed; + } + + public void setEnchantmentSeed(int enchantmentSeed) { + this.enchantmentSeed = enchantmentSeed; + } +}