Здравствуйте. Сделал тут блок с гуи. Вот есть 1 проблема: Если блок в слоте которого есть какие-то вещи разрушить после установки не выходя из игры, то содержимое блока вываливается и всё нормально. А если поставить блок, положить в него вещи и перезайти в игру, то при разрушении блока его содержимое не дропается, т.е. пропадает. Хотя если зайти в ГУИ блока, то вещь там есть. То есть проблема именно в дропе с вещи.
Класс блока
Класс контейнера
Класс Тайла
За не очень красивый местами код прошу простить, так как этот код тестовый, дальше он будет переписан в нормальный, когда будет рабочим
Класс блока
public class BlockTest extends BlockContainer implements ITileEntityProvider {
private final Random field_149933_a = new Random();
private boolean activeBlock = false;
private static TestMod instance;
public BlockTest(Material material, TestMod instanceMod) {
super(material);
setCreativeTab(CreativeTabs.tabBlock);
setBlockName(TestMod.MODID + "." + "cleanMachine");
setHardness(2.0F);
setResistance(10.0F);
setHarvestLevel("pickaxe", 3);
setStepSound(Block.soundTypeMetal);
setBlockTextureName("testMod:testblock");
instance = instanceMod;
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player,
int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_) {
if (!player.isSneaking()) {
player.openGui(instance, BlockTestGuiHandler.GuiID, world, x, y, z);
return true;
} else {
return false;
}
}
/* @Override
public TileEntity createTileEntity(World world, int metadata) {
return new BlockTestTile();
}*/
@Override
public Item getItemDropped(int p_149650_1_, Random p_149650_2_, int p_149650_3_) {
return Item.getItemFromBlock(TestMod.BlockTest);
}
@SideOnly(Side.CLIENT)
@Override
public void onNeighborBlockChange(World world, int x, int y, int z, Block b){
if(!world.isRemote) {
if (activeBlock && !world.isBlockIndirectlyGettingPowered(x, y, z))
{
//выключить
Minecraft.getMinecraft().thePlayer.sendChatMessage("Выключен");
activeBlock=false;
}
else if (!activeBlock && world.isBlockIndirectlyGettingPowered(x, y, z))
{
Minecraft.getMinecraft().thePlayer.sendChatMessage("Включен");
activeBlock=true;
//включить
}
}
}
@Override
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
return new BlockTestTile("TestInventory", true);
}
//Настроить выпадение содержимого блока при уничтожении
@Override
public void breakBlock(World world, int x, int y, int z, Block block, int p_149749_6_) {
BlockTestTile tileentity = (BlockTestTile) world.getTileEntity(x, y, z);
if (tileentity != null) {
ItemStack itemstack = tileentity.getStackInSlot(0);
if (itemstack != null) {
float f = this.field_149933_a.nextFloat() * 0.8F + 0.1F;
float f1 = this.field_149933_a.nextFloat() * 0.8F + 0.1F;
float f2 = this.field_149933_a.nextFloat() * 0.8F + 0.1F;
while (itemstack.stackSize > 0) {
int j1 = this.field_149933_a.nextInt(21) + 10;
if (j1 > itemstack.stackSize) {
j1 = itemstack.stackSize;
}
itemstack.stackSize -= j1;
EntityItem entityitem = new EntityItem(world, (double) ((float) x + f), (double) ((float) y + f1),
(double) ((float) z + f2), new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));
if (itemstack.hasTagCompound()) {
entityitem.getEntityItem().setTagCompound((NBTTagCompound) itemstack.getTagCompound().copy());
}
float f3 = 0.05F;
entityitem.motionX = (double) ((float) this.field_149933_a.nextGaussian() * f3);
entityitem.motionY = (double) ((float) this.field_149933_a.nextGaussian() * f3 + 0.2F);
entityitem.motionZ = (double) ((float) this.field_149933_a.nextGaussian() * f3);
world.spawnEntityInWorld(entityitem);
}
}
world.func_147453_f(x, y, z, block);
}
}
}
private final Random field_149933_a = new Random();
private boolean activeBlock = false;
private static TestMod instance;
public BlockTest(Material material, TestMod instanceMod) {
super(material);
setCreativeTab(CreativeTabs.tabBlock);
setBlockName(TestMod.MODID + "." + "cleanMachine");
setHardness(2.0F);
setResistance(10.0F);
setHarvestLevel("pickaxe", 3);
setStepSound(Block.soundTypeMetal);
setBlockTextureName("testMod:testblock");
instance = instanceMod;
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player,
int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_) {
if (!player.isSneaking()) {
player.openGui(instance, BlockTestGuiHandler.GuiID, world, x, y, z);
return true;
} else {
return false;
}
}
/* @Override
public TileEntity createTileEntity(World world, int metadata) {
return new BlockTestTile();
}*/
@Override
public Item getItemDropped(int p_149650_1_, Random p_149650_2_, int p_149650_3_) {
return Item.getItemFromBlock(TestMod.BlockTest);
}
@SideOnly(Side.CLIENT)
@Override
public void onNeighborBlockChange(World world, int x, int y, int z, Block b){
if(!world.isRemote) {
if (activeBlock && !world.isBlockIndirectlyGettingPowered(x, y, z))
{
//выключить
Minecraft.getMinecraft().thePlayer.sendChatMessage("Выключен");
activeBlock=false;
}
else if (!activeBlock && world.isBlockIndirectlyGettingPowered(x, y, z))
{
Minecraft.getMinecraft().thePlayer.sendChatMessage("Включен");
activeBlock=true;
//включить
}
}
}
@Override
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
return new BlockTestTile("TestInventory", true);
}
//Настроить выпадение содержимого блока при уничтожении
@Override
public void breakBlock(World world, int x, int y, int z, Block block, int p_149749_6_) {
BlockTestTile tileentity = (BlockTestTile) world.getTileEntity(x, y, z);
if (tileentity != null) {
ItemStack itemstack = tileentity.getStackInSlot(0);
if (itemstack != null) {
float f = this.field_149933_a.nextFloat() * 0.8F + 0.1F;
float f1 = this.field_149933_a.nextFloat() * 0.8F + 0.1F;
float f2 = this.field_149933_a.nextFloat() * 0.8F + 0.1F;
while (itemstack.stackSize > 0) {
int j1 = this.field_149933_a.nextInt(21) + 10;
if (j1 > itemstack.stackSize) {
j1 = itemstack.stackSize;
}
itemstack.stackSize -= j1;
EntityItem entityitem = new EntityItem(world, (double) ((float) x + f), (double) ((float) y + f1),
(double) ((float) z + f2), new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));
if (itemstack.hasTagCompound()) {
entityitem.getEntityItem().setTagCompound((NBTTagCompound) itemstack.getTagCompound().copy());
}
float f3 = 0.05F;
entityitem.motionX = (double) ((float) this.field_149933_a.nextGaussian() * f3);
entityitem.motionY = (double) ((float) this.field_149933_a.nextGaussian() * f3 + 0.2F);
entityitem.motionZ = (double) ((float) this.field_149933_a.nextGaussian() * f3);
world.spawnEntityInWorld(entityitem);
}
}
world.func_147453_f(x, y, z, block);
}
}
}
Класс контейнера
public class BlockTestContainer extends Container {
public BlockTestTile testTile;
public BlockTestContainer(InventoryPlayer player,BlockTestTile tile){
testTile=tile;
this.addSlotToContainer(new TestSlot(tile,0,79,40));
int i;
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));
}
}
//TO DO
@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn, int fromSlot) {
ItemStack itemstack = null;
Slot slot = (Slot)this.inventorySlots.get(fromSlot);
if (slot != null && slot.getHasStack())
{
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();
if (fromSlot == 0)
{
if (!this.mergeItemStack(itemstack1, 1, 37, true))
{
return null;
}
}
else
{
if (((Slot)this.inventorySlots.get(0)).getHasStack() || !((Slot)this.inventorySlots.get(0)).isItemValid(itemstack1))
{
return null;
}
if (itemstack1.hasTagCompound() && itemstack1.stackSize == 1)
{
((Slot)this.inventorySlots.get(0)).putStack(itemstack1.copy());
itemstack1.stackSize = 0;
}
else if (itemstack1.stackSize >= 1)
{
((Slot)this.inventorySlots.get(0)).putStack(new ItemStack(itemstack1.getItem(), 1, itemstack1.getItemDamage()));
--itemstack1.stackSize;
}
}
if (itemstack1.stackSize == 0)
{
slot.putStack((ItemStack)null);
}
else
{
slot.onSlotChanged();
}
if (itemstack1.stackSize == itemstack.stackSize)
{
return null;
}
slot.onPickupFromSlot(playerIn, itemstack1);
}
return itemstack;
}
@Override
public boolean canInteractWith(EntityPlayer player) {
return testTile.isUseableByPlayer(player);
}
}
public BlockTestTile testTile;
public BlockTestContainer(InventoryPlayer player,BlockTestTile tile){
testTile=tile;
this.addSlotToContainer(new TestSlot(tile,0,79,40));
int i;
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));
}
}
//TO DO
@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn, int fromSlot) {
ItemStack itemstack = null;
Slot slot = (Slot)this.inventorySlots.get(fromSlot);
if (slot != null && slot.getHasStack())
{
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();
if (fromSlot == 0)
{
if (!this.mergeItemStack(itemstack1, 1, 37, true))
{
return null;
}
}
else
{
if (((Slot)this.inventorySlots.get(0)).getHasStack() || !((Slot)this.inventorySlots.get(0)).isItemValid(itemstack1))
{
return null;
}
if (itemstack1.hasTagCompound() && itemstack1.stackSize == 1)
{
((Slot)this.inventorySlots.get(0)).putStack(itemstack1.copy());
itemstack1.stackSize = 0;
}
else if (itemstack1.stackSize >= 1)
{
((Slot)this.inventorySlots.get(0)).putStack(new ItemStack(itemstack1.getItem(), 1, itemstack1.getItemDamage()));
--itemstack1.stackSize;
}
}
if (itemstack1.stackSize == 0)
{
slot.putStack((ItemStack)null);
}
else
{
slot.onSlotChanged();
}
if (itemstack1.stackSize == itemstack.stackSize)
{
return null;
}
slot.onPickupFromSlot(playerIn, itemstack1);
}
return itemstack;
}
@Override
public boolean canInteractWith(EntityPlayer player) {
return testTile.isUseableByPlayer(player);
}
}
public class BlockTestTile extends TileEntity implements ISidedInventory,IPipeConnection{
private String inventoryTitle;
private int slotsCount;
private ItemStack[] inventoryContents;
private List field_70480_d;
private boolean field_94051_e;
public static final int SLOTCOUNT=1;
public BlockTestTile(String inventoryTitle, boolean p_i1561_2_)
{
if(this.inventoryContents==null) {
this.inventoryContents = new ItemStack[SLOTCOUNT];
}
this.inventoryTitle = inventoryTitle;
this.field_94051_e = p_i1561_2_;
this.slotsCount = SLOTCOUNT;
}
public BlockTestTile(){
if(this.inventoryContents==null) {
this.inventoryContents = new ItemStack[SLOTCOUNT];
}
}
public void func_110134_a(IInvBasic p_110134_1_)
{
if (this.field_70480_d == null)
{
this.field_70480_d = new ArrayList();
}
this.field_70480_d.add(p_110134_1_);
}
public void func_110132_b(IInvBasic p_110132_1_)
{
this.field_70480_d.remove(p_110132_1_);
}
public void func_110133_a(String p_110133_1_)
{
this.field_94051_e = true;
this.inventoryTitle = p_110133_1_;
}
@Override
public int[] getAccessibleSlotsFromSide(int p_94128_1_) {
return new int[0];
}
@Override
public boolean canInsertItem(int p_102007_1_, ItemStack p_102007_2_, int p_102007_3_) {
return this.isItemValidForSlot(p_102007_1_, p_102007_2_);
}
@Override
public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_, int p_102008_3_) {
return false;
}
@Override
public int getSizeInventory()
{
return this.slotsCount;
}
@Override
public ItemStack getStackInSlot(int index) {
if(this.inventoryContents!=null){
return index >= 0 && index < this.inventoryContents.length ? this.inventoryContents[index] : null;
}else
return null;
}
@Override
public ItemStack decrStackSize(int p_70298_1_, int p_70298_2_) {
if (this.inventoryContents[p_70298_1_] != null)
{
ItemStack itemstack;
if (this.inventoryContents[p_70298_1_].stackSize <= p_70298_2_)
{
itemstack = this.inventoryContents[p_70298_1_];
this.inventoryContents[p_70298_1_] = null;
this.markDirty();
return itemstack;
}
else
{
itemstack = this.inventoryContents[p_70298_1_].splitStack(p_70298_2_);
if (this.inventoryContents[p_70298_1_].stackSize == 0)
{
this.inventoryContents[p_70298_1_] = null;
}
this.markDirty();
return itemstack;
}
}
else
{
return null;
}
}
@Override
public ItemStack getStackInSlotOnClosing(int p_70304_1_) {
if (this.inventoryContents[p_70304_1_] != null)
{
ItemStack itemstack = this.inventoryContents[p_70304_1_];
this.inventoryContents[p_70304_1_] = null;
return itemstack;
}
else
{
return null;
}
}
@SideOnly(Side.CLIENT)
@Override
public void setInventorySlotContents(int index, ItemStack stack) {
if(this.inventoryContents!=null) {
this.inventoryContents[index] = stack;
if (stack != null && stack.stackSize > this.getInventoryStackLimit()) {
stack.stackSize = this.getInventoryStackLimit();
}
this.markDirty();
}
}
@Override
public void markDirty()
{
if (this.field_70480_d != null)
{
for (int i = 0; i < this.field_70480_d.size(); ++i)
{
((IInventory)this.field_70480_d.get(i)).markDirty();
}
}
}
@Override
public String getInventoryName()
{
return this.inventoryTitle;
}
@Override
public boolean hasCustomInventoryName()
{
return this.field_94051_e;
}
@Override
public int getInventoryStackLimit() {
return 1;
}
@Override
public boolean isUseableByPlayer(EntityPlayer p_70300_1_)
{
return true;
}
@Override
public void openInventory() {}
@Override
public void closeInventory() {}
@Override
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
if(p_94041_2_!=null){
return p_94041_2_.getItem() == ItemLoader.filter;
} else return false;
}
//TO DO
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
if(inventoryContents!=null) {
int idItem = data.getInteger("idItem");
int countItem = data.getInteger("countItem");
inventoryContents[0] = new ItemStack(Item.getItemById(idItem), countItem);
}
}
//TO DO
@Override
public void writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
if(inventoryContents!=null) {
data.setInteger("idItem", Item.getIdFromItem(inventoryContents[0].getItem()));
data.setInteger("countItem", inventoryContents[0].stackSize);
}
}
@Override
public ConnectOverride overridePipeConnection(IPipeTile.PipeType pipeType, ForgeDirection forgeDirection) {
if(pipeType!= IPipeTile.PipeType.ITEM) {
return ConnectOverride.CONNECT;
}else {
return ConnectOverride.DISCONNECT;
}
}
public ItemStack[] getInventoryContents() {
return inventoryContents;
}
}
private String inventoryTitle;
private int slotsCount;
private ItemStack[] inventoryContents;
private List field_70480_d;
private boolean field_94051_e;
public static final int SLOTCOUNT=1;
public BlockTestTile(String inventoryTitle, boolean p_i1561_2_)
{
if(this.inventoryContents==null) {
this.inventoryContents = new ItemStack[SLOTCOUNT];
}
this.inventoryTitle = inventoryTitle;
this.field_94051_e = p_i1561_2_;
this.slotsCount = SLOTCOUNT;
}
public BlockTestTile(){
if(this.inventoryContents==null) {
this.inventoryContents = new ItemStack[SLOTCOUNT];
}
}
public void func_110134_a(IInvBasic p_110134_1_)
{
if (this.field_70480_d == null)
{
this.field_70480_d = new ArrayList();
}
this.field_70480_d.add(p_110134_1_);
}
public void func_110132_b(IInvBasic p_110132_1_)
{
this.field_70480_d.remove(p_110132_1_);
}
public void func_110133_a(String p_110133_1_)
{
this.field_94051_e = true;
this.inventoryTitle = p_110133_1_;
}
@Override
public int[] getAccessibleSlotsFromSide(int p_94128_1_) {
return new int[0];
}
@Override
public boolean canInsertItem(int p_102007_1_, ItemStack p_102007_2_, int p_102007_3_) {
return this.isItemValidForSlot(p_102007_1_, p_102007_2_);
}
@Override
public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_, int p_102008_3_) {
return false;
}
@Override
public int getSizeInventory()
{
return this.slotsCount;
}
@Override
public ItemStack getStackInSlot(int index) {
if(this.inventoryContents!=null){
return index >= 0 && index < this.inventoryContents.length ? this.inventoryContents[index] : null;
}else
return null;
}
@Override
public ItemStack decrStackSize(int p_70298_1_, int p_70298_2_) {
if (this.inventoryContents[p_70298_1_] != null)
{
ItemStack itemstack;
if (this.inventoryContents[p_70298_1_].stackSize <= p_70298_2_)
{
itemstack = this.inventoryContents[p_70298_1_];
this.inventoryContents[p_70298_1_] = null;
this.markDirty();
return itemstack;
}
else
{
itemstack = this.inventoryContents[p_70298_1_].splitStack(p_70298_2_);
if (this.inventoryContents[p_70298_1_].stackSize == 0)
{
this.inventoryContents[p_70298_1_] = null;
}
this.markDirty();
return itemstack;
}
}
else
{
return null;
}
}
@Override
public ItemStack getStackInSlotOnClosing(int p_70304_1_) {
if (this.inventoryContents[p_70304_1_] != null)
{
ItemStack itemstack = this.inventoryContents[p_70304_1_];
this.inventoryContents[p_70304_1_] = null;
return itemstack;
}
else
{
return null;
}
}
@SideOnly(Side.CLIENT)
@Override
public void setInventorySlotContents(int index, ItemStack stack) {
if(this.inventoryContents!=null) {
this.inventoryContents[index] = stack;
if (stack != null && stack.stackSize > this.getInventoryStackLimit()) {
stack.stackSize = this.getInventoryStackLimit();
}
this.markDirty();
}
}
@Override
public void markDirty()
{
if (this.field_70480_d != null)
{
for (int i = 0; i < this.field_70480_d.size(); ++i)
{
((IInventory)this.field_70480_d.get(i)).markDirty();
}
}
}
@Override
public String getInventoryName()
{
return this.inventoryTitle;
}
@Override
public boolean hasCustomInventoryName()
{
return this.field_94051_e;
}
@Override
public int getInventoryStackLimit() {
return 1;
}
@Override
public boolean isUseableByPlayer(EntityPlayer p_70300_1_)
{
return true;
}
@Override
public void openInventory() {}
@Override
public void closeInventory() {}
@Override
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
if(p_94041_2_!=null){
return p_94041_2_.getItem() == ItemLoader.filter;
} else return false;
}
//TO DO
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
if(inventoryContents!=null) {
int idItem = data.getInteger("idItem");
int countItem = data.getInteger("countItem");
inventoryContents[0] = new ItemStack(Item.getItemById(idItem), countItem);
}
}
//TO DO
@Override
public void writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
if(inventoryContents!=null) {
data.setInteger("idItem", Item.getIdFromItem(inventoryContents[0].getItem()));
data.setInteger("countItem", inventoryContents[0].stackSize);
}
}
@Override
public ConnectOverride overridePipeConnection(IPipeTile.PipeType pipeType, ForgeDirection forgeDirection) {
if(pipeType!= IPipeTile.PipeType.ITEM) {
return ConnectOverride.CONNECT;
}else {
return ConnectOverride.DISCONNECT;
}
}
public ItemStack[] getInventoryContents() {
return inventoryContents;
}
}
За не очень красивый местами код прошу простить, так как этот код тестовый, дальше он будет переписан в нормальный, когда будет рабочим