Jump to content

Recommended Posts

Posted

I'm trying to save 4 boolean values to nbt along with 4 items in a container. The items save just fine but when I reload all the booleans have been reset to false. Can I save these along with the items or will I need to use a custom packet?

 

TileEntity

package gmail.Lance5057.tileentities;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
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 net.minecraftforge.common.util.Constants;

public class TileEntity_CrestMount extends TileEntity implements IInventory
{
public static int invSize = 4;
public ItemStack[] inventory;
public boolean[] flip; 

private final String name = "Crest Inventory";

public TileEntity_CrestMount()
{
	super();
	inventory = new ItemStack[invSize];
	flip = new boolean[4];
}

@Override
public void updateEntity()
{
	super.updateEntity();
//		if (!worldObj.isRemote)
//		{

//			 getWorldObj().markBlockForUpdate(xCoord, yCoord, zCoord);
//			 markDirty();
//			
//		}
}

@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 pkt) {
    readFromNBT(pkt.func_148857_g());
}

@Override
public int getSizeInventory() {
	return invSize;
}

@Override
public ItemStack getStackInSlot(int slot) {
	return inventory[slot];
}

@Override
public ItemStack decrStackSize(int slot, int amount) {
	ItemStack stack = getStackInSlot(slot);
	if (stack != null)
	{
		if (stack.stackSize > amount)
		{
			stack = stack.splitStack(amount);

			if (stack.stackSize == 0)
			{
				setInventorySlotContents(slot, null);
			}
		}
		else
		{
			setInventorySlotContents(slot, null);
		}

		this.markDirty();
	}
	return stack;
}

@Override
public ItemStack getStackInSlotOnClosing(int slot) {
	ItemStack stack = getStackInSlot(slot);

	if (stack != null)
	{
		setInventorySlotContents(slot, stack);
	}
	return stack;
}

@Override
public void setInventorySlotContents(int slot, ItemStack itemstack) {
	this.inventory[slot] = itemstack;

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

	this.markDirty();		
}

@Override
public String getInventoryName() {
	return name;
}

@Override
public boolean hasCustomInventoryName() {
	return name.length() > 0;
}

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

@Override
public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
	return true;
}

@Override
public void openInventory() {

}

@Override
public void closeInventory() {

}

@Override
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
	return true;
}

@Override
public void writeToNBT(NBTTagCompound compound)
{
	super.writeToNBT(compound);
	if(flip!=null)
	{
	compound.setBoolean("flip_1", flip[0]);
	compound.setBoolean("flip_2", flip[1]);
	compound.setBoolean("flip_3", flip[2]);
	compound.setBoolean("flip_4", flip[3]);
	}

	writeInventoryToNBT(compound);	
}

@Override
 public void readFromNBT(NBTTagCompound compound) 
 {
	super.readFromNBT(compound);

	flip[0] = compound.getBoolean("flip_1");
	flip[1] = compound.getBoolean("flip_2");
	flip[2] = compound.getBoolean("flip_3");
	flip[3] = compound.getBoolean("flip_4");

		readInventoryFromNBT(compound);
	}

public void readInventoryFromNBT(NBTTagCompound tags) {
	NBTTagList nbttaglist = tags.getTagList("Items", Constants.NBT.TAG_COMPOUND);
	for (int iter = 0; iter < nbttaglist.tagCount(); iter++) {
		NBTTagCompound tagList = (NBTTagCompound) nbttaglist.getCompoundTagAt(iter);
		byte slotID = tagList.getByte("Slot");
		if (slotID >= 0 && slotID < inventory.length) {
			inventory[slotID] = ItemStack.loadItemStackFromNBT(tagList);
		}
	}
}


public void writeInventoryToNBT(NBTTagCompound tags) {
	NBTTagList nbttaglist = new NBTTagList();
	for (int iter = 0; iter < inventory.length; iter++) {
		if (inventory[iter] != null) {
			NBTTagCompound tagList = new NBTTagCompound();
			tagList.setByte("Slot", (byte) iter);
			inventory[iter].writeToNBT(tagList);
			nbttaglist.appendTag(tagList);
		}
	}

	tags.setTag("Items", nbttaglist);
}
}

 

GUI

package gmail.Lance5057.gui;

import gmail.Lance5057.com.mod_TinkersDefense;
import gmail.Lance5057.containers.Container_CrestMount;
import gmail.Lance5057.network.Message_CrestMount;
import gmail.Lance5057.tileentities.TileEntity_CrestMount;

import javax.swing.plaf.basic.BasicComboBoxUI.KeyHandler;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

public class Gui_CrestMount extends GuiContainer
{
private float xSize_lo;
private float ySize_lo;

private static final ResourceLocation iconLocation = new ResourceLocation("tinkersdefense", "textures/gui/crest_mount.png");

private final TileEntity_CrestMount inventory;

public Gui_CrestMount(InventoryPlayer invPlayer, TileEntity_CrestMount te_crest)
{
super(new Container_CrestMount(invPlayer, te_crest));
this.inventory = te_crest;


}

@Override
public void initGui()
{
super.initGui();
this.buttonList.add(new GuiButton(1,this.guiLeft + 61, this.guiTop + 10, 16, 16,"Flip"));
this.buttonList.add(new GuiButton(2,this.guiLeft + 16, this.guiTop + 16, 16, 16,"Flip"));
this.buttonList.add(new GuiButton(3,this.guiLeft + 16, this.guiTop + 32, 16, 16,"Flip"));
this.buttonList.add(new GuiButton(4,this.guiLeft + 16, this.guiTop + 48, 16, 16,"Flip"));
}

@Override
protected void actionPerformed(GuiButton button)
{
for(int i = 1; i<5; i++)
{
 if(button.id == i)
 {
	 if(inventory.flip[i-1]==false)
		 inventory.flip[i-1]=true;
	 else
		 inventory.flip[i-1]=false;

	 if (inventory.getWorldObj().isRemote)
		 inventory.getWorldObj().markBlockForUpdate(inventory.xCoord, inventory.yCoord, inventory.zCoord);
	 //mod_TinkersDefense.INSTANCE.sendToServer(new Message_CrestMount(inventory.xCoord, inventory.yCoord, inventory.zCoord, inventory.flip));
 }
}
}

public void drawScreen(int par1, int par2, float par3)
{
super.drawScreen(par1, par2, par3);
this.xSize_lo = (float)par1;
this.ySize_lo = (float)par2;
}

protected void drawGuiContainerForegroundLayer(int par1, int par2)
{

}

protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)
{
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(iconLocation);
int k = (this.width - this.xSize) / 2;
int l = (this.height - this.ySize) / 2;
this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
}
}

 

The booleans are changed via a GUI so I included that code as well.

Posted

//			 getWorldObj().markBlockForUpdate(xCoord, yCoord, zCoord);
//			 markDirty();

 

This part? When that's not commented and I click the button to flip it snaps it right back to false after being true for a split second.

Posted

Ok so that was the right direction... I was experimenting with that because I wasn't sure, that's why it was commented out. My issue now is when I click the gui button the server is terminated.

 

Error Log

[20:43:07] [server thread/ERROR] [FML]: FMLIndexedMessageCodec exception caught
io.netty.handler.codec.DecoderException: java.lang.NullPointerException
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:99) ~[MessageToMessageDecoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) ~[MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) [DefaultChannelPipeline.class:?]
at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) [EmbeddedChannel.class:?]
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86) [FMLProxyPacket.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) [integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
Caused by: java.lang.NullPointerException
at gmail.Lance5057.network.Message_CrestMount.fromBytes(Message_CrestMount.java:44) ~[Message_CrestMount.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.decodeInto(SimpleIndexedCodec.java:17) ~[simpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.decodeInto(SimpleIndexedCodec.java:7) ~[simpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:77) ~[FMLIndexedMessageToMessageCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:17) ~[FMLIndexedMessageToMessageCodec.class:?]
at io.netty.handler.codec.MessageToMessageCodec$2.decode(MessageToMessageCodec.java:81) ~[MessageToMessageCodec$2.class:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:89) ~[MessageToMessageDecoder.class:?]
... 13 more
[20:43:07] [server thread/ERROR] [FML]: SimpleChannelHandlerWrapper exception
io.netty.handler.codec.DecoderException: java.lang.NullPointerException
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:99) ~[MessageToMessageDecoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) ~[MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) [DefaultChannelPipeline.class:?]
at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) [EmbeddedChannel.class:?]
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86) [FMLProxyPacket.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) [integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
Caused by: java.lang.NullPointerException
at gmail.Lance5057.network.Message_CrestMount.fromBytes(Message_CrestMount.java:44) ~[Message_CrestMount.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.decodeInto(SimpleIndexedCodec.java:17) ~[simpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.decodeInto(SimpleIndexedCodec.java:7) ~[simpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:77) ~[FMLIndexedMessageToMessageCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:17) ~[FMLIndexedMessageToMessageCodec.class:?]
at io.netty.handler.codec.MessageToMessageCodec$2.decode(MessageToMessageCodec.java:81) ~[MessageToMessageCodec$2.class:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:89) ~[MessageToMessageDecoder.class:?]
... 13 more
[20:43:07] [server thread/ERROR] [FML]: There was a critical exception handling a packet on channel tinkersdefense
io.netty.handler.codec.DecoderException: java.lang.NullPointerException
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:99) ~[MessageToMessageDecoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) ~[MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) ~[DefaultChannelPipeline.class:?]
at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) ~[EmbeddedChannel.class:?]
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86) [FMLProxyPacket.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) [integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
Caused by: java.lang.NullPointerException
at gmail.Lance5057.network.Message_CrestMount.fromBytes(Message_CrestMount.java:44) ~[Message_CrestMount.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.decodeInto(SimpleIndexedCodec.java:17) ~[simpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.decodeInto(SimpleIndexedCodec.java:7) ~[simpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:77) ~[FMLIndexedMessageToMessageCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:17) ~[FMLIndexedMessageToMessageCodec.class:?]
at io.netty.handler.codec.MessageToMessageCodec$2.decode(MessageToMessageCodec.java:81) ~[MessageToMessageCodec$2.class:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:89) ~[MessageToMessageDecoder.class:?]
... 13 more

 

Message Class

package gmail.Lance5057.network;

import gmail.Lance5057.tileentities.TileEntity_CrestMount;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.tileentity.TileEntity;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;

public class Message_CrestMount implements IMessage, IMessageHandler<Message_CrestMount, IMessage> 
{
public int x, y, z;
public boolean[] flip;

public Message_CrestMount()
{

}

public Message_CrestMount(int x, int y, int z, boolean[] flip)
{
	this.x = x;
	this.y = y;
	this.z = z;
	this.flip = flip;
}


@Override
public IMessage onMessage(Message_CrestMount message, MessageContext ctx) {
	TileEntity te = Minecraft.getMinecraft().theWorld.getTileEntity(message.x,message.y,message.z);
	if (te instanceof TileEntity_CrestMount) {
		((TileEntity_CrestMount) te).flip = message.flip;
		}
	return null;
}

@Override
public void fromBytes(ByteBuf buf) {
	this.x = buf.readInt();
	this.y = buf.readInt();
	this.z = buf.readInt();
	for(int i = 0; i<flip.length; i++)
		{
			this.flip[i] = buf.readBoolean();
		}
}

@Override
public void toBytes(ByteBuf buf) {
	buf.writeInt(x);
	buf.writeInt(y);
	buf.writeInt(z);
	for(int i = 0; i<flip.length; i++)
	{
		buf.writeBoolean(flip[i]);
	}
}

}

 

Packet Handler

package gmail.Lance5057.network;

import gmail.Lance5057.com.mod_TinkersDefense;
import cpw.mods.fml.relauncher.Side;

public class PacketHandler {
private static int id = 0;

public static void init()
{
mod_TinkersDefense.INSTANCE.registerMessage(Message_CrestMount.class, Message_CrestMount.class,id++,Side.SERVER);

}
}

Posted

I changed it so flip is no longer null but it didn't fix it. Also 'flip is sent with the message already.

Edited Message

package gmail.Lance5057.network;

import gmail.Lance5057.tileentities.TileEntity_CrestMount;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.tileentity.TileEntity;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;

public class Message_CrestMount implements IMessage, IMessageHandler<Message_CrestMount, IMessage> 
{
public int x, y, z;
public boolean[] flip = new boolean[4];

public Message_CrestMount()
{

}

public Message_CrestMount(int x, int y, int z, boolean[] flip)
{
	this.x = x;
	this.y = y;
	this.z = z;
	System.arraycopy(flip, 0, this.flip, 0, flip.length);

}


@Override
public IMessage onMessage(Message_CrestMount message, MessageContext ctx) {
	TileEntity te = Minecraft.getMinecraft().theWorld.getTileEntity(message.x,message.y,message.z);
	if (te instanceof TileEntity_CrestMount) {
		((TileEntity_CrestMount) te).flip = message.flip;
		}
	return null;
}

@Override
public void fromBytes(ByteBuf buf) {
	this.x = buf.readInt();
	this.y = buf.readInt();
	this.z = buf.readInt();
	for(int i = 0; i<flip.length; i++)
		{
			this.flip[i] = buf.readBoolean();
		}
}

@Override
public void toBytes(ByteBuf buf) {
	buf.writeInt(x);
	buf.writeInt(y);
	buf.writeInt(z);
	for(int i = 0; i<flip.length; i++)
	{
		buf.writeBoolean(flip[i]);
	}
}

}

Posted

It finally works! I separated the IMessage and IMessageHandler and changed this line

 

TileEntity te = Minecraft.getMinecraft().theWorld.getTileEntity(message.x,message.y,message.z);

 

to

 

		TileEntity te = ctx.getServerHandler().playerEntity.worldObj.getTileEntity(message.x, message.y, message.z);

 

Thank you for the 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.