Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

TileEntity inventory without using IInventory?


memcallen
 Share

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

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
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.

 Share



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • [11:22:34] [main/INFO]: ModLauncher running: args [--username, Amaru782, --version, forge-36.2.19, --gameDir, C:\Users\usuario\curseforge\minecraft\Instances\Better Minecraft [FORGE] - 1.16.5, --assetsDir, C:\Users\usuario\curseforge\minecraft\Install\assets, --assetIndex, 1.16, --uuid, dc27f25f4a0349eca32fbd0abdd36c7c, --accessToken, ????????, --userType, msa, --versionType, release, --width, 1366, --height, 768, --launchTarget, fmlclient, --fml.forgeVersion, 36.2.19, --fml.mcVersion, 1.16.5, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20210115.111550] [11:22:34] [main/INFO]: ModLauncher 8.0.9+86+master.3cf110c starting: java version 1.8.0_51 by Oracle Corporation [11:22:35] [main/WARN]: LEGACY JDK DETECTED, SECURED JAR HANDLING DISABLED [11:22:36] [main/INFO]: Added Lets Encrypt root certificates as additional trust [11:22:36] [main/INFO]: SpongePowered MIXIN Subsystem Version=0.8.4 Source=file:/C:/Users/usuario/curseforge/minecraft/Install/libraries/org/spongepowered/mixin/0.8.4/mixin-0.8.4.jar Service=ModLauncher Env=CLIENT [11:22:36] [main/WARN]: Configuration file C:\Users\usuario\curseforge\minecraft\Instances\Better Minecraft [FORGE] - 1.16.5\config\fml.toml is not correct. Correcting [11:22:36] [main/WARN]: Incorrect key [defaultConfigPath] was corrected from null to defaultconfigs [11:22:37] [main/FATAL]: Failed to find Minecraft resource version 1.16.5-20210115.111550 at C:\Users\usuario\curseforge\minecraft\Install\libraries\net\minecraftforge\forge\1.16.5-36.2.19\forge-1.16.5-36.2.19-client.jar [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]: java.lang.RuntimeException: Missing minecraft resource! [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at net.minecraftforge.fml.loading.FMLCommonLaunchHandler.lambda$validatePaths$4(FMLCommonLaunchHandler.java:124) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at net.minecraftforge.fml.loading.FMLCommonLaunchHandler$$Lambda$170/292641216.accept(Unknown Source) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at net.minecraftforge.fml.loading.FMLCommonLaunchHandler.validatePaths(FMLCommonLaunchHandler.java:121) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at net.minecraftforge.fml.loading.FMLLoader.setupLaunchHandler(FMLLoader.java:202) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at net.minecraftforge.fml.loading.FMLServiceProvider.initialize(FMLServiceProvider.java:94) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.TransformationServiceDecorator.onInitialize(TransformationServiceDecorator.java:68) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.TransformationServicesHandler.lambda$initialiseTransformationServices$7(TransformationServicesHandler.java:107) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.TransformationServicesHandler$$Lambda$92/30578394.accept(Unknown Source) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at java.util.HashMap$Values.forEach(HashMap.java:972) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.TransformationServicesHandler.initialiseTransformationServices(TransformationServicesHandler.java:107) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.TransformationServicesHandler.initializeTransformationServices(TransformationServicesHandler.java:59) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.Launcher.run(Launcher.java:76) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.Launcher.main(Launcher.java:66)  
    • excuse my idiocy where do i find the debug.log  
    • [1.17]Hey bro,i want to load a texture that can appear on the screen(like ui) when i hold some itmes,so i make a: package com.xiaomi.derby.screen; import com.mojang.blaze3d.vertex.PoseStack; import com.xiaomi.derby.item.ModItems; import net.minecraft.client.Minecraft; import net.minecraft.client.player.LocalPlayer; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.InteractionHand; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import static net.minecraftforge.fmlclient.gui.GuiUtils.drawTexturedModalRect; public class UIRenderer { private static final ResourceLocation TEXTURE = new ResourceLocation("derby","textures/gui/speed_bar"); @SubscribeEvent public void onRenderGui(RenderGameOverlayEvent.Post event) { LocalPlayer player = Minecraft.getInstance().player; if(RenderGameOverlayEvent.ElementType.ALL.equals(event.getType())){ assert player != null; if(player.getItemInHand(InteractionHand.MAIN_HAND).is(ModItems.HORSE_WHIP.get())) { Minecraft.getInstance().textureManager.bindForSetup(TEXTURE); drawTexturedModalRect(new PoseStack(), 50, 50, 80, 41, 80, 41, 0); } } } } But when i entered the game , i found there was not the texture which i set,it was a black rectangle.Can you tell me how can i solve this problem?THANKS A LOT!  
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.