Posted May 27, 20196 yr Hello, my container is exhibiting some weird behaviour: The itemstack inside is not saved if I close the gui, if however my code reaches a certain point (I have marked it with a comment) then the itemstack is saved normally. My Code: Container: Spoiler package com.andreas.leer.container; import javax.annotation.Nonnull; import com.andreas.leer.block.BlockVoidcom; import com.andreas.leer.tileentities.TileEntityVoidcom; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import net.minecraft.world.World; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.SlotItemHandler; public class ContainerVoidcom extends Container{ final private TileEntityVoidcom tile; IItemHandler inventory; public ContainerVoidcom(InventoryPlayer playerInv, World worldIn,final TileEntityVoidcom ptile) { this.tile = ptile; inventory = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null); //Der Fallen-Slot addSlotToContainer(new SlotItemHandler(inventory,0,80,20) { @Override public void onSlotChanged(){ if(!worldIn.isRemote) if(getStack().getUnlocalizedName().equals("item.trap")) worldIn.setBlockState(tile.getPos(), worldIn.getBlockState(tile.getPos()).withProperty(BlockVoidcom.FULL,true), 2); else worldIn.setBlockState(tile.getPos(), worldIn.getBlockState(tile.getPos()).withProperty(BlockVoidcom.FULL,false), 2); tile.markDirty(); } @Override public int getSlotStackLimit(){ return 1; } @Override public boolean isItemValid(@Nonnull ItemStack stack) { if(super.isItemValid(stack)) { String name = stack.getUnlocalizedName(); System.out.println(name); if(name.equals("item.trap")) return true; } return false; } }); //Playerinventar for (int i = 0; i < 3; i++) { for (int j = 0; j < 9; j++) { addSlotToContainer(new Slot(playerInv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); } } //Hotbar for (int k = 0; k < 9; k++) { addSlotToContainer(new Slot(playerInv, k, 8 + k * 18, 142)); } } @Override public boolean canInteractWith(EntityPlayer playerIn) { return true; } //Shift Klick @Override public ItemStack transferStackInSlot(EntityPlayer player, int index) { ItemStack itemstack = ItemStack.EMPTY; Slot slot = inventorySlots.get(index); if (slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); int containerSlots = inventorySlots.size() - player.inventory.mainInventory.size(); if (index < containerSlots) { if (!this.mergeItemStack(itemstack1, containerSlots, inventorySlots.size(), true)) { return ItemStack.EMPTY; } } else if (!this.mergeItemStack(itemstack1, 0, containerSlots, false)) { return ItemStack.EMPTY; } if (itemstack1.getCount() == 0) { slot.putStack(ItemStack.EMPTY); } else { slot.onSlotChanged(); } if (itemstack1.getCount() == itemstack.getCount()) { return ItemStack.EMPTY; } slot.onTake(player, itemstack1); } return itemstack; } } The Gui: Spoiler package com.andreas.leer.gui; import com.andreas.leer.LEER; import com.andreas.leer.block.InitBlocks; import com.andreas.leer.network.PacketHandler; import com.andreas.leer.network.VoidcomMessage; import com.andreas.leer.tileentities.TileEntityVoidcom; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.resources.I18n; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.util.ResourceLocation; public class GuiVoidcom extends GuiContainer{ private InventoryPlayer playerInv; private static final ResourceLocation BG_TEXTURE = new ResourceLocation(LEER.MODID, "textures/guis/gui_voidcom.png"); private TileEntityVoidcom tile = null; public static int energy = 0; public static int progress = 0; public GuiVoidcom(Container container,InventoryPlayer playerInv, TileEntityVoidcom ptile) { super(container); this.playerInv = playerInv; this.tile = ptile; } @Override protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { PacketHandler.INSTANCE.sendToServer(new VoidcomMessage(tile.getPos())); GlStateManager.color(1, 1, 1, 1); mc.getTextureManager().bindTexture(BG_TEXTURE); int p = (int)(126*((float)progress/tile.max)); int e = (int)(62*((float)energy/tile.energy.getMaxEnergyStored())); int x = (width - xSize) / 2; int y = (height - ySize) / 2; //Main Gui drawTexturedModalRect(x, y, 0, 0, xSize, ySize); //Energybar drawTexturedModalRect(x+25,y+42, 0, 181, e, 8); //ProgressBar drawTexturedModalRect(x+25,y+54, 0, 166, p, 14); } @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { this.drawDefaultBackground(); super.drawScreen(mouseX, mouseY, partialTicks); this.renderHoveredToolTip(mouseX, mouseY); } @Override protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { String name = I18n.format(InitBlocks.voidcom.getUnlocalizedName() + ".name"); fontRenderer.drawString(name, xSize / 2 - fontRenderer.getStringWidth(name) / 2, 6, 0x404040); fontRenderer.drawString(playerInv.getDisplayName().getUnformattedText(), 8, ySize - 94, 0x404040); } } The Tile Entity(Code is marked here): Spoiler package com.andreas.leer.tileentities; import javax.annotation.Nullable; import com.andreas.leer.energy.EnergyBase; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.ITickable; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.energy.CapabilityEnergy; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.ItemStackHandler; public class TileEntityVoidcom extends TileEntity implements ITickable{ public EnergyBase energy = new EnergyBase(50000); private ItemStackHandler inventory = new ItemStackHandler(1); private boolean test = true; public int max=100; public int progress=0; public TileEntityVoidcom(){ } @Override public void update() { if(!world.isRemote) { ItemStack stack = inventory.getStackInSlot(0); if(stack != null) { if(stack.hasCapability(CapabilityEnergy.ENERGY,null)) if(stack.getCapability(CapabilityEnergy.ENERGY,null).receiveEnergy(1000,true)>=1000 && energy.extractEnergy(25000,true)>=25000) { stack.getCapability(CapabilityEnergy.ENERGY,null).receiveEnergy(1000,false); energy.extractEnergy(1000,false); } if(stack.getUnlocalizedName().equals("item.trap")&&(!stack.hasTagCompound()||!(stack.getTagCompound().getBoolean("full")))) { //Sichtcheck for(int i=1;i<(pos.getY()+1)&&test;i++) { if(world.isAirBlock(pos.down(i))) test=true; else test=false; } //Falls Sicht besteht if(test) { if(energy.extractEnergy(25000,true)>=25000) { //This is the point !!! progress+=1; energy.extractEnergy(25000,false); } if(progress>=max){ NBTTagCompound nbt; if(stack.hasTagCompound()) { nbt = stack.getTagCompound(); nbt.setBoolean("full",true); } else { nbt = new NBTTagCompound(); nbt.setBoolean("full",true); } stack.setTagCompound(nbt); progress=0; } } } } } test=true; } @Override public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) { if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { return true; } if(capability == CapabilityEnergy.ENERGY && (facing != EnumFacing.UP || facing != EnumFacing.DOWN)) { return true; } return super.hasCapability(capability, facing); } @SuppressWarnings("unchecked") @Nullable @Override public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) { if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { return (T)inventory; } if(capability == CapabilityEnergy.ENERGY) { return (T)energy; } return super.getCapability(capability, facing); } /*NBT Sachen*/ @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { compound.setTag("inventory", inventory.serializeNBT()); compound.setInteger("energy", energy.getEnergyStored()); return super.writeToNBT(compound); } @Override public void readFromNBT(NBTTagCompound compound) { inventory.deserializeNBT(compound.getCompoundTag("inventory")); energy.setEnergyStored(compound.getInteger("energy")); super.readFromNBT(compound); } } Thank you for your help!
May 27, 20196 yr Author 1 minute ago, diesieben07 said: Also, I have no idea what to make of your error description. Please elaborate what is not saved or is saved in what exact circumstances. How are you checking if something "is saved"? First of all thank you for the advice! My problem is that when I close the gui the content of the slot is not saved everytime. It is however sometimes saved, if I wait long enough and I think that has something to do with changing the progress variable, as when I removed the part I marked(in the tile entity class) the itemstack was not saved anymore.
May 27, 20196 yr Author When I say "not saved" I mean that if I close the gui the itemstack in slot 0 is gone.
May 27, 20196 yr Author 16 minutes ago, diesieben07 said: Why in the world are these static? For some reason my gui packet is no longer working since I made them non-static ?
May 27, 20196 yr Author 33 minutes ago, diesieben07 said: Things like this are very hard to debug by just looking at code. Please post a working Git repository of your mod. Okay here you go:https://github.com/GloriousAlpaca/Leer I'm not used to using github, so let me know if there are any github related errors!
May 27, 20196 yr Author 3 minutes ago, diesieben07 said: package com.gloriousalpaca - You still do not own this domain. Well surely I won't need to buy a domain ? I'll just drop the com then. 7 minutes ago, diesieben07 said: Code-Style, issue 1, 2 and 4. About Issue 1: So I can just safely delete the commonproxy class? Issue 2: Could you tell me where I did not use @Override ? 11 minutes ago, diesieben07 said: Problematic code, issue 14. Is the class InitItems as a whole considered static or is it just the registerModels method? Also I can't seem to find the event for registering tile entities. 23 minutes ago, diesieben07 said: Why? I would like to change the blockstate based on whether an item is inserted or not. 24 minutes ago, diesieben07 said: This does nothing. Yes I was testing something, originally I was checking for the unlocalized name, but I assume this is bad practice. Should I rather check for the registry name ? 25 minutes ago, diesieben07 said: You are not following the IItemHandler documentation. Okay so I am supposed to first extract the ItemStack, then modify it and lastly insert it again ? Thank you very much for taking your time to help me, this has been incredibely useful and I am very sorry for making such common errors.
May 27, 20196 yr Author 3 minutes ago, diesieben07 said: Your "CommonProxy" is like a server proxy. You need to have a client-side proxy, a server-side proxy and an interface that's being implemented by both. Okay so should I just rename it to serverproxy ? 6 minutes ago, diesieben07 said: Uh. Neither. Did you read issue 14? It's about creating item and block instances in static initializers. Not about methods, classes or models. Would this be okay then (I have not removed ItemBase yet): Spoiler public class InitItems { public static void register(IForgeRegistry<Item> registry) { ItemBase obsidianshard = new ItemBase("leer.obsidianshard"); ItemTrap trap = new ItemTrap(); ItemTester tester = new ItemTester(); registry.registerAll( obsidianshard, trap, tester ); } public static void registerModels() { ItemBase obsidianshard = new ItemBase("leer.obsidianshard"); ItemTrap trap = new ItemTrap(); ItemTester tester = new ItemTester(); obsidianshard.registerItemModel(); trap.registerItemModel(); tester.registerItemModel(); } } Again thank you very much!
May 27, 20196 yr Author I am not sure if I have understood this correctly but I tried using the ObjectHolder Annotation: Spoiler @SubscribeEvent public void registerItems(RegistryEvent.Register<Item> event) { ItemBase obsidianshard = new ItemBase("leer.obsidianshard"); ItemTrap trap = new ItemTrap(); ItemTester tester = new ItemTester(); event.getRegistry().registerAll( obsidianshard, trap, tester ); InitBlocks.registerItemBlocks(event.getRegistry()); } @SubscribeEvent public static void registerEntities(RegistryEvent.Register<EntityEntry> event) { InitEntities.register(event); ItemBase obsidianshard = ItemHolder.obsidianshard; ItemTrap trap = ItemHolder.trap; ItemTester tester = ItemHolder.tester; obsidianshard.registerItemModel(); trap.registerItemModel(); tester.registerItemModel(); } This is the ItemHolder class: Spoiler package com.gloriousalpaca.leer.item; import net.minecraftforge.fml.common.registry.GameRegistry; import net.minecraftforge.fml.common.registry.GameRegistry.ObjectHolder; @ObjectHolder("leer") public class ItemHolder { @GameRegistry.ObjectHolder("obsidianshard") public static final ItemBase obsidianshard = null; @GameRegistry.ObjectHolder("trap") public static final ItemTrap trap = null; @GameRegistry.ObjectHolder("tester") public static final ItemTester tester = null; }
May 27, 20196 yr Author 45 minutes ago, diesieben07 said: That's fine. Sorry to bother you again, however now my items are not appearing in-game. I have updated the github repository if you would like to take a look.
May 28, 20196 yr Author 13 hours ago, diesieben07 said: You are using @EventBusSubscriber to register the event handler LEER.RegistrationHandler. That means it's event handler methods must be static. Some are not. Everything except the soundevents are working now! This is the crash I get when I register the soundevents: Spoiler ---- Minecraft Crash Report ---- // This doesn't make any sense! Time: 5/28/19 12:36 PM Description: Initializing game java.lang.NullPointerException: Can't use a null-name for the registry, object net.minecraft.util.SoundEvent@6f99d91f. at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:864) at net.minecraftforge.registries.ForgeRegistry.add(ForgeRegistry.java:294) at net.minecraftforge.registries.ForgeRegistry.add(ForgeRegistry.java:288) at net.minecraftforge.registries.ForgeRegistry.register(ForgeRegistry.java:120) at net.minecraftforge.registries.ForgeRegistry.registerAll(ForgeRegistry.java:161) at mod.leer.LEER$RegistrationHandler.registerSoundEvents(LEER.java:129) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_4_RegistrationHandler_registerSoundEvents_Register.invoke(.dynamic) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) at net.minecraftforge.fml.common.eventhandler.EventBus$1.invoke(EventBus.java:144) at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182) at net.minecraftforge.registries.GameData.fireRegistryEvents(GameData.java:859) at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:628) at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:252) at net.minecraft.client.Minecraft.init(Minecraft.java:514) at net.minecraft.client.Minecraft.run(Minecraft.java:422) at net.minecraft.client.main.Main.main(Main.java:118) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) at net.minecraft.launchwrapper.Launch.main(Launch.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) at GradleStart.main(GradleStart.java:25) A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Thread: Client thread Stacktrace: at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:864) at net.minecraftforge.registries.ForgeRegistry.add(ForgeRegistry.java:294) at net.minecraftforge.registries.ForgeRegistry.add(ForgeRegistry.java:288) at net.minecraftforge.registries.ForgeRegistry.register(ForgeRegistry.java:120) at net.minecraftforge.registries.ForgeRegistry.registerAll(ForgeRegistry.java:161) at mod.leer.LEER$RegistrationHandler.registerSoundEvents(LEER.java:129) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_4_RegistrationHandler_registerSoundEvents_Register.invoke(.dynamic) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) at net.minecraftforge.fml.common.eventhandler.EventBus$1.invoke(EventBus.java:144) at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182) at net.minecraftforge.registries.GameData.fireRegistryEvents(GameData.java:859) at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:628) at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:252) at net.minecraft.client.Minecraft.init(Minecraft.java:514) -- Initialization -- Details: Stacktrace: at net.minecraft.client.Minecraft.run(Minecraft.java:422) at net.minecraft.client.main.Main.main(Main.java:118) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) at net.minecraft.launchwrapper.Launch.main(Launch.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) at GradleStart.main(GradleStart.java:25) -- System Details -- Details: Minecraft Version: 1.12.2 Operating System: Windows 10 (amd64) version 10.0 Java Version: 1.8.0_171, Oracle Corporation Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation Memory: 789875344 bytes (753 MB) / 1208483840 bytes (1152 MB) up to 5712117760 bytes (5447 MB) JVM Flags: 0 total; IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0 FML: MCP 9.42 Powered by Forge 14.23.5.2836 14 mods loaded, 14 mods active States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored | State | ID | Version | Source | Signature | |:----- |:----------------- |:------------ |:----------------------------------------------- |:---------------------------------------- | | LCH | minecraft | 1.12.2 | minecraft.jar | None | | LCH | mcp | 9.42 | minecraft.jar | None | | LCH | FML | 8.0.99.99 | forgeSrc-1.12.2-14.23.5.2836.jar | None | | LCH | forge | 14.23.5.2836 | forgeSrc-1.12.2-14.23.5.2836.jar | None | | LCH | leer | 1.0 | leer-1.0.jar | None | | LCH | codechickenlib | 3.2.2.353 | CodeChickenLib-1.12.2-3.2.2.353-universal.jar | f1850c39b2516232a2108a7bd84d1cb5df93b261 | | LCH | redstoneflux | 2.1.0 | RedstoneFlux-1.12-2.1.0.6-universal.jar | 8a6abf2cb9e141b866580d369ba6548732eff25f | | LCH | cofhcore | 4.6.2 | CoFHCore-1.12.2-4.6.2.25-universal.jar | None | | LCH | cofhworld | 1.3.0 | CoFHWorld-1.12.2-1.3.0.6-universal.jar | 8a6abf2cb9e141b866580d369ba6548732eff25f | | LCH | jei | 4.11.0.212 | jei_1.12.2-4.11.0.212.jar | None | | LCH | thermalfoundation | 2.6.2 | ThermalFoundation-1.12.2-2.6.2.26-universal.jar | 8a6abf2cb9e141b866580d369ba6548732eff25f | | LCH | thermalexpansion | 5.5.3 | ThermalExpansion-1.12.2-5.5.3.41-universal.jar | 8a6abf2cb9e141b866580d369ba6548732eff25f | | LCH | thermaldynamics | 2.5.4 | ThermalDynamics-1.12.2-2.5.4.18-universal.jar | 8a6abf2cb9e141b866580d369ba6548732eff25f | | LCH | thermalinnovation | 0.3.2 | ThermalInnovation-1.12.2-0.3.2.11-universal.jar | 8a6abf2cb9e141b866580d369ba6548732eff25f | Loaded coremods (and transformers): GL info: ' Vendor: 'NVIDIA Corporation' Version: '4.6.0 NVIDIA 397.93' Renderer: 'GeForce GTX 760/PCIe/SSE2' Launched Version: 1.12.2 LWJGL: 2.9.4 OpenGL: GeForce GTX 760/PCIe/SSE2 GL version 4.6.0 NVIDIA 397.93, NVIDIA Corporation GL Caps: Using GL 1.3 multitexturing. Using GL 1.3 texture combiners. Using framebuffer objects because OpenGL 3.0 is supported and separate blending is supported. Shaders are available because OpenGL 2.1 is supported. VBOs are available because OpenGL 1.5 is supported. Using VBOs: Yes Is Modded: Definitely; Client brand changed to 'fml,forge' Type: Client (map_client.txt) Resource Packs: Current Language: English (US) Profiler Position: N/A (disabled) CPU: 4x Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz
May 29, 20196 yr Author 1 hour ago, diesieben07 said: You do not set the registry name for your SoundEvent instances. I thought the ResourceLocation already was a registry name thank you.
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.