Hi,
I am making a mod which requires players to put in an item in a GUI. The GUI then checks the item name and the player's name to do stuff (this stuff is working).
However, I have no way of communicating the player's name from the server to the client, although I have the item's name on the client side.
Plus, there is a timer on the GUI and, when the item is deposited, counts down from 10 to 0 (when it reaches 0, the item disappears). However, if I put in an item in this table and the countdown begins, another player can access the table at the same time and he will see the item I have put in as well as the countdown (although the countdown will start at 10 no matter what and the item will disappear when my countdown reaches 0).
I don't know what to do. Please help!
TileEntity class:
public class TileEntityObjectiveTable extends TileEntity implements ICapabilityProvider, ITickable {
protected ItemStackHandler handler= new ItemStackHandler(1);
private String customName;
private int time = 0;
private int totalTime=200;
private String name = "test";
public ItemStack[] valid_items = {
new ItemStack(ModItems.ALLOY),
new ItemStack(ModItems.MOLTEN_COPPER),
new ItemStack(ModItems.MOLTEN_DIAMOND),
new ItemStack(ModItems.MOLTEN_EMERALD),
new ItemStack(ModItems.MOLTEN_GOLD),
new ItemStack(ModItems.MOLTEN_IRON),
new ItemStack(ModItems.MOLTEN_PLATINUM),
new ItemStack(ModItems.MOLTEN_SODIUM),
new ItemStack(ModItems.OBJECTIVE_SKIP),
new ItemStack(ModItems.SALT),
new ItemStack(ModItems.SALTY_WATER),
new ItemStack(ModItems.STRONG_ACID),
new ItemStack(ModItems.STRONG_BASE),
new ItemStack(ModItems.WEAK_ACID),
new ItemStack(ModItems.WEAK_BASE)
};
public boolean isItemValid(ItemStack stack) {
for (ItemStack valid : this.valid_items) {
if (valid.isItemEqualIgnoreDurability(stack)) {
return true;
}
} return false;
}
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
return true;
return super.hasCapability(capability, facing);
}
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
return (T) this.handler;
return super.getCapability(capability, facing);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
if(this.hasCustomName()) compound.setString("CustomName", this.customName);
return compound;
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
this.handler.deserializeNBT(compound.getCompoundTag("Inventory"));
this.time=compound.getInteger("time");
this.name=compound.getString("Name");
if(compound.hasKey("CustomName", 8)) this.setCustomName(compound.getString("CustomName"));
}
@Override
public SPacketUpdateTileEntity getUpdatePacket(){
NBTTagCompound compound = new NBTTagCompound();
compound.setTag("Inventory", this.handler.serializeNBT());
compound.setInteger("Time", (short)this.time);
compound.setString("Name", this.name);
return new SPacketUpdateTileEntity(getPos(), 1, compound);
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt){
NBTTagCompound compound = pkt.getNbtCompound();
this.handler.deserializeNBT(compound.getCompoundTag("Inventory"));
this.time=compound.getInteger("time");
this.name=compound.getString("Name");
}
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate)
{
return (oldState.getBlock() != newSate.getBlock());
}
public boolean isRunning(){
return this.handler.getStackInSlot(0) != ItemStack.EMPTY;
}
private IBlockState getState() {
return world.getBlockState(pos);
}
@Override
public ITextComponent getDisplayName() {
return this.hasCustomName() ? new TextComponentString(this.customName) : new TextComponentTranslation("Objective Table");
}
public boolean hasCustomName() {
return this.customName != null && !this.customName.isEmpty();
}
public void setCustomName(String customName) {
this.customName = customName;
}
public boolean isUsableByPlayer(EntityPlayer playerIn) {
return this.world.getTileEntity(this.pos) != this ? false : playerIn.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
}
@Override
public void update() {
if(isItemValid(this.handler.getStackInSlot(0))) {
System.out.println(this.handler.getStackInSlot(0).toString());
time++;
if(time == totalTime) {
check(name, handler.getStackInSlot(0).toString());
System.out.println(handler.getStackInSlot(0).toString());
System.out.println(name.toString());
handler.setStackInSlot(0, ItemStack.EMPTY);
time=0;
}
}
else {
time=0;
}
}
public void check(String name, String item) {
if(!world.isRemote) {
Gamemode.checkObjective(name, item);
}
}
public int getField(int i) {
switch (i) {
case 1: return this.time;
default : return 0;
}
}
public void setField(int i, int j) {
switch (i) {
case 1: this.time=j;
break;
default : return ;
}
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
GUI class:
public class GuiObjectiveTable extends GuiContainer{
private static final ResourceLocation TEXTURES = new ResourceLocation (Reference.MOD_ID + ":textures/gui/objective_table.png");
private final EntityPlayer player;
private final TileEntityObjectiveTable tileentity;
private int time = 0;
private int totalTime = 200;
private String name;
private GuiButton button;
public GuiObjectiveTable(EntityPlayer player, TileEntityObjectiveTable tileentity) {
super (new ContainerObjectiveTable(player, tileentity));
this.player = player;
this.tileentity = tileentity;
tileentity.setName(player.getName());
}
@Override
public void initGui() {
super.initGui();
}
@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
String tileName = this.tileentity.getDisplayName().getUnformattedText();
this.fontRenderer.drawString(tileName, (this.xSize / 2 - this.fontRenderer.getStringWidth(tileName) / 2 ) + 3, 8, 4210752);
}
@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
GlStateManager.color(1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(TEXTURES);
this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
if (this.tileentity.getField(1)!=0) {
int k = this.getTime();
if(k<=5) {
tileentity.setName(this.player.getDisplayNameString());
}
this.fontRenderer.drawString(this.tileentity.getName(), this.getGuiLeft() + 122, this.getGuiTop() +71 + 2, 4210752);
String v = "" + (10-k/20);
this.buttonList.clear();
buttonList.add(button = new GuiButton(0, this.guiLeft + 8, this.guiTop + 30 + 5,45, 20, v));
}
else {
this.buttonList.clear();
String v = "" + 0;
buttonList.add(button = new GuiButton(0, this.guiLeft + 8, this.guiTop + 30 + 5,45, 20, v));
}
}
private int getTime() {
return this.tileentity.getField(1);
}
public boolean isItemValid(ItemStack stack) {
for (ItemStack valid : this.valid_items) {
if (valid.isItemEqualIgnoreDurability(stack)) {
return true;
}
} return false;
}
public ItemStack[] valid_items = {
new ItemStack(ModItems.CALCIUM),
new ItemStack(ModItems.COPPER),
new ItemStack(ModItems.PLATINUM),
new ItemStack(ModItems.SODIUM),
new ItemStack(ModItems.MOLTEN_CALCIUM),
new ItemStack(ModItems.CHLORINE),
new ItemStack(ModItems.OBJECTIVE_SKIP)
};
}
Thank you!