Advertisement
Guest User

Untitled

a guest
Apr 14th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package tutorial.generic;
  2. import net.minecraft.block.BlockGrass;
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.nbt.NBTTagCompound;
  5. import net.minecraftforge.event.entity.EntityEvent;
  6. import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
  7. import net.minecraftforge.event.entity.EntityJoinWorldEvent;
  8. import net.minecraftforge.event.entity.living.LivingDeathEvent;
  9. import net.minecraftforge.event.world.BlockEvent.BreakEvent;
  10. import cpw.mods.fml.common.eventhandler.SubscribeEvent;
  11.  
  12.  
  13. public class genericHooks {
  14.     @SubscribeEvent
  15. public void onEntityConstructing(EntityConstructing event) {
  16.         if (event.entity instanceof EntityPlayer) {
  17.             if (PlayerInformation.get((EntityPlayer) event.entity) == null)
  18.                 PlayerInformation.register((EntityPlayer) event.entity);
  19.         }
  20.     }
  21.  
  22.  
  23. @SubscribeEvent
  24. public void onBreakEvent(BreakEvent event){
  25.  
  26.      if (event.getPlayer() instanceof EntityPlayer) {
  27.          EntityPlayer ent = (EntityPlayer) event.getPlayer();
  28.          PlayerInformation playerInfo = PlayerInformation.get((EntityPlayer) event.getPlayer());
  29.      
  30.          if ( event.block.getClass() == BlockGrass.class ){
  31.              System.out.println("Broken block:"+event.block.getClass());             
  32.              playerInfo.addXP(1);
  33.              System.out.println("From NBT, Player XP:" + playerInfo.getXP());
  34.              }
  35.          }
  36.     }
  37.    
  38.     // These are methods in the EventHandler class, in case you don't know that by now
  39.  
  40.     // we need to add this new event - it is called for every living entity upon death
  41.     @SubscribeEvent
  42.     public void onLivingDeathEvent(LivingDeathEvent event)
  43.     {
  44.     // we only want to save data for players (most likely, anyway)
  45.     if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer)
  46.     {
  47.     // NOTE: See step 6 for a way to do this all in one line!!!
  48.     // create a new NBT Tag Compound to store the IExtendedEntityProperties data
  49.     NBTTagCompound playerData = new NBTTagCompound();
  50.     // write the data to the new compound
  51.     ((PlayerInformation)(event.entity.getExtendedProperties(PlayerInformation.EXT_PROP_NAME))).saveNBTData(playerData);
  52.     // and store it in our proxy
  53.     CommonProxy.storeEntityData(((EntityPlayer) event.entity).username, playerData);
  54.     // call our handy static one-liner to save custom data to the proxy
  55.     PlayerInformation.saveProxyData((EntityPlayer) event.entity);
  56.     }
  57.     }
  58.  
  59.     // we already have this event, but we need to modify it some
  60.     @SubscribeEvent
  61.     public void onEntityJoinWorld(EntityJoinWorldEvent event)
  62.     {
  63.     if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer)
  64.     {
  65.     // NOTE: See step 6 for a way to do this all in one line!!!
  66.     // before syncing the properties, we must first check if the player has some saved in the proxy
  67.     // recall that 'getEntityData' also removes it from the map, so be sure to store it locally
  68.     NBTTagCompound playerData = CommonProxy.getEntityData(((EntityPlayer) event.entity).username);
  69.     // make sure the compound isn't null
  70.     if (playerData != null) {
  71.     // then load the data back into the player's IExtendedEntityProperties
  72.     ((PlayerInformation)(event.entity.getExtendedProperties(PlayerInformation.EXT_PROP_NAME))).loadNBTData(playerData);
  73.     }
  74.     // finally, we sync the data between server and client (we did this earlier in 3.3)
  75.     ((PlayerInformation)(event.entity.getExtendedProperties(PlayerInformation.EXT_PROP_NAME))).syncExtendedProperties();
  76.     }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement