- 173
- 6
- 22
Создал блок(сейф) с инвентарным TileEntity, но при попытке открытия этого чуда ничего не происходит.
По сути весь код я собирал по кусочкам из гугла т.к. не смог найти понятный гайд по блокам с инвентарём.
Не бейте тапками пожалуйста
BlockSafe:
public class BlockSafe extends Block {
public BlockSafe() {
super(Material.iron);
this.setBlockName("BlockSafe");
this.setCreativeTab(CreativeTabs.tabBlock);
this.setHardness(60F);
this.setResistance(500F);
this.setHarvestLevel("pickaxe", 2);
this.setLightLevel(160F);
this.setBlockTextureName("firstmod:"+this.getUnlocalizedName());
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float clickX,
float clickY, float clickZ) {
Block block = world.getBlock(x, y, z);
TileEntity entity = world.getTileEntity(x, y, z);
System.out.println();
if (block != null && entity instanceof SafeTileEntity && player != null) {
if (!player.isSneaking()) {
player.openGui(CustomModMain.instance, 0, world, x, y, z);
return true;
}
}
return false;
}
}
ContainerSafe:
public class ContainerSafe extends Container {
private SafeTileEntity tile;
public ContainerSafe(InventoryPlayer player, SafeTileEntity testTile) {
tile = testTile;
int i = 0;
addSlotToContainer(new Slot(testTile, i++, 8, 38));
addSlotToContainer(new Slot(testTile, i++, 44, 21));
for (i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
this.addSlotToContainer(new Slot(player, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
}
}
for (i = 0; i < 9; ++i) {
this.addSlotToContainer(new Slot(player, i, 8 + i * 18, 142));
}
}
@Override
public boolean canInteractWith(EntityPlayer player) {
return tile.isUseableByPlayer(player);
}
}
SafeTileEntity:
public class SafeTileEntity extends TileEntityChest {
private String customName;
private int cachedChestType;
private ItemStack[] chestContents = new ItemStack[36];
/**
* Contains the chest tile located adjacent to this one (if any)
*/
public SafeTileEntity adjacentChestZNeg;
/**
* Contains the chest tile located adjacent to this one (if any)
*/
public SafeTileEntity adjacentChestXPos;
/**
* Contains the chest tile located adjacent to this one (if any)
*/
public SafeTileEntity adjacentChestXNeg;
/**
* Contains the chest tile located adjacent to this one (if any)
*/
public SafeTileEntity adjacentChestZPos;
/**
* Returns the name of the inventory
*/
public String getInventoryName() {
return this.hasCustomInventoryName() ? this.customName : "Tut Chest";
}
/**
* Returns if the inventory is named
*/
public boolean hasCustomInventoryName() {
return this.customName != null && this.customName.length() > 0;
}
public void func_145976_a(String p_145976_1_) {
this.customName = p_145976_1_;
}
public void readFromNBT(NBTTagCompound p_145839_1_) {
super.readFromNBT(p_145839_1_);
NBTTagList nbttaglist = p_145839_1_.getTagList("Items", 10);
this.chestContents = new ItemStack[this.getSizeInventory()];
if (p_145839_1_.hasKey("Safe", 8)) {
this.customName = p_145839_1_.getString("afe");
}
for (int i = 0; i < nbttaglist.tagCount(); ++i) {
NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
int j = nbttagcompound1.getByte("Slot") & 255;
if (j >= 0 && j < this.chestContents.length) {
this.chestContents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
}
}
}
public void writeToNBT(NBTTagCompound p_145841_1_) {
super.writeToNBT(p_145841_1_);
NBTTagList nbttaglist = new NBTTagList();
for (int i = 0; i < this.chestContents.length; ++i) {
if (this.chestContents[i] != null) {
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound1.setByte("Slot", (byte) i);
this.chestContents[i].writeToNBT(nbttagcompound1);
nbttaglist.appendTag(nbttagcompound1);
}
}
p_145841_1_.setTag("Items", nbttaglist);
if (this.hasCustomInventoryName()) {
p_145841_1_.setString("Safe", this.customName);
}
}
private void func_145978_a(SafeTileEntity p_145978_1_, int p_145978_2_) {
if (p_145978_1_.isInvalid()) {
this.adjacentChestChecked = false;
} else if (this.adjacentChestChecked) {
switch (p_145978_2_) {
case 0:
if (this.adjacentChestZPos != p_145978_1_) {
this.adjacentChestChecked = false;
}
break;
case 1:
if (this.adjacentChestXNeg != p_145978_1_) {
this.adjacentChestChecked = false;
}
break;
case 2:
if (this.adjacentChestZNeg != p_145978_1_) {
this.adjacentChestChecked = false;
}
break;
case 3:
if (this.adjacentChestXPos != p_145978_1_) {
this.adjacentChestChecked = false;
}
}
}
}
}
GuiSafe:
public class GuiSafe extends GuiContainer {
/** Текстура GUI */
private static final ResourceLocation textures = new ResourceLocation(
MODID, "textures/gui/BlockSafeGui.png");
private SafeTileEntity tile;
public GuiSafe(InventoryPlayer player, SafeTileEntity tileTest) {
super(new ContainerSafe(player, tileTest));
tile = tileTest;
}
@Override
protected void drawGuiContainerForegroundLayer(int x, int y) {fontRendererObj.drawString(I18n.format("container.inventory", new Object[0]), 8, ySize - 96 + 2,
4210752);
String s = tile.hasCustomInventoryName() ? tile.getInventoryName()
: I18n.format(tile.getInventoryName(), new Object[0]);
fontRendererObj.drawString(s, 6, 6, 4210752);
}
@Override
public void drawGuiContainerBackgroundLayer(float size, int x, int y) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
mc.getTextureManager().bindTexture(textures);
}
}
GuiSafeHandler:
public class GuiSafeHandler implements IGuiHandler {
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
TileEntity tile = world.getTileEntity(x, y, z);
if (ID == 0 && tile instanceof SafeTileEntity)
return new ContainerSafe(player.inventory, (SafeTileEntity) tile);
return null;
}
@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
TileEntity tile = world.getTileEntity(x, y, z);
if (ID == 0 && tile instanceof SafeTileEntity)
return new GuiSafe(player.inventory, (SafeTileEntity) tile);
return null;
}
}
По сути весь код я собирал по кусочкам из гугла т.к. не смог найти понятный гайд по блокам с инвентарём.
Не бейте тапками пожалуйста