Jump to content

Recommended Posts

Posted

So I have a field called

private int steam

And I would like to get it on client side, but it is not synced.

What would I need to do to sync that field.

I'm writing it to NBT, and I have the PacketPipeline and the AbstractPacket classes as explained on the wiki.

Please answer it would help me a lot.

Sincerely Nicba1010!

Posted

You can use the getDescriptionPacket() and the onPacketData() method in the TileEntity class. In the getDescpriptioPacket method you need to return a S35UpdateTileEntityPacket(xCoord,yCoord,zCoord,1,YOUR_NBT_COMPOUND) and in the onPacketData method you readFromNBT the packet's NBTTagCompound.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

Soo I added this code but it still doesn't work.

//NEWCODE START
public Packet getDescriptionPacket() {
	NBTTagCompound nbtTag = new NBTTagCompound();
	this.writeToNBT(nbtTag);
	return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord,
			this.zCoord, 1, nbtTag);
}

public void onDataPacket(NetworkManager net,
		S35PacketUpdateTileEntity packet) {
	readFromNBT(packet.func_148857_g());
}
//NEWCODE END
public void readFromNBT(NBTTagCompound tagCompound) {
	super.readFromNBT(tagCompound);
	NBTTagList nbttaglist = tagCompound.getTagList("Items", 10);
	this.furnaceItemStacks = new ItemStack[this.getSizeInventory()];

	for (int i = 0; i < nbttaglist.tagCount(); ++i) {
		NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
		byte b0 = nbttagcompound1.getByte("Slot");

		if (b0 >= 0 && b0 < this.furnaceItemStacks.length) {
			this.furnaceItemStacks[b0] = ItemStack
					.loadItemStackFromNBT(nbttagcompound1);
		}
	}

	this.furnaceBurnTime = tagCompound.getShort("BurnTime");
	this.furnaceCookTime = tagCompound.getShort("CookTime");
	this.currentItemBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
	this.steam = tagCompound.getInteger("steam");

	if (tagCompound.hasKey("CustomName", ) {
		this.customInventoryName = tagCompound.getString("CustomName");
	}
}

public void writeToNBT(NBTTagCompound tagCompound) {
	super.writeToNBT(tagCompound);
	tagCompound.setShort("BurnTime", (short) this.furnaceBurnTime);
	tagCompound.setShort("CookTime", (short) this.furnaceCookTime);
	tagCompound.setInteger("Steam", this.steam);
	NBTTagList nbttaglist = new NBTTagList();

	for (int i = 0; i < this.furnaceItemStacks.length; ++i) {
		if (this.furnaceItemStacks[i] != null) {
			NBTTagCompound nbttagcompound1 = new NBTTagCompound();
			nbttagcompound1.setByte("Slot", (byte) i);
			this.furnaceItemStacks[i].writeToNBT(nbttagcompound1);
			nbttaglist.appendTag(nbttagcompound1);
		}
	}

	tagCompound.setTag("Items", nbttaglist);

	if (this.hasCustomInventoryName()) {
		tagCompound.setString("CustomName", this.customInventoryName);
	}
}

Posted

Try this

@Override
public Packet getDescriptionPacket()
{
NBTTagCompound tagCompound = new NBTTagCompound();
this.writeToNBT(tagCompound);
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, tagCompound);
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
{
readFromNBT(pkt.func_148857_g());
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}

I am the author of Draconic Evolution

Posted

For your packet description, I noticed a 1 where i have a zero.  You might check the significance of that.  Also, you should start using @Override to make sure you didn't type a name or something wrong.  In what I shared, it is set to only accept packets from the server to the client.  That was just based on what I was using it for.

 

 

 

 

    @Override

    public Packet getDescriptionPacket() {

   

        // Build NBT Tag

        NBTTagCompound tag = new NBTTagCompound();

        writeToNBT(tag);

       

        // Write the tage

        return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 0, tag);

       

    }

 

    @Override

    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {

    //System.out.println("Spawner Tile Packet Detected");

   

        // Check to see if client

        if (worldObj.isRemote) {

        //System.out.println("  - client detected");

       

            // Tell the tile to read in the new data

        NBTTagCompound tag = packet.func_148857_g();

            readFromNBT(tag);

           

        }

       

    }

 

 

 

 

 

Could you describe what isn't working.  Have you put log markers in to see what part of the code actually happens versus what doesn't?

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

Still doesn't work :(

Whole TE Code

package industrialage.core.tileentitiy;

import industrialage.core.recipes.SteamGeneratorRecipes;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemHoe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemTool;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class TileEntitySteamGenerator extends TileEntity implements
	ISidedInventory {
/**
 * The ItemStacks that hold the items currently being used in the furnace
 */
private ItemStack[] generatorItemStacks = new ItemStack[3];
/** The number of ticks that the furnace will keep burning */
public int generatorBurnTime;
/**
 * The number of ticks that a fresh copy of the currently-burning item would
 * keep the furnace burning for
 */
public int currentItemBurnTime;
/** The number of ticks that the current item has been cooking for */
public int generatorCookTime;
private String customInventoryName;
int cookRate = 1, fuelBurnRate = 1;
private int steam = 0;

/**
 * Returns the number of slots in the inventory.
 */
public int getSizeInventory() {
	return this.generatorItemStacks.length;
}

/**
 * Returns the stack in slot i
 */
public ItemStack getStackInSlot(int par1) {
	return this.generatorItemStacks[par1];
}

/**
 * Removes from an inventory slot (first arg) up to a specified number
 * (second arg) of items and returns them in a new stack.
 */
public ItemStack decrStackSize(int slot, int number) {
	if (this.generatorItemStacks[slot] != null) {
		ItemStack itemstack;
		if (this.generatorItemStacks[slot].stackSize <= number) {
			itemstack = this.generatorItemStacks[slot];
			this.generatorItemStacks[slot] = null;
			return itemstack;
		} else {
			itemstack = this.generatorItemStacks[slot].splitStack(number);

			if (this.generatorItemStacks[slot].stackSize == 0) {
				this.generatorItemStacks[slot] = null;
			}

			return itemstack;
		}
	} else {
		return null;
	}
}

/**
 * When some containers are closed they call this on each slot, then drop
 * whatever it returns as an EntityItem - like when you close a workbench
 * GUI.
 */
public ItemStack getStackInSlotOnClosing(int par1) {
	if (this.generatorItemStacks[par1] != null) {
		ItemStack itemstack = this.generatorItemStacks[par1];
		this.generatorItemStacks[par1] = null;
		return itemstack;
	} else {
		return null;
	}
}

/**
 * Sets the given item stack to the specified slot in the inventory (can be
 * crafting or armor sections).
 */
public void setInventorySlotContents(int par1, ItemStack par2ItemStack) {
	this.generatorItemStacks[par1] = par2ItemStack;

	if (par2ItemStack != null
			&& par2ItemStack.stackSize > this.getInventoryStackLimit()) {
		par2ItemStack.stackSize = this.getInventoryStackLimit();
	}
}

/**
 * Returns the name of the inventory
 */
public String getInventoryName() {
	return this.hasCustomInventoryName() ? this.customInventoryName
			: "container.furnace";
}

/**
 * Returns if the inventory is named
 */
public boolean hasCustomInventoryName() {
	return this.customInventoryName != null
			&& this.customInventoryName.length() > 0;
}

public void setCustomInventoryName(String name) {
	this.customInventoryName = name;
}

@Override
public Packet getDescriptionPacket() {
	NBTTagCompound tag = new NBTTagCompound();
	writeToNBT(tag);
	return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 0, tag);

}

@Override
public void onDataPacket(NetworkManager net,
		S35PacketUpdateTileEntity packet) {
	NBTTagCompound tag = packet.func_148857_g();
	readFromNBT(tag);
}

public void readFromNBT(NBTTagCompound tagCompound) {
	NBTTagList nbttaglist = tagCompound.getTagList("Items", 10);
	this.generatorItemStacks = new ItemStack[this.getSizeInventory()];

	for (int i = 0; i < nbttaglist.tagCount(); ++i) {
		NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
		byte b0 = nbttagcompound1.getByte("Slot");

		if (b0 >= 0 && b0 < this.generatorItemStacks.length) {
			this.generatorItemStacks[b0] = ItemStack
					.loadItemStackFromNBT(nbttagcompound1);
		}
	}

	this.generatorBurnTime = tagCompound.getShort("BurnTime");
	this.generatorCookTime = tagCompound.getShort("CookTime");
	this.currentItemBurnTime = getItemBurnTime(this.generatorItemStacks[1]);
	this.steam = tagCompound.getInteger("steam");

	if (tagCompound.hasKey("CustomName", ) {
		this.customInventoryName = tagCompound.getString("CustomName");
	}
	super.readFromNBT(tagCompound);
}

public void writeToNBT(NBTTagCompound tagCompound) {
	tagCompound.setShort("BurnTime", (short) this.generatorBurnTime);
	tagCompound.setShort("CookTime", (short) this.generatorCookTime);
	tagCompound.setInteger("Steam", this.steam);
	NBTTagList nbttaglist = new NBTTagList();

	for (int i = 0; i < this.generatorItemStacks.length; ++i) {
		if (this.generatorItemStacks[i] != null) {
			NBTTagCompound nbttagcompound1 = new NBTTagCompound();
			nbttagcompound1.setByte("Slot", (byte) i);
			this.generatorItemStacks[i].writeToNBT(nbttagcompound1);
			nbttaglist.appendTag(nbttagcompound1);
		}
	}

	tagCompound.setTag("Items", nbttaglist);

	if (this.hasCustomInventoryName()) {
		tagCompound.setString("CustomName", this.customInventoryName);
	}
	super.writeToNBT(tagCompound);
}

/**
 * Returns the maximum stack size for a inventory slot.
 */
public int getInventoryStackLimit() {
	return 64;
}

/**
 * Returns an integer between 0 and the passed value representing how close
 * the current item is to being completely cooked
 */
@SideOnly(Side.CLIENT)
public int getCookProgressScaled(int max) {
	return this.generatorCookTime * max / (200 / fuelBurnRate);
}

@SideOnly(Side.CLIENT)
public int getSteam() {
	return this.steam;
}

/**
 * Returns an integer between 0 and the passed value representing how much
 * burn time is left on the current fuel item, where 0 means that the item
 * is exhausted and the passed value means that the item is fresh
 */
@SideOnly(Side.CLIENT)
public int getBurnTimeRemainingScaled(int max) {
	if (this.currentItemBurnTime == 0) {
		this.currentItemBurnTime = (200 / cookRate);
	}

	return this.generatorBurnTime * max / this.currentItemBurnTime;
}

/**
 * Furnace isBurning
 */
public boolean isBurning() {
	return this.generatorBurnTime > 0;
}

public void updateEntity() {
	boolean flag = this.generatorBurnTime > 0;
	boolean flag1 = false;
	if (!worldObj.isRemote)
		System.out.println(getSteam());// Returns real (server side) amount
										// e.g. 1000
	if (worldObj.isRemote)
		System.out.println(getSteam());// Returns 0
	if (this.generatorBurnTime > 0) {
		--this.generatorBurnTime;
	}
	if (!this.worldObj.isRemote) {
		if (this.generatorBurnTime == 0 && this.canSmelt()) {
			this.currentItemBurnTime = this.generatorBurnTime = getItemBurnTime(this.generatorItemStacks[1]);

			if (this.generatorBurnTime > 0) {
				flag1 = true;

				if (this.generatorItemStacks[1] != null) {
					--this.generatorItemStacks[1].stackSize;

					if (this.generatorItemStacks[1].stackSize == 0) {
						this.generatorItemStacks[1] = generatorItemStacks[1]
								.getItem().getContainerItem(
										generatorItemStacks[1]);
					}
				}
			}
		}

		if (this.isBurning() && this.canSmelt()) {
			++this.generatorCookTime;

			if (this.generatorCookTime == (200 / fuelBurnRate)) {
				this.generatorCookTime = 0;
				this.smeltItem();
				flag1 = true;
			}
		} else {
			this.generatorCookTime = 0;
		}

		if (flag != this.generatorBurnTime > 0) {
			flag1 = true;
			// BlockFurnace.updateFurnaceBlockState(this.furnaceBurnTime >
			// 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
		}
	}

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

/**
 * Returns true if the furnace can smelt an item, i.e. has a source item,
 * destination stack isn't full, etc.
 */
private boolean canSmelt() {
	if (this.generatorItemStacks[0] == null) {
		return false;
	} else {
		Integer amt = SteamGeneratorRecipes.smelting().getSmeltingResult(
				this.generatorItemStacks[0]);
		if (amt == null)
			return false;
		if (this.generatorItemStacks[2] != null)
			if (this.generatorItemStacks[2].stackSize == 16)
				return false;
		return true;
	}
}

/**
 * Turn one item from the furnace source stack into the appropriate smelted
 * item in the furnace result stack
 */
public void smeltItem() {
	if (this.canSmelt()) {
		Integer amt = SteamGeneratorRecipes.smelting().getSmeltingResult(
				this.generatorItemStacks[0]);
		ItemStack itemstack = new ItemStack(Items.bucket, 1);
		steam += amt;
		if (this.generatorItemStacks[2] == null) {
			this.generatorItemStacks[2] = itemstack.copy();
		} else if (this.generatorItemStacks[2].stackSize < 16) {
			this.generatorItemStacks[2].stackSize += itemstack.stackSize;
		}
		--this.generatorItemStacks[0].stackSize;

		if (this.generatorItemStacks[0].stackSize <= 0) {
			this.generatorItemStacks[0] = null;
		}
	}
}

/**
 * Returns the number of ticks that the supplied fuel item will keep the
 * furnace burning, or 0 if the item isn't fuel
 */
public static int getItemBurnTime(ItemStack fuel) {
	if (fuel == null) {
		return 0;
	} else {
		Item item = fuel.getItem();

		if (item instanceof ItemBlock
				&& Block.getBlockFromItem(item) != Blocks.air) {
			Block block = Block.getBlockFromItem(item);

			if (block == Blocks.wooden_slab) {
				return 150;
			}

			if (block.getMaterial() == Material.wood) {
				return 300;
			}

			if (block == Blocks.coal_block) {
				return 16000;
			}
		}

		if (item instanceof ItemTool
				&& ((ItemTool) item).getToolMaterialName().equals("WOOD"))
			return 200;
		if (item instanceof ItemSword
				&& ((ItemSword) item).getToolMaterialName().equals("WOOD"))
			return 200;
		if (item instanceof ItemHoe
				&& ((ItemHoe) item).getToolMaterialName().equals("WOOD"))
			return 200;
		if (item == Items.stick)
			return 100;
		if (item == Items.coal)
			return 1600;
		if (item == Items.lava_bucket)
			return 20000;
		if (item == Item.getItemFromBlock(Blocks.sapling))
			return 100;
		if (item == Items.blaze_rod)
			return 2400;
		return GameRegistry.getFuelValue(fuel);
	}
}

public static boolean isItemFuel(ItemStack item) {
	/**
	 * Returns the number of ticks that the supplied fuel item will keep the
	 * furnace burning, or 0 if the item isn't fuel
	 */
	return getItemBurnTime(item) > 0;
}

/**
 * Do not make give this method the name canInteractWith because it clashes
 * with Container
 */
public boolean isUseableByPlayer(EntityPlayer player) {
	return this.worldObj.getTileEntity(this.xCoord, this.yCoord,
			this.zCoord) != this ? false : player.getDistanceSq(
			(double) this.xCoord + 0.5D, (double) this.yCoord + 0.5D,
			(double) this.zCoord + 0.5D) <= 64.0D;
}

public void openInventory() {
}

public void closeInventory() {
}

/**
 * Returns true if automation is allowed to insert the given stack (ignoring
 * stack size) into the given slot.
 */
public boolean isItemValidForSlot(int slot, ItemStack item) {
	return slot == 2 ? false : (slot == 1 ? isItemFuel(item) : true);
}

@Override
public int[] getAccessibleSlotsFromSide(int var1) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public boolean canInsertItem(int var1, ItemStack var2, int var3) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean canExtractItem(int var1, ItemStack var2, int var3) {
	// TODO Auto-generated method stub
	return false;
}
}

Posted

It doesn't matter if the super calls are at the beginning or the end in this case.

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

I see the problem.

When you are writing the steam value you write it as "Steam" but when you try to read it you use "steam" see the problem? these strings are case sensitive.

 

Edit: ondata and getDescription should get called both when the world loads or when you use worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);

 

I am the author of Draconic Evolution

Posted

Put a print in the read/write nbt calls and the getdescription and onpackets calls.

 

Also some to the updateentity to tell where it exits and how.

 

Lastly, you didn't add enough @Override.  Add it to every single fuction you are overwriting to see if one of them pops up and error and tells you that class doesn't exists in tilenentity.

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

Yeah all Override tags good all methods overriden(the ones that are in the superclass).

Fixed the typo my bad :(;

But still doesnt explain why the readNbt doesnnt get called, but the write does, i put some debug prints there;

Posted

I was wondering WTH does that mean :)

It works thanks.

So how often should i call it, i suppose like when the server and client steam values dont match but how would i go about doing that.

Posted

You fire it anytime you change something on the server side.

 

Typicall that is based upon pushing a GUI button.  In your case anytime you update your Steam value or something else.

Long time Bukkit & Forge Programmer

Happy to try and help

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.