forked from KryptonCaptain/Et-Futurum
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #531 from mts2200/ieep
Use IEEP to store enchantment seeds
- Loading branch information
Showing
5 changed files
with
137 additions
and
51 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/ganymedes01/etfuturum/core/handlers/EntityEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/ganymedes01/etfuturum/storage/EtFuturumPlayer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |