Jump to content

Recommended Posts

Posted

So i have some code that adds a NBT tag to a block when i pass it through a shapeless crafting reciepe

MastStack.setTagCompound(new NBTTagCompound());
        NBTTagCompound tags = MastStack.stackTagCompound;
        tags.setBoolean("Upright", true);
        GameRegistry.addShapelessRecipe(MastStack, new Object[] {MastUp,MastUp,MastUp});

however it doesn't preserve the data when it is placed as a block/tileentity. The data will be stored in the TE as i have set up several setters/getters and am already storing most of my data there anyway, however i have no idea how to pass the data form Item to TE

 

 

[glow=green,2,300]TEmast[/glow]

package ships.addon.blocks_items;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ChatComponentText;

public class TEmast extends TileEntity {
private byte Direction;
private byte Dye;
private boolean Upright;
private boolean Connected;
@Override
public AxisAlignedBB getRenderBoundingBox()
{
	return AxisAlignedBB.getBoundingBox(xCoord, yCoord, zCoord, xCoord + 1, yCoord + 1, zCoord + 1);
}
//---- Setters and Getters ----\\
public byte getValue() {
    return Direction;
}
public void setValue(byte value) {
    Direction = value;
}

public byte getDye() {
    return Dye;
}
public void setDye(byte value) {
	Dye = value;
}

public boolean getBoolean() {
    return Upright;
}
public void setBoolean(boolean value) {
	Upright = value;
}
public boolean getBool() {
    return Connected;
}
public void setBool(boolean value) {
	Connected = value;
}
//---- Read and Write ----\\
    public void writeToNBT(NBTTagCompound nbt){
    	super.writeToNBT(nbt);
    	nbt.setBoolean("Connected", this.Connected);
    	nbt.setBoolean("Upright", this.Upright);
    	nbt.setByte("Direction", this.Direction);
    	nbt.setByte("Dye", this.Dye);
        markForUpdate();
    }
    public void readFromNBT(NBTTagCompound nbt){
    	super.readFromNBT(nbt);
    	this.Connected = nbt.getBoolean("Connected");
      	this.Upright = nbt.getBoolean("Upright");
    	this.Direction = nbt.getByte("Direction");
        this.Dye = nbt.getByte("Dye");      	
    }
    
    //---- Packets ----\\
    @Override
    public Packet getDescriptionPacket(){
    	NBTTagCompound tileTag = new NBTTagCompound();
    	this.writeToNBT(tileTag);
    	return new S35PacketUpdateTileEntity(this.xCoord,this.yCoord,this.zCoord,0,tileTag);   	
    }
@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
{
	this.readFromNBT(pkt.func_148857_g());
}

//---- Mark for Update ----\\
public void markForUpdate() {
    this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
}

[glow=red,2,300]Crafting with the For loops and Items stacks[/glow]

for(int i=0; i<12; i++){
		//Iterate Item Stacks
		ItemStack MastStack = new ItemStack(Registry.Mast, 3, i);
		ItemStack MastUp = new ItemStack(Registry.Mast, 1, i);
		ItemStack WoodStack = new ItemStack(Blocks.log, 3, 0);
		if(i<4){WoodStack = new ItemStack(Blocks.log, 3, i);}
		else if(i<6){WoodStack = new ItemStack(Blocks.log2, 3, i-4);}
		else{WoodStack = new ItemStack(Blocks.planks, 3, i-6);}			
	GameRegistry.addShapedRecipe(MastStack, new Object[]{
		"LLL",
		"WWW",
		'L',WoodStack,'W',Registry.Sail
	});
	MastStack.setTagCompound(new NBTTagCompound());
        NBTTagCompound tags = MastStack.stackTagCompound;
        tags.setBoolean("Upright", true);
        GameRegistry.addShapelessRecipe(MastStack, new Object[] {MastUp,MastUp,MastUp});
	}

Posted

To do this you need to create own item block for this block, to override on item use, to pass nbt to te...

Thanks, where would i find an example of this? Cuase i already have a ItemBlock however i am not sure how to use

onItemUse()

[glow=red,2,300]ItemBlockMast[/glow]

public class ItemMast extends ItemBlock{
private final Block Mast;
public ItemMast(Block block) {
	super(block);
	this.Mast = block;
	this.setHasSubtypes(true);
}
public String getUnlocalizedName(ItemStack item){
	int i = item.getItemDamage();
	if(i<0||i>=BlockMast.subBlocks.length){
		i=i-BlockMast.subBlocks.length;
	}
	return super.getUnlocalizedName()+"."+BlockMast.subBlocks[i];
}
public int getMetadata(int meta){
	return meta;
}
    @SideOnly(Side.CLIENT)
    public IIcon getIconFromDamage(int meta)
    {
    	return this.Mast.getIcon(0, meta);
    }
}

 

Yes Im in 1.7.10

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.