Quiver Bug

Started by Anakin_Guy on

Topic category: Help with Minecraft modding (Java Edition)

Joined Dec 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Quiver Bug
Sun, 06/09/2024 - 22:00 (edited)

It is necessary to prevent the quiver from being dragged from certain slot from which opened the inventory, I need to lock a specific slot with a given quiver from hotbar in player inventory, in which the player open quiver inventory, only the slot in which the player interacts with the quiver should be locked, and when the quiver inventory is closed, the locking from specific slot with quiver is cancel

 

I'm use MCreator Version: 2020.2 for Minecraft 1.12.2

@ElementsForgeTestDONTMOD.ModElement.Tag
public class ItemQuiver extends ElementsForgeTestDONTMOD.ModElement {
    @GameRegistry.ObjectHolder("forgetestdontmod:quiver")
    public static final Item block = null;

    public ItemQuiver(ElementsForgeTestDONTMOD instance) {
        super(instance, 191);
    }

    @Override
    public void initElements() {
        elements.items.add(() -> new ItemCustom());
    }

    @SideOnly(Side.CLIENT)
    @Override
    public void registerModels(ModelRegistryEvent event) {
        ModelLoader.setCustomModelResourceLocation(block, 0, new ModelResourceLocation("forgetestdontmod:quiver", "inventory"));
    }

    public static class ItemCustom extends Item {
        public static final String TAG_INVENTORY = "quiver_inventory";

        public ItemCustom() {
            setMaxDamage(0);
            maxStackSize = 1;
            setUnlocalizedName("quiver");
            setRegistryName("quiver");
            setCreativeTab(CreativeTabs.COMBAT);
        }

        public InventoryQuiver getInventory(ItemStack itemStack) {
            if (itemStack.hasTagCompound()) {
                NBTTagCompound nbt = itemStack.getTagCompound();
                if (itemStack.hasTagCompound() && itemStack.getTagCompound().hasKey(TAG_INVENTORY)) {
                    InventoryQuiver inventory = new InventoryQuiver("Quiver", false, 9);
                    return inventory;
                }
            }
            return null;
        }

        public void setInventory(ItemStack itemStack, InventoryQuiver inventory) {
            NBTTagCompound nbt = itemStack.getTagCompound();
            if (nbt == null) {
                nbt = new NBTTagCompound();
                itemStack.setTagCompound(nbt);
            }
            NBTTagCompound inventoryNBT = new NBTTagCompound();
            NBTTagList tagList = new NBTTagList();
            inventory.writeToNBT(tagList);
            inventoryNBT.setTag("Items", tagList);
            nbt.setTag(TAG_INVENTORY, inventoryNBT);
        }

        @Override
        public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
            ItemStack itemStack = playerIn.getHeldItem(handIn);
            if (!worldIn.isRemote) {
                InventoryQuiver inventory = getInventory(itemStack);
                if (inventory == null) {
                    inventory = new InventoryQuiver("Quiver", false, 9);
                    setInventory(itemStack, inventory);
                }
                inventory.setInventoryItemStack(itemStack);

                playerIn.displayGUIChest(inventory);
            }
            return new ActionResult<>(EnumActionResult.SUCCESS, itemStack);
        }

        @Override
        public int getItemEnchantability() {
            return 0;
        }

        @Override
        public int getMaxItemUseDuration(ItemStack itemstack) {
            return 0;
        }

        @Override
        public float getDestroySpeed(ItemStack par1ItemStack, IBlockState par2Block) {
            return 1F;
        }

        @Override
        public void addInformation(ItemStack itemstack, World world, List<String> list, ITooltipFlag flag) {
            super.addInformation(itemstack, world, list, flag);
            list.add("Right click to open inventory");
        }
    }

    public static class InventoryQuiver extends InventoryBasic {
      private ItemStack inventoryItemStack;
      public static final String TAG_INVENTORY = "quiver_inventory";

      public InventoryQuiver(String title, boolean customName, int slotCount) {
          super(title, customName, slotCount);
      }

      public void setInventoryItemStack(ItemStack itemStack) {
          inventoryItemStack = itemStack;
      }

      public ItemStack getInventoryItemStack() {
          return inventoryItemStack;
      }

      @Override
      public void openInventory(EntityPlayer player) {
          if (inventoryItemStack != null && !inventoryItemStack.isEmpty()) {
              readFromNBT(inventoryItemStack.getTagCompound().getTagList(TAG_INVENTORY, 10));
          }
          super.openInventory(player);
      }

      @Override
      public void closeInventory(EntityPlayer player) {
          if (inventoryItemStack != null && !inventoryItemStack.isEmpty()) {
              NBTTagCompound itemStackNBT = inventoryItemStack.getTagCompound();
              if (itemStackNBT == null) {
                  itemStackNBT = new NBTTagCompound();
              }
              itemStackNBT.setTag(TAG_INVENTORY, writeToNBT(new NBTTagList()));
              inventoryItemStack.setTagCompound(itemStackNBT);
          }
          super.closeInventory(player);
      }

      @Override
      public void markDirty() {
          super.markDirty();
          if (inventoryItemStack != null && !inventoryItemStack.isEmpty()) {
              NBTTagCompound itemStackNBT = inventoryItemStack.getTagCompound();
              if (itemStackNBT != null) {
                  itemStackNBT.setTag(TAG_INVENTORY, writeToNBT(new NBTTagList()));
                  inventoryItemStack.setTagCompound(itemStackNBT);
              }
          }
      }

      public void readFromNBT(NBTTagList itemList) {
          for (int i = 0; i < itemList.tagCount(); ++i) {
              NBTTagCompound itemTags = itemList.getCompoundTagAt(i);
              int slot = itemTags.getByte("Slot") & 255;

              if (slot >= 0 && slot < getSizeInventory()) {
                  ItemStack stack = new ItemStack(itemTags);
                  setInventorySlotContents(slot, stack);
              }
          }
      }

      public NBTTagList writeToNBT(NBTTagList itemList) {
          for (int i = 0; i < getSizeInventory(); ++i) {
              ItemStack stack = getStackInSlot(i);
              if (!stack.isEmpty()) {
                  NBTTagCompound itemTags = new NBTTagCompound();
                  itemTags.setByte("Slot", (byte) i);
                  stack.writeToNBT(itemTags);
                  itemList.appendTag(itemTags);
              }
          }
          return itemList;
      }
   }
}
Edited by Anakin_Guy on Sun, 06/09/2024 - 22:00
Joined Dec 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
It seems to me, or am I…
Fri, 07/19/2024 - 11:28

It seems to me, or am I trying to fix this bug before I make the location of the slots in the quiver inventory ?