Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.demoxin.minecraft.moreenchants;
  2.  
  3. import net.minecraft.entity.Entity;
  4. import net.minecraft.entity.EntityLivingBase;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.item.ItemSword;
  8. import net.minecraft.nbt.NBTTagCompound;
  9. import net.minecraft.potion.Potion;
  10. import net.minecraft.potion.PotionEffect;
  11. import net.minecraft.world.World;
  12.  
  13. public class Item_MagicSword extends ItemSword
  14. {
  15.     public ItemMagicSword(ToolMaterial p_i45356_1_) {
  16.         super(p_i45356_1_);
  17.     }
  18.    
  19.     public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
  20.     {
  21.         // Make sure we're not going to get any NPEs by working on a tag compound that doesn't exist
  22.         if(!par1ItemStack.hasTagCompound())
  23.         {
  24.             // Create the tag compound
  25.             par1ItemStack.setTagCompound(new NBTTagCompound());
  26.         }
  27.        
  28.         // If we don't already have the tag set, we need to create it. Our tag name is katanaInvis
  29.         if(!par1ItemStack.getTagCompound().hasKey("katanaInvis"))
  30.         {
  31.             // Create the initial tag.
  32.             par1ItemStack.getTagCompound().setBoolean("katanaInvis", true);
  33.         }
  34.         else
  35.         {
  36.             // Toggle the true/false value of the tag.
  37.             par1ItemStack.getTagCompound().setBoolean("katanaInvis", !par1ItemStack.getTagCompound().getBoolean("katanaInvis"));
  38.         }
  39.        
  40.         // Return the sword to reflect the changes
  41.         return par1ItemStack;
  42.     }
  43.    
  44.     public void onUpdate(ItemStack par1ItemStack, World par2World, Entity par3Entity, int par4, boolean par5)
  45.     {
  46.         // Make sure we're gonna be able to cast safely.
  47.         if(!(par3Entity instanceof EntityLivingBase))
  48.             return;
  49.        
  50.         // Cast the basic Entity to a more specific EntityLivingBase, used for players and mobs
  51.         EntityLivingBase user = (EntityLivingBase)par3Entity;
  52.        
  53.         // Make sure the ELB is holding an item.
  54.         if(user.getHeldItem() == null)
  55.             return;
  56.        
  57.         // Make sure the item they're holding is this one.
  58.         if(user.getHeldItem().getItem() != this)
  59.             return;
  60.        
  61.         // If there's no tag compound, we can assume they haven't ever toggled invisibility on, so no need to test any further.
  62.         if(!par1ItemStack.hasTagCompound())
  63.             return;
  64.        
  65.         // Make sure the key is valid to avoid NPEs, then check for that value.
  66.         if(par1ItemStack.getTagCompound().hasKey("katanaInvis") && par1ItemStack.getTagCompound().getBoolean("katanaInvis") == true)
  67.         {
  68.             // The katanaInvis tag is true, add the effect for 1 second.
  69.             user.addPotionEffect(new PotionEffect(Potion.invisibility.id, 20, 1));
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement