Jump to content

TileEntity inventory without using IInventory?


memcallen

Recommended Posts

I don't know if this is the correct way to store items in nbt but heres my code:

package com.example.gammacraft.TileEntity;

import net.minecraft.item.ItemStack;
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.tileentity.TileEntity;

public class MultiToolModifierTileEntity extends TileEntity {

public ItemStack[] inv;

public MultiToolModifierTileEntity() {
	inv=new ItemStack[10];
}

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());
}

public void writeToNBT(NBTTagCompound nbt){

	for(int i =1;i==inv.length;){
		NBTTagCompound NBT = new NBTTagCompound();
		inv[i].writeToNBT(NBT);
		nbt.setTag("ItemStack"+i,NBT);
		nbt.setTag("ItemStackData"+i, inv[i].stackTagCompound);
	}


}

public void readFromNBT(NBTTagCompound nbt){

	for(int i =1;i==inv.length;){
		NBTTagCompound itemstack = nbt.getCompoundTag("ItemStack"+i);
		NBTTagCompound data = nbt.getCompoundTag("ItemStackData"+i);

		inv[i]=ItemStack.loadItemStackFromNBT(itemstack);
		inv[i].stackTagCompound=data;

	}

}

}

 

I currently have it take the item in my hand and it puts the item in the ItemStack[] "inv"- thats the part that works. I can't make it save to NBT, for whatever reason. If you were wondering why I'm not using IInventory, it's because I don't want this block to be able to be automated-its my version of a crafting table.

The proud(ish) developer of Ancients

Link to comment
Share on other sites

Do you know how to loop?  At all?

 

This line is wrong, very very wrong.

 

for(int i =1;i==inv.length;)

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

for(a; b; c) { d }

is the same thing as

a;
while(b); {
  d;
  c;
}

 

Hint: where are  you changing the value of i?

 

Edit:

Whoops.  Made a mistake late last night.

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

whatever, I changed the code to this:

package com.example.gammacraft.TileEntity;

import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
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.tileentity.TileEntity;
import net.minecraft.world.World;

public class MultiToolModifierTileEntity extends TileEntity {

public ItemStack[] inv;

public MultiToolModifierTileEntity() {
	inv=new ItemStack[10];
}

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());
}

public void writeToNBT(NBTTagCompound nbt){

	for(int i =1;i==inv.length;i++){
		NBTTagCompound NBT = new NBTTagCompound();
		inv[i].writeToNBT(NBT);
		nbt.setTag("ItemStack"+i,NBT);
		nbt.setTag("ItemStackData"+i, inv[i].stackTagCompound);
	}


}

public void readFromNBT(NBTTagCompound nbt){

	for(int i =1;i==inv.length;i++){
		NBTTagCompound itemstack = nbt.getCompoundTag("ItemStack"+i);
		NBTTagCompound data = nbt.getCompoundTag("ItemStackData"+i);

		inv[i]=ItemStack.loadItemStackFromNBT(itemstack);
		inv[i].stackTagCompound=data;

	}

}

}

 

but it still doesn't save the stack.

The proud(ish) developer of Ancients

Link to comment
Share on other sites

Oh, I though the i++ was optional0

 

Optional in the sense that if you don't have it, the program can still function...for some definition of function.  It is possible to write a for loop thus:

 

for(;{
    //code
}

 

But you kind of have to know WTF you're doing to use it that way.  Also, the missing i++ is not your only problem.

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

ok I'm getting somewhere, I changed the for loops to for(int i = 0;i!=inv.length+1;i++){//do stuff}, and I added a try/catch around a line in the read func, but I'm getting an error with the function "func_150298_a" I googled it and I'm writing an  nbt tag to itself, but I can't see where or how. btw there is no error showing me where the line is thats causing the error.

 

stacktrace:

java.lang.NullPointerException
at net.minecraft.nbt.NBTTagCompound.func_150298_a(NBTTagCompound.java:309)
at net.minecraft.nbt.NBTTagCompound.write(NBTTagCompound.java:23)
at net.minecraft.nbt.NBTTagList.write(NBTTagList.java:35)
at net.minecraft.nbt.NBTTagCompound.func_150298_a(NBTTagCompound.java:314)
at net.minecraft.nbt.NBTTagCompound.write(NBTTagCompound.java:23)
at net.minecraft.nbt.NBTTagCompound.func_150298_a(NBTTagCompound.java:314)
at net.minecraft.nbt.NBTTagCompound.write(NBTTagCompound.java:23)
at net.minecraft.nbt.CompressedStreamTools.func_150663_a(CompressedStreamTools.java:146)
at net.minecraft.nbt.CompressedStreamTools.write(CompressedStreamTools.java:136)
at net.minecraft.world.chunk.storage.AnvilChunkLoader.writeChunkNBTTags(AnvilChunkLoader.java:197)
at net.minecraft.world.chunk.storage.AnvilChunkLoader.writeNextIO(AnvilChunkLoader.java:183)
at net.minecraft.world.storage.ThreadedFileIOBase.processQueue(ThreadedFileIOBase.java:30)
at net.minecraft.world.storage.ThreadedFileIOBase.run(ThreadedFileIOBase.java:23)
at java.lang.Thread.run(Unknown Source)

 

This doesn't crash me but I have no idea how to fix it.

 

EDIT:

the error only gets thrown when I take the item out of my block.

The proud(ish) developer of Ancients

Link to comment
Share on other sites

Good luck sorting through the onBlockActivated code...

 

package com.example.gammacraft.blocks.Machines;

import com.example.gammacraft.RandomFunctions;
import com.example.gammacraft.gammacraft;
import com.example.gammacraft.TileEntity.MultiToolModifierTileEntity;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

public class MultiToolModifier extends BlockContainer {

public MultiToolModifier() {
	super(Material.iron);
	this.setBlockBounds(0F, 0F, 0F, 1F, 0.25F, 1F);
	this.setBlockName("MultiToolModifier");
	this.setCreativeTab(gammacraft.gammacraftcreativetab);
}

float pixel = 1F/16F;

@Override
public boolean onBlockActivated(World world,int x,int y,int z,EntityPlayer player,int side,float hx,float hy,float hz){
	MultiToolModifierTileEntity te = (MultiToolModifierTileEntity)world.getTileEntity(x, y, z);

		if(side==1){
			//slot 1
			if(withinBounds(hx,pixel*2,pixel*4)&&withinBounds(hz,pixel*4,pixel*6)){
				if(!world.isRemote)
				System.out.println("Button 1 is pressed");

				if(te.inv[0]!=null){
					player.inventory.mainInventory[player.inventory.currentItem]=te.inv[0];
					te.inv[0]=null;
				}else{
					te.inv[0]=player.inventory.mainInventory[player.inventory.currentItem];
					player.inventory.mainInventory[player.inventory.currentItem]=null;
				}

				return true;
			}
			//slot 2
			if(withinBounds(hx,pixel*2,pixel*4)&&withinBounds(hz,pixel*7,pixel*9)){
				if(!world.isRemote)
					System.out.println("Button 2 is pressed");

				if(te.inv[1]!=null){
					player.inventory.mainInventory[player.inventory.currentItem]=te.inv[1];
					te.inv[1]=null;
				}else{
					te.inv[1]=player.inventory.mainInventory[player.inventory.currentItem];
					player.inventory.mainInventory[player.inventory.currentItem]=null;
				}


					return true;
			}
			//slot 3
			if(withinBounds(hx,pixel*2,pixel*4)&&withinBounds(hz,pixel*10,pixel*12)){
				if(!world.isRemote)
					System.out.println("Button 3 is pressed");

				if(te.inv[2]!=null){
					player.inventory.mainInventory[player.inventory.currentItem]=te.inv[2];
					te.inv[2]=null;
				}else{
					te.inv[2]=player.inventory.mainInventory[player.inventory.currentItem];
					player.inventory.mainInventory[player.inventory.currentItem]=null;
				}

					return true;
			}
			//slot 4
			if(withinBounds(hx,pixel*5,pixel*7)&&withinBounds(hz,pixel*4,pixel*6)){
				if(!world.isRemote)
					System.out.println("Button 4 is pressed");

				if(te.inv[3]!=null){
					player.inventory.mainInventory[player.inventory.currentItem]=te.inv[3];
					te.inv[3]=null;
				}else{
					te.inv[3]=player.inventory.mainInventory[player.inventory.currentItem];
					player.inventory.mainInventory[player.inventory.currentItem]=null;
				}

					return true;
			}
			//slot 5
			if(withinBounds(hx,pixel*5,pixel*7)&&withinBounds(hz,pixel*7,pixel*9)){
				if(!world.isRemote)
					System.out.println("Button 5 is pressed");

				if(te.inv[4]!=null){
					player.inventory.mainInventory[player.inventory.currentItem]=te.inv[4];
					te.inv[4]=null;
				}else{
					te.inv[4]=player.inventory.mainInventory[player.inventory.currentItem];
					player.inventory.mainInventory[player.inventory.currentItem]=null;
				}

					return true;
			}
			//slot 6
			if(withinBounds(hx,pixel*5,pixel*7)&&withinBounds(hz,pixel*10,pixel*12)){
				if(!world.isRemote)
					System.out.println("Button 6 is pressed");

				if(te.inv[5]!=null){
					player.inventory.mainInventory[player.inventory.currentItem]=te.inv[5];
					te.inv[5]=null;
				}else{
					te.inv[5]=player.inventory.mainInventory[player.inventory.currentItem];
					player.inventory.mainInventory[player.inventory.currentItem]=null;
				}

					return true;
			}
			//slot 7
			if(withinBounds(hx,pixel*8,pixel*10)&&withinBounds(hz,pixel*4,pixel*6)){
				if(!world.isRemote)
					System.out.println("Button 7 is pressed");

				if(te.inv[6]!=null){
					player.inventory.mainInventory[player.inventory.currentItem]=te.inv[6];
					te.inv[6]=null;
				}else{
					te.inv[6]=player.inventory.mainInventory[player.inventory.currentItem];
					player.inventory.mainInventory[player.inventory.currentItem]=null;
				}

					return true;
			}
			//slot 8
			if(withinBounds(hx,pixel*8,pixel*10)&&withinBounds(hz,pixel*7,pixel*9)){
				if(!world.isRemote)
					System.out.println("Button 8 is pressed");

				if(te.inv[7]!=null){
					player.inventory.mainInventory[player.inventory.currentItem]=te.inv[7];
					te.inv[7]=null;
				}else{
					te.inv[7]=player.inventory.mainInventory[player.inventory.currentItem];
					player.inventory.mainInventory[player.inventory.currentItem]=null;
				}

					return true;
			}
			//slot 9
			if(withinBounds(hx,pixel*8,pixel*10)&&withinBounds(hz,pixel*10,pixel*12)){
				if(!world.isRemote)
					System.out.println("Button 9 is pressed");

				if(te.inv[8]!=null){
					player.inventory.mainInventory[player.inventory.currentItem]=te.inv[8];
					te.inv[8]=null;
				}else{
					te.inv[8]=player.inventory.mainInventory[player.inventory.currentItem];
					player.inventory.mainInventory[player.inventory.currentItem]=null;
				}

					return true;
			}
			//slot 10 (output)
			if(withinBounds(hx,pixel*12,pixel*14)&&withinBounds(hz,pixel*7,pixel*9)){
				if(!world.isRemote)
					System.out.println("Button 10 is pressed");

				if(te.inv[9]!=null){
					player.inventory.mainInventory[player.inventory.currentItem]=te.inv[9];
					te.inv[9]=null;
				}

					return true;
			}
		}

	return false;
}




@Override
public TileEntity createNewTileEntity(World var1, int var2) {
	return new MultiToolModifierTileEntity();
}

@SideOnly(Side.CLIENT)
public static IIcon sideIcon;
@SideOnly(Side.CLIENT)
public static IIcon bottom;
@SideOnly(Side.CLIENT)
public static IIcon top;

public void registerBlockIcons(IIconRegister i){
	sideIcon = i.registerIcon("gammacraft:FireJetSide");
	top = i.registerIcon("gammacraft:MultiToolModifierTop");
}

public IIcon getIcon(int side, int meta){
	if(side==1)return top;
	else return sideIcon;
}


public boolean withinBounds(float x, float min,float max){
	return x>min&&x<max;
}
}

 

EDIT:

you're lucky I decided to make a youtube video on how it works in the world. Should make it easier to figure out how it works.

The proud(ish) developer of Ancients

Link to comment
Share on other sites

...you're going to need to figure out for loops before you can even get to GL11 operations.

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

I don't even care anymore, I figured out for loops. I figured out GUIs I haven't figured out where to render it. I know how to render it. I've used GL11 and the tessellators before. Honestly, this topic isn't about for loops its about ItemStacks not saving in nbt, so please either answer my first question or don't answer at all.

The proud(ish) developer of Ancients

Link to comment
Share on other sites

this topic isn't about for loops its about ItemStacks not saving in nbt

 

Your items aren't saving because your for loop is fucked up.

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

I see a few of problems,

 

You're storing the ItemStack's NBTTagCompound twice.

You never call markDirty()

You never do null checks.

You do a lot of stuff on both Sides, which can cause one Side to have the wrong information.

You are setting the player's held Item, without making sure it's empty first.

Link to comment
Share on other sites

This is the Itemstack writeToNBT method:

 

    /**
     * Write the stack fields to a NBT object. Return the new NBT object.
     */
    public NBTTagCompound writeToNBT(NBTTagCompound p_77955_1_)
    {
        p_77955_1_.setShort("id", (short)Item.getIdFromItem(this.field_151002_e));
        p_77955_1_.setByte("Count", (byte)this.stackSize);
        p_77955_1_.setShort("Damage", (short)this.itemDamage);

        if (this.stackTagCompound != null)
        {
            p_77955_1_.setTag("tag", this.stackTagCompound);
        }

        return p_77955_1_;
    }

 

It checks and stores the stackTagCompound, which you store a second time in your writeToNBT:

 

			nbt.setTag("ItemStack"+i,NBT); // Stores the NBT.stackTagCompound Tag
		nbt.setTag("ItemStackData"+i, inv[i].stackTagCompound); // Duplicate Tag

 

 

 

markDirty() is a method on TileEntity. It ensures that the TileEntity is saved. Even if Minecraft would normally not save it, for example, when no blocks have changed.

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.