我正在创建一个具有容器的新 mod。问题是库存插槽偏移了 7。似乎服务器认为第一列是倒数第二列的位置,而第二列是最后一列的位置。这是一些代码。类应该包括在内。
public class GuiHandler implements IGuiHandler {
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
if (ID == 0) {
return new ContainerGenerator(player.inventory, (TileEntityGenerator) world.getTileEntity(x, y, z));
} else {
return null;
}
}
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
if (ID == 0) {
return new GuiGenerator(player.inventory, (TileEntityGenerator) world.getTileEntity(x, y, z));
} else {
return null;
}
}
}
容器类
public class ContainerGenerator extends Container {
private TileEntityGenerator tileEntity;
private int count;
public ContainerGenerator(InventoryPlayer inventory, TileEntityGenerator tileEntity) {
this.tileEntity = tileEntity;
this.tileEntity.openInventory();
this.count = tileEntity.getCount();
this.addSlotToContainer(new Slot(tileEntity, 0, 27, 47));
this.addSlotToContainer(new Slot(tileEntity, 1, 134, 47));
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 9; j++) {
this.addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
}
}
for (int i = 0; i < 9; i++) {
this.addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 142));
}
}
// some other random code
和一些相关的瓷砖实体覆盖(还有一些其他的随机代码)
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
this.count = nbt.getInteger("count");
NBTTagList tagList = nbt.getTagList("items", 10);
this.items = new ItemStack[2];
NBTTagCompound compound = tagList.getCompoundTagAt(0);
byte slotId = compound.getByte("slot");
if (slotId >= 0 && slotId < 2) {
items[slotId] = ItemStack.loadItemStackFromNBT(compound);
}
compound = tagList.getCompoundTagAt(1);
slotId = compound.getByte("slot");
if (slotId >= 0 && slotId < 2) {
items[slotId] = ItemStack.loadItemStackFromNBT(compound);
}
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
nbt.setInteger("count", count);
NBTTagList list = new NBTTagList();
if (items[0] != null) {
NBTTagCompound compound = new NBTTagCompound();
compound.setByte("slot", (byte) 0);
items[0].writeToNBT(compound);
list.appendTag(compound);
}
if (items[1] != null) {
NBTTagCompound compound = new NBTTagCompound();
compound.setByte("slot", (byte) 1);
items[1].writeToNBT(compound);
list.appendTag(compound);
}
nbt.setTag("items", list);
}
@Override
public int getSizeInventory() {
return items.length;
}
@Override
public ItemStack getStackInSlot(int slot) {
return items[slot];
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
if (items[slot] == null) {
return null;
}
if (items[slot].stackSize == amount) {
ItemStack itemStack = items[slot];
items[slot] = null;
return itemStack;
} else {
ItemStack itemStack = items[slot].splitStack(amount);
if (items[slot].stackSize == 0) {
items[slot] = null;
}
markDirty();
return itemStack;
}
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
if (items[slot] != null) {
ItemStack itemStack = items[slot];
items[slot] = null;
return itemStack;
} else {
return null;
}
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
items[slot] = stack;
if (stack != null && stack.stackSize > getInventoryStackLimit()) {
markDirty();
}
}
编辑:截图。请注意哪些插槽被突出显示(如果有)。
很抱歉这篇冗长的帖子。我是改装新手,所以我很难准确指出问题所在。