Advertisement
HalestormXV

Untitled

Mar 23rd, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. package halestormxv.objects.items;
  2.  
  3. import halestormxv.RunicSorcery;
  4. import halestormxv.init.ItemInit;
  5. import halestormxv.utility.Reference;
  6. import halestormxv.utility.interfaces.IHasModel;
  7. import net.minecraft.client.util.ITooltipFlag;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.inventory.ItemStackHelper;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.item.ItemSword;
  12. import net.minecraft.nbt.NBTBase;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.util.*;
  15. import net.minecraft.world.World;
  16. import net.minecraftforge.common.capabilities.Capability;
  17. import net.minecraftforge.common.capabilities.ICapabilityProvider;
  18. import net.minecraftforge.common.util.INBTSerializable;
  19. import net.minecraftforge.items.CapabilityItemHandler;
  20. import net.minecraftforge.items.IItemHandler;
  21. import net.minecraftforge.items.ItemStackHandler;
  22.  
  23. import javax.annotation.Nonnull;
  24. import javax.annotation.Nullable;
  25. import java.util.List;
  26.  
  27.  
  28. public class ItemRuneBlade extends ItemSword implements IHasModel
  29. {
  30.     public ItemRuneBlade(String name, ToolMaterial material)
  31.     {
  32.         super(material);
  33.         setUnlocalizedName(name);
  34.         setRegistryName(name);
  35.         setCreativeTab(RunicSorcery.RUNICSORCERY_SPECIAL);
  36.         setMaxStackSize(1);
  37.         ItemInit.ITEMS.add(this);
  38.     }
  39.  
  40.     @Override
  41.     public String getItemStackDisplayName(ItemStack stack)
  42.     {
  43.         String colorName = super.getItemStackDisplayName(stack);
  44.         return "\u00A75" + colorName;
  45.     }
  46.  
  47.     @Override
  48.     public void registerModels()
  49.     {
  50.         RunicSorcery.proxy.registerItemRenderer(this, 0 , "inventory");
  51.     }
  52.  
  53.     @Nonnull
  54.     @Override
  55.     public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand)
  56.     {
  57.         ItemStack stackToSaveTo = player.getHeldItem(hand);
  58.         NBTTagCompound nbt = new NBTTagCompound();
  59.         if (!world.isRemote && player.isSneaking()) { player.openGui(RunicSorcery.instance, Reference.GUI_RUNE_BLADE, world, hand.ordinal(), -1, -1); }
  60.         stackToSaveTo.deserializeNBT(nbt);
  61.  
  62.         return ActionResult.newResult(EnumActionResult.SUCCESS, player.getHeldItem(hand));
  63.     }
  64.  
  65.     @Override
  66.     public ICapabilityProvider initCapabilities(ItemStack item, NBTTagCompound nbt)
  67.     {
  68.         if(item.getItem() instanceof ItemRuneBlade) { return new RuneSlotHandler(); }
  69.         return null;
  70.     }
  71.  
  72.     @Override
  73.     public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn)
  74.     {
  75.         tooltip.add("");
  76.         tooltip.add("\u00A7a" + "A person’s strength in this world is just an illusion.");
  77.         tooltip.add("\u00A7a" + "I’d rather stay the way I am until the last moment.");
  78.         tooltip.add("\u00A7a" + "The world is beautiful, even when it’s filled");
  79.         tooltip.add("\u00A7a" + "with sadness and tears.");
  80.     }
  81.  
  82.     /**Capability Handler for ItemStack
  83.      * This is the handler to take care of storing
  84.      * the different types of Runes within the Item.
  85.      */
  86.     public static class RuneSlotHandler implements ICapabilityProvider, INBTSerializable<NBTTagCompound>
  87.     {
  88.         private final ItemStackHandler handler;
  89.  
  90.         private RuneSlotHandler() { handler = new ItemStackHandler( 4 ); }
  91.  
  92.         @Override
  93.         //Retrieve data here
  94.         public NBTTagCompound serializeNBT()
  95.         {
  96.             NBTTagCompound ret = new NBTTagCompound();
  97.             NBTBase inv = CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.getStorage().writeNBT(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, handler, null);
  98.             ret.setTag("RunesStored", inv);
  99.             return ret;
  100.         }
  101.  
  102.         @Override
  103.         public void deserializeNBT(NBTTagCompound nbt)
  104.         {
  105.             //Do your saving here
  106.             if (nbt.hasKey("RunesStored"))
  107.             {
  108.                 CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.getStorage().readNBT(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, handler, null, nbt.getTag("RunesStored"));
  109.             }
  110.         }
  111.  
  112.         @Override
  113.         public boolean hasCapability(Capability<?> capability, EnumFacing facing)
  114.         {
  115.             return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;
  116.         }
  117.  
  118.         @Override
  119.         public <T> T getCapability(Capability<T> capability, EnumFacing facing)
  120.         {
  121.             if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
  122.             {
  123.                 return (T) handler;
  124.             }
  125.             return null;
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement