Баг с Колчаном (1.12.2)

Версия Minecraft
1.12.2
API
MCreator
Необходимо предотвратить перетаскивание или выбрасывание колчана из слота, из которого открывался инвентарь с определенной руки.
Но я не знаю, как заблокировать конкретный слот


Я использую MCreator версии: 2020.2 для Minecraft 1.12.2.

package net.mcreator.forgetestdontmod.item;

import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.event.ModelRegistryEvent;

import net.minecraft.world.World;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Item;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.util.ActionResult;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumActionResult;
import net.minecraft.nbt.NBTBase;

import net.mcreator.forgetestdontmod.ElementsForgeTestDONTMOD;

import javax.annotation.Nullable;

import java.util.List;

@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;
}
}
}
 
Последнее редактирование:
Сверху