Jump to content

[1.11] Gui Synchronization


abused_master

Recommended Posts

Hey guys, so i was wondering how i can fully sync my gui with the client, the reason being is my gui for my furnace works fine except for 2 problems, #1 when i take out all the stuff it has smelted and put them in my inventory the amount that was there stays until you click it or reopen the gui, another thing is my progress bar, its not rendering correctly because of this.

Link to comment
Share on other sites

Tile:

public class TileRFFurnace extends TileEntity implements ITickable, IInventory, ISidedInventory, IEnergyReceiver, IEnergyProvider{

public EnergyStorage storage = new EnergyStorage(50000);
private NonNullList<ItemStack> furnaceInv= NonNullList.<ItemStack>func_191197_a(2, ItemStack.field_190927_a);
private int cookTime;
private int totalCookTime;

@Override
public void readFromNBT(NBTTagCompound nbt) {
	super.readFromNBT(nbt);
	NBTTagList inventory = (NBTTagList) nbt.getTag("Items");
			for(int i = 0; i < inventory.tagCount(); i++) {
					NBTTagCompound itemTag = inventory.getCompoundTagAt(i);
					ItemStack stack = new ItemStack(itemTag);
					furnaceInv.set(itemTag.getByte("Slot"), stack);
				}
	this.cookTime = nbt.getInteger("CookTime");
	this.totalCookTime = nbt.getInteger("TotalCookTime");
	storage.readFromNBT(nbt);
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
	super.writeToNBT(nbt);
			NBTTagList inventory = new NBTTagList();
			for(byte slot = 0; slot < furnaceInv.size(); slot++) {
					ItemStack stack = furnaceInv.get(slot);
					if(!stack.func_190926_b()) {
							NBTTagCompound itemTag = new NBTTagCompound();
							stack.writeToNBT(itemTag);
							itemTag.setByte("Slot", slot);
							inventory.appendTag(itemTag);
						}
				}
			nbt.setTag("Items", inventory);
	nbt.setInteger("cookTime", (short)this.cookTime);
	nbt.setInteger("TotalCookTime", (short)this.totalCookTime);
	return storage.writeToNBT(nbt);
}

@Override
public boolean canConnectEnergy(EnumFacing from) {

	return true;
}

@Override
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate) {
	return storage.receiveEnergy(maxReceive, simulate);
}

@Override
public int getEnergyStored(EnumFacing from) {
	return storage.getEnergyStored();
}

@Override
public int getMaxEnergyStored(EnumFacing from) {
	return storage.getMaxEnergyStored();
}

    public boolean canInteractWith(EntityPlayer playerIn) {
        return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
    }

    private net.minecraftforge.items.IItemHandler itemHandler;

    protected net.minecraftforge.items.IItemHandler createUnSidedHandler()
    {
        return new net.minecraftforge.items.wrapper.InvWrapper(this);
    }

    @SuppressWarnings("unchecked")
    @Override
    @javax.annotation.Nullable
    public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, @javax.annotation.Nullable net.minecraft.util.EnumFacing facing)
    {
        if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
            return (T) (itemHandler == null ? (itemHandler = createUnSidedHandler()) : itemHandler);
        return super.getCapability(capability, facing);
    }

    @Override
    public boolean hasCapability(net.minecraftforge.common.capabilities.Capability<?> capability, @javax.annotation.Nullable net.minecraft.util.EnumFacing facing)
    {
        return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
    }

@Override
public int getSizeInventory() {
	return this.furnaceInv.size();
}


@Override
public ItemStack getStackInSlot(int index) {
        return (ItemStack)this.furnaceInv.get(index);
}


@Override
public ItemStack decrStackSize(int index, int count) {
        return ItemStackHelper.getAndSplit(this.furnaceInv, index, count);
}


@Override
public ItemStack removeStackFromSlot(int index) {
        return ItemStackHelper.getAndRemove(this.furnaceInv, index);
}

@Override
public int getInventoryStackLimit() {
	return 64;
}


@Override
public boolean isUseableByPlayer(EntityPlayer player) {
        return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
}


@Override
public void openInventory(EntityPlayer player) {

}
@Override
public void closeInventory(EntityPlayer player) {

}


@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
    {
        if (index == 2)
        {
            return false;
        }
        else if (index != 1)
        {
            return true;
        }else {

        	return true;
        }
    }
  }

@Override
public int getField(int id)
{
	switch (id)
	{
		case 0:
			return this.cookTime;
		case 1:
			return this.totalCookTime;
		default:
			return 0;
	}
}

@Override
public void setField(int id, int value)
{
	switch (id)
	{
		case 0:
			this.cookTime = value;
			break;
		case 1:
			this.totalCookTime = value;
	}
}

@Override
public int getFieldCount() {
	return 4;
}


@Override
public void clear() {
            this.furnaceInv.clear();
}


@Override
public String getName() {
	return ModBlocks.furnaceInv.getUnlocalizedName();
}


@Override
public boolean hasCustomName() {
	return false;
}


@Override
public int[] getSlotsForFace(EnumFacing side) {
        return null;
}


@Override
public boolean canInsertItem(int index, net.minecraft.item.ItemStack itemStackIn, EnumFacing direction) {
        return this.isItemValidForSlot(index, itemStackIn);
}


@Override
public boolean canExtractItem(int index, net.minecraft.item.ItemStack stack, EnumFacing direction) {
	 if (direction == EnumFacing.DOWN && index == 1)
        {
            Item item = stack.getItem();

            if (item != Items.WATER_BUCKET && item != Items.BUCKET)
            {
                return false;
            }
        }
	 return true;
      }

    public boolean canSmelt()
    {
    	if (((ItemStack)this.furnaceInv.get(0)).func_190926_b()) {
	return false;
    	}else {
		return true;
	}
    }

@Override
public void update() {
	if(!getWorld().isRemote) {
		PacketHandler.INSTANCE.sendToAll(new MessageTEUpdate(this));
	}

	boolean flag1 = false;

	if (!this.worldObj.isRemote)
	{
		ItemStack itemstack = (ItemStack)this.furnaceInv.get(1);
		if (storage.getEnergyStored() > 50 || !itemstack.func_190926_b() && !((ItemStack)this.furnaceInv.get(0)).func_190926_b())
		{
			if (!(storage.getEnergyStored() > 50) && this.canSmelt())
			{
				if (storage.getEnergyStored() > 50)
				{
					flag1 = true;

					if (!itemstack.func_190926_b())
					{
						Item item = itemstack.getItem();
						itemstack.func_190918_g(1);

						if (itemstack.func_190926_b())
						{
							ItemStack item1 = item.getContainerItem(itemstack);
							this.furnaceInv.set(1, item1);
						}
					}
				}
			}

			if (storage.getEnergyStored() > 50 && this.canSmelt())
			{
				++this.cookTime;

				if (this.cookTime == this.totalCookTime)
				{
					this.cookTime = 0;
					this.totalCookTime = this.getCookTime((ItemStack)this.furnaceInv.get(0));
					this.SmeltItem();
					flag1 = true;
				}
			}
			else
			{
				this.cookTime = 0;
			}
		}
		else if (!(storage.getEnergyStored() > 50) && this.cookTime > 0)
		{
			this.cookTime = MathHelper.clamp_int(this.cookTime - 2, 0, this.totalCookTime);
		}
	}

	if (flag1)
	{
		this.markDirty();
	}

}

public void SmeltItem() {
	if (this.canSmelt()) {
		ItemStack itemstack = (ItemStack) this.furnaceInv.get(0);
		ItemStack itemstack1 = FurnaceRecipes.instance().getSmeltingResult(itemstack);
		ItemStack itemstack2 = (ItemStack) this.furnaceInv.get(1);
		if (storage.getEnergyStored() >= 50 && !itemstack1.func_190926_b()) {
			storage.setEnergyStored(storage.getEnergyStored() - 50);

			if (itemstack2.func_190926_b()) {
				this.furnaceInv.set(1, itemstack1.copy());
			} else if (itemstack2.getItem() == itemstack1.getItem()) {
				itemstack2.func_190917_f(itemstack1.func_190916_E());
			}
			itemstack.func_190918_g(1);
		}
	}
}

@Override
public boolean func_191420_l() {
	for (ItemStack itemstack : this.furnaceInv)
        {
            if (!itemstack.func_190926_b())
            {
                return false;
            }
       	  }
	return true;
     	}

@Override
public void setInventorySlotContents(int index, net.minecraft.item.ItemStack stack) {
        ItemStack itemstack = (ItemStack)this.furnaceInv.get(index);
        boolean flag = !stack.func_190926_b() && stack.isItemEqual(itemstack) && ItemStack.areItemStackTagsEqual(stack, itemstack);
        this.furnaceInv.set(index, stack);

        if (stack.func_190916_E() > this.getInventoryStackLimit())
        {
            stack.func_190920_e(this.getInventoryStackLimit());
        }

        if (index == 0 && !flag)
        {
		this.totalCookTime = this.getCookTime(stack);
		this.cookTime = 0;
            this.markDirty();
        }
}

@Nullable
    @Override
    public SPacketUpdateTileEntity getUpdatePacket() {
    	
        NBTTagCompound data = new NBTTagCompound();
        writeToNBT(data);
        return new SPacketUpdateTileEntity(this.pos, 1, data);
    }
    
    @Override
    @SideOnly(Side.CLIENT)
    public void onDataPacket(NetworkManager networkManager, SPacketUpdateTileEntity s35PacketUpdateTileEntity) {
        readFromNBT(s35PacketUpdateTileEntity.getNbtCompound());
        worldObj.markBlockRangeForRenderUpdate(this.pos, this.pos);
        markForUpdate();
    }
    

    public void markForUpdate() {
        if (this.worldObj != null) {
            Block block = worldObj.getBlockState(this.pos).getBlock();
            this.worldObj.notifyBlockUpdate(this.pos, worldObj.getBlockState(this.pos), worldObj.getBlockState(this.pos), 3);
        }
    }
    
    @Override
    public NBTTagCompound getUpdateTag()
    {
      NBTTagCompound nbtTagCompound = new NBTTagCompound();
      writeToNBT(nbtTagCompound);
      return nbtTagCompound;
    }

    @Override
    public void handleUpdateTag(NBTTagCompound tag)
    {
      this.readFromNBT(tag);
    }

@Override
public int extractEnergy(EnumFacing from, int maxExtract, boolean simulate) {
	return 0;
}

public int getCookTime(ItemStack stack)
{
	return 200;
}
}

 

GUI:

public class GuiRFFurnace extends GuiContainer {

public static final ResourceLocation RFFurance= new ResourceLocation(Info.MODID, "textures/gui/furnace_gui.png");
    public static final int WIDTH = 176;
    public static final int HEIGHT = 166;
    TileRFFurnace furnace;

    public GuiRFFurnace (TileRFFurnace tileEntity, RFFurnacecontainer, TileEntity te) {
        super(container);
        xSize = WIDTH;
        ySize = HEIGHT;
        furnace = (TileRFFurnace) tileEntity;
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
        mc.getTextureManager().bindTexture(RFFurnace);
        drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);

        int l = this.getCookProgressScaled(24);
        this.drawTexturedModalRect(guiLeft + 81, guiTop + 27, 176, 46, l, 16);

        if (this.isPointInRegion(10, 9, 14, 42, mouseX, mouseY)) {
            List<String> rf = new ArrayList<String>();
            rf.add(furnace.storage.getEnergyStored() + "/50000 RF");
            GuiUtils.drawHoveringText(rf, mouseX, mouseY, 25, 120, -10, mc.fontRendererObj);
        }
    }
    private int getCookProgressScaled(int pixels)
    {
        int i = this.furnace.getField(0);
        int j = this.furnace.getField(1);
        return j != 0 && i != 0 ? i * pixels / j : 0;
    }
}

Link to comment
Share on other sites

implements ITickable, IInventory, ISidedInventory, IEnergyReceiver, IEnergyProvider

 

Aww for fuck's sake, haven't you see anything on these forums about Capabilities?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

implements ITickable, IInventory, ISidedInventory, IEnergyReceiver, IEnergyProvider

 

Aww for fuck's sake, haven't you see anything on these forums about Capabilities?

 

To expand on this slightly:

 

The whole point of the Capability System is that you don't implement every interface on your

Entity

/

TileEntity

/

Item

, instead you store an instance of the interface and provide it from the

ICapabilityProvider

methods.

 

The Capability System is documented here.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

implements ITickable, IInventory, ISidedInventory, IEnergyReceiver, IEnergyProvider

 

Aww for fuck's sake, haven't you see anything on these forums about Capabilities?

 

To expand on this slightly:

 

The whole point of the Capability System is that you don't implement every interface on your

Entity

/

TileEntity

/

Item

, instead you store an instance of the interface and provide it from the

ICapabilityProvider

methods.

 

The Capability System is documented here.

 

do you have an example of this being done? im reading up on the documentation right now but id also like an example to follow along to see how someone else has done it for reference

Link to comment
Share on other sites

Well, it's not actually the implementation you are looking for, but it's using capabilities. It's basically a Solar Panel with a GUI for which I looked a bit into the Vanilla furnace code. Hopes this helps. ;)

 

http://www.minecraftforge.net/forum/index.php?topic=44428

http://www.minecraftforge.net/forum/index.php?topic=44438

Developer of Primeval Forest.

Link to comment
Share on other sites

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.