Advertisement
TechMage66

ItemTalismanWater

Dec 5th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1. package com.techmage.magetech.item;
  2.  
  3. import com.techmage.magetech.reference.Names;
  4. import net.minecraft.entity.Entity;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.nbt.NBTTagCompound;
  8. import net.minecraft.potion.Potion;
  9. import net.minecraft.potion.PotionEffect;
  10. import net.minecraft.world.World;
  11.  
  12. public class ItemTalismanWater extends ItemMageTech_Magic
  13. {
  14.     public ItemTalismanWater()
  15.     {
  16.         super();
  17.         this.setUnlocalizedName(Names.Items.TALISMAN_WATER);
  18.         this.setMaxStackSize(1);
  19.     }
  20.  
  21.     @Override
  22.     public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn)
  23.     {
  24.         // Only active on the server
  25.         if (!worldIn.isRemote)
  26.         {
  27.             // Check if the item has a tagCompound
  28.             if (itemStackIn.hasTagCompound())
  29.             {
  30.                 // Set the items tagCompound's key "activated" to true
  31.                 itemStackIn.getTagCompound().setBoolean("activated", true);
  32.             }
  33.  
  34.             // Create a new tagCompound for the item and set it's key "activated" to true
  35.             else
  36.             {
  37.                 itemStackIn.setTagCompound(new NBTTagCompound());
  38.                 itemStackIn.getTagCompound().setBoolean("activated", true);
  39.             }
  40.         }
  41.  
  42.         return itemStackIn;
  43.     }
  44.  
  45.     @Override
  46.     public void onUpdate(ItemStack itemStackIn, World worldIn, Entity entityIn, int itemSlot, boolean isSelected)
  47.     {
  48.         // Only active on the server
  49.         if (!worldIn.isRemote)
  50.         {
  51.             // Check if the entity holding the item a a player
  52.             if (entityIn instanceof EntityPlayer)
  53.             {
  54.                 // Check if the item has a tagCompound
  55.                 if (itemStackIn.hasTagCompound())
  56.                 {
  57.                     // Check if the "passive" key is true
  58.                     if (itemStackIn.getTagCompound().getBoolean("activated"))
  59.                     {
  60.                         // Check if the player is in water
  61.                         if (entityIn.isInWater())
  62.                         {
  63.                             // Set the items tagCompound's key "buffed" to true
  64.                             itemStackIn.getTagCompound().setBoolean("buffed", true);
  65.  
  66.                             // Set the items tagCompound's key "activated" to false
  67.                             itemStackIn.getTagCompound().setBoolean("activated", false);
  68.  
  69.                             EntityPlayer player = ((EntityPlayer) entityIn);
  70.  
  71.                             // Add Effects to the player
  72.                             player.addPotionEffect(new PotionEffect(Potion.waterBreathing.id, Integer.MAX_VALUE, 0));
  73.                             player.addPotionEffect(new PotionEffect(Potion.damageBoost.id, Integer.MAX_VALUE, 0));
  74.                             player.addPotionEffect(new PotionEffect(Potion.nightVision.id, Integer.MAX_VALUE, 0));
  75.                         }
  76.                     }
  77.  
  78.                     // Check if the "buffed" key is true
  79.                     if (itemStackIn.getTagCompound().getBoolean("buffed"))
  80.                     {
  81.                         if (!entityIn.isInWater())
  82.                         {
  83.                             EntityPlayer player = ((EntityPlayer) entityIn);
  84.  
  85.                             // Remove Effects from the player
  86.                             player.removePotionEffect(Potion.waterBreathing.getId());
  87.                             player.removePotionEffect(Potion.damageBoost.getId());
  88.                             player.removePotionEffect(Potion.nightVision.getId());
  89.  
  90.                             // Set the "buffed" key to false
  91.                             itemStackIn.getTagCompound().setBoolean("buffed", false);
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.     }
  98.  
  99.     @Override
  100.     public boolean hasEffect(ItemStack itemStackIn)
  101.     {
  102.         // Check if the item has a tagCompound
  103.         if (itemStackIn.hasTagCompound())
  104.         {
  105.             // Check if there is a "activated" or a "buffed" key
  106.             if (itemStackIn.getTagCompound().hasKey("activated") || itemStackIn.getTagCompound().hasKey("buffed"))
  107.             {
  108.                 // Return true whether "activated" or "buffed" is true
  109.                 return itemStackIn.getTagCompound().getBoolean("activated") || itemStackIn.getTagCompound().getBoolean("buffed");
  110.             }
  111.         }
  112.  
  113.         return false;
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement