So here I am again, stuck with a problem I can't figure out myself. I searched for this topic and found some older post with the same/similar issue, but with a different (older) forge version. The bugfix they found was adding @NetworkMod to the main mod file, but this is not needed anymore for 1.7 as far as I know.
I can place my block, click on it and it will open the gui properly. All player items show up just fine. I can put items in my block with a hopper and they will pop out when I destroy the block, but they wont show up.
When I click an item, it seems to pick it up to the mouse and place it back in the slot in a split second. Shift-Clicking items wont work either. They get duplicated and when I close the gui and open it back up, they are still there, but when I destroy the block, they wont spit out.
I bet I just missing something very obvious here, but I have no clue where to look anymore. Hope someone can point me to the right direction =)
My mod file
[spoiler=Main]
@Mod(modid = SolarCraft.MODID, version = SolarCraft.VERSION)
public class SolarCraft {
public static final String MODID = "solarcraft";
public static final String VERSION = "0.1";
public static Block crystallizerBlock;
(...)
@Instance(SolarCraft.MODID)
public static SolarCraft instance;
@SidedProxy(clientSide = "solarcraft.proxy.ClientProxy", serverSide = "solarcraft.proxy.CommonProxy")
public static CommonProxy proxy;
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
}
@EventHandler
public void init(FMLInitializationEvent event) {
crystallizerBlock = registerBlock(new BlockCrystallizer("crystallizerBlock"));
(...)
TileTechCrystallizer.init();
(...)
NetworkRegistry.INSTANCE.registerGuiHandler(SolarCraft.instance, new GuiHandler());
proxy.registerRenderers();
}
private Block registerBlock(Block regBlock) {
GameRegistry.registerBlock(regBlock, regBlock.getUnlocalizedName());
return regBlock;
}
(...)
}
The gui handler
[spoiler=GuiHandler]
public class GuiHandler implements IGuiHandler {
(...)
public static final int crystallizerGui = 4;
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
TileEntity te;
switch (ID) {
(...)
case crystallizerGui:
te = world.getTileEntity(x, y, z);
if (te != null && te instanceof TileTechCrystallizer)
return new ContainerCrystallizer(player.inventory, (TileTechCrystallizer) te);
break;
}
return null;
}
@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
TileEntity te = world.getTileEntity(x, y, z);
switch (ID) {
(...)
case crystallizerGui:
if (te != null && te instanceof TileTechCrystallizer)
return new GuiCrystallizer(player.inventory, (TileTechCrystallizer) te);
break;
}
return null;
}
}
The Gui
[spoiler=GuiCrystallizer]
public class GuiCrystallizer extends GuiContainer {
public GuiCrystallizer(IInventory playerInv, TileTechCrystallizer te) {
super(new ContainerCrystallizer(playerInv, te));
this.xSize = 176;
this.ySize = 166;
}
@Override
protected void drawGuiContainerBackgroundLayer(float gameTicks, int mouseX, int mouseY) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.renderEngine.bindTexture(new ResourceLocation("solarcraft:textures/gui/crystallizer.png"));
int x = (width - xSize) / 2;
int y = (height - ySize) / 2;
this.drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
}
}
The container
[spoiler=ContainerMachine]
public abstract class ContainerMachine extends Container {
protected TileTechMachine tem;
protected int invSize = 1;
public ContainerMachine(TileTechMachine tem) {
this.tem = tem;
this.invSize = tem.getSizeInventory();
}
@Override
public boolean canInteractWith(EntityPlayer playerIn) {
return this.tem.isUseableByPlayer(playerIn);
}
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
ItemStack stack = null;
Slot slotObject = (Slot) inventorySlots.get(slot);
// null checks and checks if the item can be stacked (maxStackSize > 1)
if (slotObject != null && slotObject.getHasStack()) {
ItemStack stackInSlot = slotObject.getStack();
stack = stackInSlot.copy();
// merges the item into player inventory since its in the tileEntity
if (slot < tem.getSizeInventory()) {
if (!this.mergeItemStack(stackInSlot, tem.getSizeInventory(), 36 + tem.getSizeInventory(), true)) {
return null;
}
}
// places it into the tileEntity is possible since its in the player inventory
else if (!this.mergeItemStack(stackInSlot, 0, tem.getSizeInventory(), false)) {
return null;
}
if (stackInSlot.stackSize == 0) {
slotObject.putStack(null);
} else {
slotObject.onSlotChanged();
}
if (stackInSlot.stackSize == stack.stackSize) {
return null;
}
slotObject.onPickupFromSlot(player, stackInSlot);
}
return stack;
}
}
[spoiler=ContainerCrystallizer]
public class ContainerCrystallizer extends ContainerMachine {
public ContainerCrystallizer(IInventory playerInv, TileTechMachine tem) {
super(tem);
// My Tile Entity
addSlotToContainer(new Slot(tem, 0, 44, 11));
addSlotToContainer(new Slot(tem, 1, 62, 11));
addSlotToContainer(new Slot(tem, 2, 44, 29));
addSlotToContainer(new Slot(tem, 3, 62, 29));
addSlotToContainer(new Slot(tem, 4, 89, 59));
addSlotToContainer(new Slot(tem, 5, 116, 20));
// Player Inventory
for (int y = 0; y < 3; ++y) {
for (int x = 0; x < 9; ++x) {
int i = x + y * 9 + 9;
Slot slot = addSlotToContainer(new Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
}
}
// Player Hotbar
for (int x = 0; x < 9; ++x) {
Slot slot = addSlotToContainer(new Slot(playerInv, x, 8 + x * 18, 142));
}
}
}
The block
[spoiler=BlockCrystallizer]
public class BlockCrystallizer extends BlockMachine {
public BlockCrystallizer(String unlocalizedName) {
super(unlocalizedName);
}
@Override
public TileEntity createNewTileEntity(World world, int meta) {
return new TileTechCrystallizer();
}
@Override
public void registerBlockIcons(IIconRegister iconRegister) {
super.registerBlockIcons(iconRegister);
icons[3] = iconRegister.registerIcon(SolarCraft.MODID + ":" + "crystallizer");
}
@Override
public int getGuiId() {
return GuiHandler.crystallizerGui;
}
}
The tile
[spoiler=TileTechMachine]
public abstract class TileTechMachine extends TileTech implements IEnergyReceiver, IInventory {
private int energyRate = 0;
private Set<ForgeDirection> inputDirections = new HashSet<ForgeDirection>();
public ItemStack[] inventory;
private int inventorySize;
private String customName;
public TileTechMachine(int invSize) {
inventorySize = invSize;
this.inventory = new ItemStack[invSize];
}
@Override
public boolean canConnectEnergy(ForgeDirection arg0) {
return inputDirections.contains(arg0);
}
@Override
public int getEnergyStored(ForgeDirection from) {
return getStorage().getEnergyStored();
}
@Override
public int getMaxEnergyStored(ForgeDirection from) {
return getStorage().getMaxEnergyStored();
}
@Override
public int receiveEnergy(ForgeDirection side, int maxReceive, boolean simulate) {
return getStorage().receiveEnergy(maxReceive, simulate);
}
@Override
public void updateEntity() {
super.updateEntity();
process();
}
protected abstract void process();
@Override
public ItemStack getStackInSlot(int index) {
return this.inventory[index];
}
@Override
public ItemStack decrStackSize(int slot, int num) {
if (this.inventory[slot] != null) {
ItemStack stack;
if (this.inventory[slot].stackSize <= num) {
stack = this.inventory[slot];
this.inventory[slot] = null;
return stack;
} else {
stack = this.inventory[slot].splitStack(num);
if (this.inventory[slot].stackSize == 0) {
this.inventory[slot] = null;
}
return stack;
}
} else
return null;
}
@Override
public ItemStack getStackInSlotOnClosing(int index) {
if (this.inventory[index] != null) {
ItemStack var2 = this.inventory[index];
this.inventory[index] = null;
return var2;
} else
return null;
}
@Override
public void setInventorySlotContents(int index, ItemStack stack) {
this.inventory[index] = stack;
if (stack != null && stack.stackSize > this.getInventoryStackLimit()) {
stack.stackSize = this.getInventoryStackLimit();
}
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
if (worldObj.getTileEntity(xCoord, yCoord, zCoord) != this) {
return false;
}
return player.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D) <= 64D;
}
@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
return true;
}
@Override
public String getInventoryName() {
return this.hasCustomInventoryName() ? this.customName : "container.tiletechmachine";
}
@Override
public boolean hasCustomInventoryName() {
return this.customName != null && !this.customName.equals("");
}
@Override
public int getSizeInventory() {
return inventorySize;
}
@Override
public int getInventoryStackLimit() {
return 64;
}
public String getCustomName() {
return this.customName;
}
public void setCustomName(String customName) {
this.customName = customName;
}
@Override
public void openInventory() {
}
@Override
public void closeInventory() {
}
@Override
public void readFromNBT(NBTTagCompound tagCompound) {
super.readFromNBT(tagCompound);
NBTTagList tagList = tagCompound.getTagList("Inventory", 10);
for (int i = 0; i < tagList.tagCount(); i++) {
NBTTagCompound tag = tagList.getCompoundTagAt(i);
byte slot = tag.getByte("Slot");
if (slot >= 0 && slot < inventory.length) {
inventory[slot] = ItemStack.loadItemStackFromNBT(tag);
}
}
}
@Override
public void writeToNBT(NBTTagCompound tagCompound) {
super.writeToNBT(tagCompound);
NBTTagList itemList = new NBTTagList();
for (int i = 0; i < inventory.length; i++) {
ItemStack stack = inventory[i];
if (stack != null) {
NBTTagCompound tag = new NBTTagCompound();
tag.setByte("Slot", (byte) i);
stack.writeToNBT(tag);
itemList.appendTag(tag);
}
}
tagCompound.setTag("Inventory", itemList);
}
}
[spoiler=TileTechCrystallizer]
public class TileTechCrystallizer extends TileTechMachine {
public TileTechCrystallizer() {
super(6);
}
public static void init() {
GameRegistry.registerTileEntity(TileTechCrystallizer.class, "crystallizerTile");
}
@Override
protected void process() {
// TODO Auto-generated method stub
}
}