SHARE
TWEET

ModContainerItem.java

a guest Jan 18th, 2020 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.minetronic.masterchef.items;
  2.  
  3. import net.minecraft.entity.LivingEntity;
  4. import net.minecraft.entity.player.PlayerEntity;
  5. import net.minecraft.item.Item;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.world.World;
  8.  
  9. /**
  10.  * The containerItem is an item which returns a specified Item after it has been used
  11.  */
  12. public class ModContainerItem extends ModItem {
  13.  
  14.     private Item containerItem;
  15.  
  16.     public ModContainerItem(Item.Properties properties, Item containerItem) {
  17.         super(properties.containerItem(containerItem));
  18.         this.containerItem = containerItem;
  19.     }
  20.  
  21.     /**
  22.      * Called when the player finishes using this Item (E.g. finishes eating.). Not called when the player stops using
  23.      * the Item before the action is complete.
  24.      */
  25.     public ItemStack onItemUseFinish(ItemStack stack, World worldIn, LivingEntity entityLiving) {
  26.         ItemStack itemstack = super.onItemUseFinish(stack, worldIn, entityLiving);
  27.         if (entityLiving instanceof PlayerEntity) {
  28.             PlayerEntity player = ((PlayerEntity) entityLiving);
  29.             if (!player.abilities.isCreativeMode) {
  30.                 ItemStack containerItemStack = new ItemStack(containerItem);
  31.                 if (itemstack.isEmpty()) {
  32.                     System.out.println("Has itemstack: " + player.inventory.hasItemStack(containerItemStack));
  33.                     System.out.println("Stackable: " + containerItemStack.isStackable());
  34.                     if (containerItemStack.isStackable() && player.inventory.hasItemStack(containerItemStack)) {
  35.                         if (!player.addItemStackToInventory(containerItemStack)) {
  36.                             return containerItemStack;
  37.                         }
  38.                     } else {
  39.                         return containerItemStack;
  40.                     }
  41.                 } else {
  42.                     player.addItemStackToInventory(new ItemStack(containerItem));
  43.                     return itemstack;
  44.                 }
  45.             }
  46.         }
  47.         return itemstack;
  48.     }
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top