Java Minecraft Forge-集装箱槽偏移?
我正在创建一个具有容器的新mod。问题是库存插槽被7抵消。服务器似乎认为第一列是倒数第二列的位置,第二列是倒数第二列的位置。下面是一些代码。课程应该包括在内Java Minecraft Forge-集装箱槽偏移?,java,minecraft-forge,Java,Minecraft Forge,我正在创建一个具有容器的新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
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
公共类ContainerGenerator扩展容器{
私人tileEntity发电机tileEntity;
私人整数计数;
公共容器生成器(库存播放器库存,TileEntityGenerator tileEntity){
this.tileEntity=tileEntity;
this.tileEntity.openInventory();
this.count=tileEntity.getCount();
这个.addSlotToContainer(新插槽(tileEntity,0,27,47));
这个.addSlotToContainer(新插槽(tileEntity,1,134,47));
对于(int i=0;i<3;i++){
对于(int j=0;j<9;j++){
这个.addSlotToContainer(新插槽(库存,j+i*9+9,8+j*18,84+i*18));
}
}
对于(int i=0;i<9;i++){
这个.addSlotToContainer(新插槽(库存,i,8+i*18142));
}
}
//其他一些随机码
和一些相关的平铺实体覆盖(还有一些其他随机代码)
@覆盖
nbt(NBTTAGcompund nbt)的公共无效读取{
超级。从nbt读取(nbt);
this.count=nbt.getInteger(“count”);
NBTTagList tagList=nbt.getTagList(“项”,10);
this.items=newitemstack[2];
NBTTagCompound compound=tagList.getCompoundTagAt(0);
字节slotId=composite.getByte(“插槽”);
如果(slotId>=0&&slotId<2){
items[slotId]=ItemStack.loadItemStackFromNBT(复合);
}
复合=标记列表。getCompoundTagAt(1);
slotId=composite.getByte(“slot”);
如果(slotId>=0&&slotId<2){
items[slotId]=ItemStack.loadItemStackFromNBT(复合);
}
}
@凌驾
公共无效书面通知(NBTTAGcompund nbt){
超级写nbt(nbt);
nbt.setInteger(“计数”,计数);
NBTTagList list=新的NBTTagList();
如果(项目[0]!=null){
NBTTagCompound化合物=新的NBTTagCompound();
复合.setByte(“slot”,(byte)0);
项目[0]。已写入(复合);
列表.附加标签(化合物);
}
如果(项目[1]!=null){
NBTTagCompound化合物=新的NBTTagCompound();
复合.setByte(“插槽”,(字节)1);
第[1]项。书面报告(复合);
列表.附加标签(化合物);
}
nbt.setTag(“项目”,列表);
}
@凌驾
public int getSizeInventory(){
返回项目。长度;
}
@凌驾
公共项堆栈getStackInSlot(int插槽){
归还物品[槽];
}
@凌驾
public ItemStack decrackstacksize(int slot,int amount){
if(项目[插槽]==null){
返回null;
}
如果(项目[插槽].stackSize==金额){
ItemStack ItemStack=项目[插槽];
项目[槽]=空;
返回itemStack;
}否则{
ItemStack ItemStack=items[slot].splitStack(金额);
如果(项[slot].stackSize==0){
项目[槽]=空;
}
markDirty();
返回itemStack;
}
}
@凌驾
公共项堆栈getStackInSlotOnClosing(int槽){
如果(项目[插槽]!=null){
ItemStack ItemStack=项目[插槽];
项目[槽]=空;
返回itemStack;
}否则{
返回null;
}
}
@凌驾
public void setInventorySlotContents(int槽、ItemStack堆栈){
项目[槽]=堆栈;
if(stack!=null&&stack.stackSize>getInventoryStackLimit()){
markDirty();
}
}
编辑:屏幕截图。注意突出显示的插槽(如果有)
抱歉,这篇文章太长了。我不太熟悉modding,所以很难准确指出问题所在。是的,Tile Entity类扩展了TileEntity并实现了inventory。你能提供一个屏幕截图,说明服务器认为事情不同吗?还有其他一些小的奇怪行为,但我没有一个好的录制软件是的,所以我决定指出这个特殊的问题。所以,你把它放在插槽1,然后放在插槽7?嗯,现在在你对nbt的写入和从nbt的读取中,你能做一个log.info(复合)吗?这样你就可以看到nbt数据何时被调用,何时被写入以及如何写入。我会在接下来的几天内写回,当我可以测试它的时候。是的,Tile Entity类扩展了TileEntity并实现了IInventory。你能提供一个屏幕截图,说明服务器认为情况有所不同吗?还有其他一些小问题aviors,但我没有一个好的录音软件,所以我决定指出这个特殊的问题。所以,你把它放在插槽1,然后放在插槽7?嗯,现在在你写nbt和读nbt时,你能做一个log.info(复合)吗?这样您就可以看到何时调用nbt数据,何时写入以及如何写入。我将在接下来的几天内写回,届时我可以测试它。
@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();
}
}