GUIを閉じるとメタデータが0になる

Modding・サーバPlugin制作・ツール制作など、開発関連の質問があればこちらにお願い致します。
フォーラムルール
質問関連フォーラムで質問する時は、必ず次のトピックを一読/厳守お願い致します。
viewtopic.php?f=5&t=999
  • (PostNo.318350)

GUIを閉じるとメタデータが0になる

投稿記事by stocheche » 2018年7月26日(木) 20:40

minecraft1.7.10で、アイテムとしてチェストを追加しました。
それで作った後にメタデータを使って色を付けて、見た目をわかりやすいようにしたのですが、
GUIを開いて閉じると、メタデータが0になってしまいます
どこがおかしいのか伝授お願いします

アイテム:
package com.testMod.items;
import java.util.List;
import com.testMod.testMod;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

public class ToolHandy_chest extends Item{

private IIcon[] iicon = new IIcon[16];

public ToolHandy_chest(){
super();
this.setCreativeTab(CreativeTabs.tabTools);
this.setUnlocalizedName("handy_chest");
this.setTextureName("ogamod:handy_chest");
this.setMaxStackSize(1);
}
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player){
player.openGui(testMod.instance, 2, world, (int)player.posX, (int)player.posY, (int)player.posZ);
return itemStack;
}
@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister iicon) {
for (int i = 0; i < 16; i ++) {
this.iicon[i] = iicon.registerIcon(this.getIconString()+ i);
}
}
@Override
@SideOnly(Side.CLIENT)
public IIcon getIconFromDamage(int meta) {
return iicon[meta];
}
@Override
@SideOnly(Side.CLIENT)
public void getSubItems(Item item, CreativeTabs creativeTab, List list) {
for (int i = 0; i < 16; i ++) {
list.add(new ItemStack(this, 1, i));
}
}
@Override
public int getMetadata(int meta) {
return meta;
}
@Override
public String getUnlocalizedName(ItemStack itemStack) {
return super.getUnlocalizedName() + "." + itemStack.getItemDamage();
}
}


インベントリ:
package com.testMod.TileEntity;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;

public class InventoryHandy_Chest implements IInventory
{
private InventoryPlayer inventoryPlayer;
private ItemStack currentItem;
private ItemStack[] items;

public InventoryHandy_Chest(InventoryPlayer inventory)
{
inventoryPlayer = inventory;
currentItem = inventoryPlayer.getCurrentItem();

//InventorySize
items = new ItemStack[54];
}

@Override
public int getSizeInventory()
{
return items.length;
}

@Override
public ItemStack getStackInSlot(int slot)
{
return items[slot];
}

@Override
public ItemStack decrStackSize(int p_70298_1_, int p_70298_2_)
{
if (this.items[p_70298_1_] != null)
{
ItemStack itemstack;

if (this.items[p_70298_1_].stackSize <= p_70298_2_)
{
itemstack = this.items[p_70298_1_];
this.items[p_70298_1_] = null;
this.markDirty();
return itemstack;
}
else
{
itemstack = this.items[p_70298_1_].splitStack(p_70298_2_);

if (this.items[p_70298_1_].stackSize == 0)
{
this.items[p_70298_1_] = null;
}

this.markDirty();
return itemstack;
}
}
return null;
}

@Override
public ItemStack getStackInSlotOnClosing(int p_70304_1_)
{
if (this.items[p_70304_1_] != null)
{
ItemStack itemstack = this.items[p_70304_1_];
this.items[p_70304_1_] = null;
return itemstack;
}
return null;
}

@Override
public void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_)
{
this.items[p_70299_1_] = p_70299_2_;

if (p_70299_2_ != null && p_70299_2_.stackSize > this.getInventoryStackLimit())
{
p_70299_2_.stackSize = this.getInventoryStackLimit();
}

this.markDirty();
}

@Override
public String getInventoryName() {
return "container.testMod.HandyChest";
}

@Override
public boolean hasCustomInventoryName()
{
return false;
}

@Override
public int getInventoryStackLimit()
{
return 64;
}

@Override
public void markDirty() {}

@Override
public boolean isUseableByPlayer(EntityPlayer p_70300_1_)
{
return true;
}

/*
Containerが開かれたタイミングでItemStackの持っているNBTからアイテムを読み込んでいる
*/
@Override
public void openInventory()
{
if(!currentItem.hasTagCompound())
{
currentItem.setTagCompound(new NBTTagCompound());
currentItem.getTagCompound().setTag("Items", new NBTTagList());
}
NBTTagList tags = (NBTTagList)currentItem.getTagCompound().getTag("Items");

for(int i = 0; i < tags.tagCount(); i++)
{
NBTTagCompound tagCompound = tags.getCompoundTagAt(i);
int slot = tagCompound.getByte("Slot");
if(slot >= 0 && slot < items.length)
{
items[slot] = ItemStack.loadItemStackFromNBT(tagCompound);
}
}
}

/*
Containerを閉じるときに保存
*/
@Override
public void closeInventory()
{
NBTTagList tagList = new NBTTagList();
for(int i = 0; i < items.length; i++)
{
if(items[i] != null)
{
NBTTagCompound compound = new NBTTagCompound();
compound.setByte("Slot", (byte)i);
items[i].writeToNBT(compound);
tagList.appendTag(compound);
}
}
ItemStack result = new ItemStack(currentItem.getItem(), 1);
result.setTagCompound(new NBTTagCompound());
result.getTagCompound().setTag("Items", tagList);

//ItemStackをセットする。NBTは右クリック等のタイミングでしか保存されないため再セットで保存している。
inventoryPlayer.mainInventory[inventoryPlayer.currentItem] = result;
}

@Override
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_)
{
return true;
}


コンテナ:
package com.testMod.Inventory;

import com.testMod.TileEntity.InventoryHandy_Chest;
import com.testMod.items.testmodItems;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class ContainerHandy_chest extends Container
{
private InventoryHandy_Chest inventory;

public ContainerHandy_chest(InventoryPlayer inventoryPlayer)
{
inventory = new InventoryHandy_Chest(inventoryPlayer);
inventory.openInventory();

int i = 2 * 18 + 1;

for (int j = 0; j < 6; ++j)
{
for (int k = 0; k < 9; ++k)
{
this.addSlotToContainer(new Slot(inventory, k + j * 9, 8 + k * 18, 18 + j * 18));
}
}

for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 9; ++k)
{
this.addSlotToContainer(new SlotHandy_chest(inventoryPlayer, k + j * 9 + 9, 8 + k * 18, 103 + j * 18 + i));
}
}

for (int j = 0; j < 9; ++j)
{
this.addSlotToContainer(new SlotHandy_chest(inventoryPlayer, j, 8 + j * 18, 161 + i));
}

}

/*
Containerが開いてられるか
*/
@Override
public boolean canInteractWith(EntityPlayer p_75145_1_)
{
return true;
}

@Override
public ItemStack transferStackInSlot(EntityPlayer p_82846_1_, int p_82846_2_)
{
ItemStack itemstack = null;
Slot slot = (Slot)this.inventorySlots.get(p_82846_2_);

if (slot != null && slot.getHasStack())
{
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();


if (p_82846_2_ < this.inventory.getSizeInventory())
{
if (!this.mergeItemStack(itemstack1, this.inventory.getSizeInventory(), this.inventorySlots.size(), true))
{
return null;
}
}
//シフトクリック時に、このアイテムだったら動かさない。
else if(slot.getStack() != null && slot.getStack().getItem() == testmodItems.ToolHandy_chest)
{
return null;
}
else if (!this.mergeItemStack(itemstack1, 0, this.inventory.getSizeInventory(), false))
{
return null;
}
if (itemstack1.stackSize == 0)
{
slot.putStack((ItemStack)null);
}
else
{
slot.onSlotChanged();
}
}

return itemstack;
}

/*
Containerを閉じるときに呼ばれる
*/
@Override
public void onContainerClosed(EntityPlayer p_75134_1_)
{
super.onContainerClosed(p_75134_1_);
this.inventory.closeInventory();
}
}


gui:
package com.testMod.Inventory;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;

import org.lwjgl.opengl.GL11;

import com.testMod.TileEntity.InventoryHandy_Chest;

public class GuiHandy_chest extends GuiContainer{

private InventoryHandy_Chest inventory;

private static final ResourceLocation texture = new ResourceLocation("textures/gui/container/generic_54.png");

public GuiHandy_chest(InventoryPlayer inventoryPlayer)
{
super(new ContainerHandy_chest(inventoryPlayer));
this.ySize = 222;
}

/*
ChestとかInventoryとか文字を描画する
*/
@Override
protected void drawGuiContainerForegroundLayer(int x, int y)
{
//描画する文字, X, Y, 色
this.fontRendererObj.drawString(StatCollector.translateToLocal("container.OgaMod.HandyChest"), 8, 6, 4210752);
this.fontRendererObj.drawString(StatCollector.translateToLocal("container.inventory"), 8, this.ySize - 96 + 2, 4210752);
}


/*
背景の描画
*/
@Override
protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_)
{
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(texture);
int k = (this.width - this.xSize) / 2;
int l = (this.height - this.ySize) / 2;
this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
}
}
stocheche
ID:da05adc8
 

Return to 質問:開発・制作関連

x