Hello, i got another problem for you now. I have a value that's getting updated on the client side, but not on the server side. The way i update the code is via a GUI, when a click a button it's incrementing or decrementing a value and setting it to a TileEntity. In my container class i have some debug code:
System.out.println((tileentity.worldObj.isRemote ? "Client" : "Server") + " : " + tileentity.getPage());
That prints this in the console:
2014-02-24 17:00:47 [iNFO] [sTDOUT] Server : 0
2014-02-24 17:00:47 [iNFO] [sTDOUT] Client : 0
And if i increment the value using a button, this is getting printed in the console:
2014-02-24 17:07:41 [iNFO] [sTDOUT] Server : 0
2014-02-24 17:07:42 [iNFO] [sTDOUT] Client :1
As you see the value is getting updated client-side, but not on the server side. I added this code to my TileEntity class:
public Packet getDescriptionPacket()
{
NBTTagCompound tag = new NBTTagCompound();
this.writeToNBT(tag);
return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, tag);
}
public void onDataPacket(INetworkManager networkManager, Packet132TileEntityData packet)
{
this.readFromNBT(packet.data);
}
But that doesn't seem to work. I have registered my TileEntity class. Does anyone know how to correctly syncronize that value?
Code:
[spoiler=TileEntityCustomizableBlock]
package larsg310.mods.customization.tileentity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.tileentity.TileEntity;
public class TileEntityCustomizableBlock extends TileEntity implements ISidedInventory
{
private ItemStack[] inventory = new ItemStack[2];
private float minX = 0;
private float maxX = 1;
private int page = 0;
public float getMinX()
{
return minX;
}
public void setMinX(float minX)
{
this.minX = minX;
}
public float getMaxX()
{
return maxX;
}
public void setMaxX(float maxX)
{
this.maxX = maxX;
}
public int getPage()
{
return page;
}
public void setPage(int page)
{
this.page = page;
}
public Packet getDescriptionPacket()
{
NBTTagCompound tag = new NBTTagCompound();
this.writeToNBT(tag);
return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, tag);
}
public void onDataPacket(INetworkManager networkManager, Packet132TileEntityData packet)
{
this.readFromNBT(packet.data);
}
public void writeToNBT(NBTTagCompound tag)
{
super.writeToNBT(tag);
tag.setInteger("Page", page);
tag.setFloat("minX", minX);
tag.setFloat("maxX", maxX);
}
public void readFromNBT(NBTTagCompound tag)
{
page = tag.getInteger("Page");
minX = tag.getFloat("minX");
maxX = tag.getFloat("maxX");
}
public int getSizeInventory()
{
return inventory.length;
}
public ItemStack getStackInSlot(int slot)
{
return inventory[slot];
}
public ItemStack decrStackSize(int slot, int amount)
{
if (this.inventory[slot] != null)
{
ItemStack itemstack;
if (this.inventory[slot].stackSize <= amount)
{
itemstack = this.inventory[slot];
this.inventory[slot] = null;
return itemstack;
}
else
{
itemstack = this.inventory[slot].splitStack(amount);
if (this.inventory[slot].stackSize == 0)
{
this.inventory[slot] = null;
}
return itemstack;
}
}
else
{
return null;
}
}
public ItemStack getStackInSlotOnClosing(int slot)
{
if (this.inventory[slot] != null)
{
ItemStack itemstack = this.inventory[slot];
this.inventory[slot] = null;
return itemstack;
}
else
{
return null;
}
}
public void setInventorySlotContents(int slot, ItemStack itemstack)
{
this.inventory[slot] = itemstack;
if (itemstack != null && itemstack.stackSize > this.getInventoryStackLimit())
{
itemstack.stackSize = this.getInventoryStackLimit();
}
}
public String getInvName()
{
return "container.customizableBlock";
}
public boolean isInvNameLocalized()
{
return false;
}
public int getInventoryStackLimit()
{
return 1;
}
public boolean isUseableByPlayer(EntityPlayer entityplayer)
{
return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : entityplayer.getDistanceSq((double) this.xCoord + 0.5D, (double) this.yCoord + 0.5D, (double) this.zCoord + 0.5D) <= 64.0D;
}
public void openChest()
{
}
public void closeChest()
{
}
public boolean isItemValidForSlot(int slot, ItemStack itemstack)
{
return true;
}
public int[] getAccessibleSlotsFromSide(int var1)
{
return null;
}
public boolean canInsertItem(int i, ItemStack itemstack, int j)
{
return false;
}
public boolean canExtractItem(int i, ItemStack itemstack, int j)
{
return false;
}
}
[spoiler=ContainerCustomizableBlock]
package larsg310.mods.customization.inventory;
import larsg310.mods.customization.tileentity.TileEntityCustomizableBlock;
import net.minecraft.client.Minecraft;
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 ContainerCustomizableBlock extends Container
{
private TileEntityCustomizableBlock tileentity;
private InventoryPlayer inventory;
private Slot[] slots;
public ContainerCustomizableBlock(InventoryPlayer inventory, TileEntityCustomizableBlock tileentity)
{
this.tileentity = tileentity;
this.inventory = inventory;
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 + 80));
}
}
for (int i = 0; i < 9; ++i)
{
this.addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 142 + 80));
}
slots = new Slot[]{new Slot(tileentity, 0, Integer.MIN_VALUE, 20), new Slot(tileentity, 1, Integer.MIN_VALUE, 20)};
this.addSlotToContainer(slots[0]);
this.addSlotToContainer(slots[1]);
System.out.println((tileentity.worldObj.isRemote ? "Client" : "Server") + " : " + tileentity.getPage());
this.drawSlotsBasedOnPage();
}
public boolean canInteractWith(EntityPlayer entityplayer)
{
return this.tileentity.isUseableByPlayer(entityplayer);
}
public void detectAndSendChanges()
{
super.detectAndSendChanges();
drawSlotsBasedOnPage();
}
private void drawSlotsBasedOnPage()
{
for(Slot slot : slots)
{
slot.xDisplayPosition = Integer.MIN_VALUE;
}
switch (tileentity.getPage())
{
case 0:
slots[0].xDisplayPosition = 100;
break;
case 1:
slots[1].xDisplayPosition = 118;
break;
}
}
public ItemStack transferStackInSlot(EntityPlayer player, int slot)
{
return null;
}
}
[spoiler=GuiCustomizableBlock]
package larsg310.mods.customization.gui;
import larsg310.mods.customization.inventory.ContainerCustomizableBlock;
import larsg310.mods.customization.lib.Reference;
import larsg310.mods.customization.tileentity.TileEntityCustomizableBlock;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
public class GuiCustomizableBlock extends GuiContainer
{
private int page;
private int maxPages = 1;
private TileEntityCustomizableBlock tileentity;
ResourceLocation texture = new ResourceLocation(Reference.MOD_ID, "textures/gui/customizableBlock.png");
public GuiCustomizableBlock(InventoryPlayer inventory, TileEntityCustomizableBlock tileentity)
{
super(new ContainerCustomizableBlock(inventory, tileentity));
this.tileentity = tileentity;
this.page = tileentity.getPage();
this.ySize = 246;
}
@Override
protected void drawGuiContainerForegroundLayer(int x, int y)
{
fontRenderer.drawString(StatCollector.translateToLocal("container.customizableBlock"), 8, 6, 4210752);
String pageString = "Page: " + page + " / " + maxPages;
fontRenderer.drawString(pageString, xSize / 2 - ((pageString.length() / 2) * 6), 123, 4210752);
}
@Override
protected void drawGuiContainerBackgroundLayer(float f, int x, int y)
{
this.mc.getTextureManager().bindTexture(texture);
int x1 = (width - xSize) / 2;
int y1 = (height - ySize) / 2;
this.drawTexturedModalRect(x1, y1, 0, 0, xSize, ySize);
}
@SuppressWarnings("unchecked")
public void initGui()
{
super.initGui();
this.buttonList.clear();
this.buttonList.add(new GuiButton(0, (width - xSize) / 2 + 7, 113, 12, 20, "<"));
this.buttonList.add(new GuiButton(1, (width - xSize) / 2 + (xSize - 19), 113, 12, 20, ">"));
}
public void actionPerformed(GuiButton button)
{
switch (button.id)
{
case 0:
page--;
break;
case 1:
page++;
break;
}
if (page < 0)
{
page = 0;
}
if (page > maxPages)
{
page = maxPages;
}
this.tileentity.setPage(page);
}
}
[spoiler=BlockCustomizableBlock]
package larsg310.mods.customization.block;
import larsg310.mods.customization.CustomizationCraft;
import larsg310.mods.customization.lib.GuiIds;
import larsg310.mods.customization.lib.Names;
import larsg310.mods.customization.tileentity.TileEntityCustomizableBlock;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
public class BlockCustomizableBlock extends Block
{
public BlockCustomizableBlock(int id)
{
super(id, Material.iron);
this.setCreativeTab(CreativeTabs.tabBlock);
this.setUnlocalizedName(Names.CUSTOMIZABLE_BLOCK);
}
public TileEntity createTileEntity(World world, int meta)
{
return new TileEntityCustomizableBlock();
}
public boolean hasTileEntity(int meta)
{
return true;
}
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ)
{
if(!world.isRemote)
{
player.openGui(CustomizationCraft.instance, GuiIds.CUSTOMIZABLE_BLOCK, world, x, y, z);
}
return true;
}
public void setBlockBoundsBasedOnState(IBlockAccess world, int x, int y, int z)
{
TileEntity te = world.getBlockTileEntity(x, y, z);
if (te instanceof TileEntityCustomizableBlock)
{
TileEntityCustomizableBlock tileentity = (TileEntityCustomizableBlock) te;
this.setBlockBounds(tileentity.getMinX(), 0, 0, tileentity.getMaxX(), 1, 1);
}
}
public boolean isOpaqueCube()
{
return false;
}
}
[spoiler=GuiHandler]
package larsg310.mods.customization.handler;
import larsg310.mods.customization.CustomizationCraft;
import larsg310.mods.customization.gui.GuiCustomizableBlock;
import larsg310.mods.customization.inventory.ContainerCustomizableBlock;
import larsg310.mods.customization.lib.GuiIds;
import larsg310.mods.customization.tileentity.TileEntityCustomizableBlock;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.IGuiHandler;
import cpw.mods.fml.common.network.NetworkRegistry;
public class GuiHandler implements IGuiHandler
{
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
{
switch(ID)
{
case GuiIds.CUSTOMIZABLE_BLOCK:
return new ContainerCustomizableBlock(player.inventory, (TileEntityCustomizableBlock)world.getBlockTileEntity(x, y, z));
}
return null;
}
@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
{
switch(ID)
{
case GuiIds.CUSTOMIZABLE_BLOCK:
return new GuiCustomizableBlock(player.inventory, (TileEntityCustomizableBlock)world.getBlockTileEntity(x, y, z));
}
return null;
}
public static void register()
{
NetworkRegistry.instance().registerGuiHandler(CustomizationCraft.instance, new GuiHandler());
}
}
If you need more code, i'll be happy to provide more.