Skip to content

Commit

Permalink
Merge pull request #531 from mts2200/ieep
Browse files Browse the repository at this point in the history
Use IEEP to store enchantment seeds
  • Loading branch information
Roadhog360 authored Oct 25, 2024
2 parents 658ecbb + c7b0273 commit 8aaf5fe
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, Integer> seeds = new HashMap<>();

/**
* SlotEnchantmentTable object with ItemStack to be enchanted
*/
Expand Down Expand Up @@ -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) {
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/ganymedes01/etfuturum/storage/EtFuturumPlayer.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 8aaf5fe

Please sign in to comment.