Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tutorial.generic;
- import net.minecraft.block.BlockGrass;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraftforge.event.entity.EntityEvent;
- import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
- import net.minecraftforge.event.entity.EntityJoinWorldEvent;
- import net.minecraftforge.event.entity.living.LivingDeathEvent;
- import net.minecraftforge.event.world.BlockEvent.BreakEvent;
- import cpw.mods.fml.common.eventhandler.SubscribeEvent;
- public class genericHooks {
- @SubscribeEvent
- public void onEntityConstructing(EntityConstructing event) {
- if (event.entity instanceof EntityPlayer) {
- if (PlayerInformation.get((EntityPlayer) event.entity) == null)
- PlayerInformation.register((EntityPlayer) event.entity);
- }
- }
- @SubscribeEvent
- public void onBreakEvent(BreakEvent event){
- if (event.getPlayer() instanceof EntityPlayer) {
- EntityPlayer ent = (EntityPlayer) event.getPlayer();
- PlayerInformation playerInfo = PlayerInformation.get((EntityPlayer) event.getPlayer());
- if ( event.block.getClass() == BlockGrass.class ){
- System.out.println("Broken block:"+event.block.getClass());
- playerInfo.addXP(1);
- System.out.println("From NBT, Player XP:" + playerInfo.getXP());
- }
- }
- }
- // These are methods in the EventHandler class, in case you don't know that by now
- // we need to add this new event - it is called for every living entity upon death
- @SubscribeEvent
- public void onLivingDeathEvent(LivingDeathEvent event)
- {
- // we only want to save data for players (most likely, anyway)
- if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer)
- {
- // NOTE: See step 6 for a way to do this all in one line!!!
- // create a new NBT Tag Compound to store the IExtendedEntityProperties data
- NBTTagCompound playerData = new NBTTagCompound();
- // write the data to the new compound
- ((PlayerInformation)(event.entity.getExtendedProperties(PlayerInformation.EXT_PROP_NAME))).saveNBTData(playerData);
- // and store it in our proxy
- CommonProxy.storeEntityData(((EntityPlayer) event.entity).username, playerData);
- // call our handy static one-liner to save custom data to the proxy
- PlayerInformation.saveProxyData((EntityPlayer) event.entity);
- }
- }
- // we already have this event, but we need to modify it some
- @SubscribeEvent
- public void onEntityJoinWorld(EntityJoinWorldEvent event)
- {
- if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer)
- {
- // NOTE: See step 6 for a way to do this all in one line!!!
- // before syncing the properties, we must first check if the player has some saved in the proxy
- // recall that 'getEntityData' also removes it from the map, so be sure to store it locally
- NBTTagCompound playerData = CommonProxy.getEntityData(((EntityPlayer) event.entity).username);
- // make sure the compound isn't null
- if (playerData != null) {
- // then load the data back into the player's IExtendedEntityProperties
- ((PlayerInformation)(event.entity.getExtendedProperties(PlayerInformation.EXT_PROP_NAME))).loadNBTData(playerData);
- }
- // finally, we sync the data between server and client (we did this earlier in 3.3)
- ((PlayerInformation)(event.entity.getExtendedProperties(PlayerInformation.EXT_PROP_NAME))).syncExtendedProperties();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement