Jump to content

Recommended Posts

Posted

I have given an item an inventory and created a GUI to manage it. It runs as expected, except that when I place a single in item the first slot, it returns to the player inventory instead of sicking to the pointer when dragging it to a new slot. Here is a simple test mod that recreates the problem, isolated from any side effects.

 

My item:

 

package container.test;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInvBasic;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class MyItem extends Item {

InventoryBasic inventory;

public MyItem() {
	inventory = new InventoryBasic("", false, 9);
}

@Override
public ItemStack onItemRightClick(ItemStack stack, World w, EntityPlayer player) {
	player.openGui(MyMod.instance, 0, w, 0, 0, 0);
	return stack;
}
}

 

My GUI:

 

package container.test;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;

public class MyGui extends GuiContainer {

public MyGui(InventoryPlayer playerInventory, IInventory inventory) {
	super(new MyContainer(playerInventory, inventory));
}

@Override
protected void drawGuiContainerBackgroundLayer(float p1, int p2, int p3) {
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	int x = (width - xSize) / 2;
	int y = (height - ySize) / 2;
	mc.getTextureManager().bindTexture(new ResourceLocation("textures/gui/container/generic_54.png"));
	drawTexturedModalRect(x, y, 0, 0, xSize, 35);
	drawTexturedModalRect(x, y + 35, 0, 126, xSize, 96);
}
}

 

My container:

 

package container.test;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;

public class MyContainer extends Container {

IInventory inventory;

public MyContainer(InventoryPlayer playerInventory, IInventory inventory) {

	this.inventory = inventory;

	for (int i = 0; i < inventory.getSizeInventory(); i++) {
		addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 18));
	}

	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 9; j++) {
			addSlotToContainer(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 49 + i * 18));
		}
	}

	for (int i = 0; i < 9; i++) {
		addSlotToContainer(new Slot(playerInventory, i, 8 + i * 18, 107));
	}
}

@Override
public boolean canInteractWith(EntityPlayer player) {
	return inventory.isUseableByPlayer(player);
}

}

 

and a simple test mod implementing IGuiHandler:

 

package container.test;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.IGuiHandler;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;

@Mod(modid = "myMod", name = "myMod", version = "")
public class MyMod implements IGuiHandler {

MyItem item;
@Instance("myMod")
public static MyMod instance;

@EventHandler
public void preInit(FMLPreInitializationEvent event) {
	item = new MyItem();
	item.setCreativeTab(CreativeTabs.tabMisc);
	GameRegistry.registerItem(item, "myMod");
	NetworkRegistry.INSTANCE.registerGuiHandler(this, this);
}

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	return new MyGui(player.inventory, item.inventory);
}

}

 

I have tried implementing IInventory instead of using InventoryBasic - same result. I am running on Forge 1180. Any help appreciated!

 

 

 

Posted

If I understand correctly, I return the container?

 

@Override

public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {

return new MyContainer(player.inventory, item.inventory);

}

 

Things are even worse now. When I place a single item in a slot, two show up.

 

So far I only having considered the case where the item (the game's instance of MyItem) is in one player's inventory. I left the server side out, thinking it is only needed to synchronize the two GUIs if two players are modifying the inventory at the same time.

Posted

Client side TE code is mainly concerned with the UI operations, while server side deals mostly with the item and container manipulations. So, you have to have both in order to keep the server and client in agreement.

Posted

Ok, I have implemented getServerGuiElement having it return MyContainer.

 

If I start an singleplayer game and open it to the lan, connect to the game from another machine, put MyItem in each player's inventory, and on MyItem's gui so that MyItem's inventory is visible on both machines, I get this on behavior:

 

On the remote client, things are perfect. I can put items in the inventory, move them around as much as I want, and MyItem's inventory is updated on the other client (the one that has the integrated server).

 

On the client which started the game (the one with the integrated server), when I put a single item in MyItem's inventory, two show up, then when I click on it to drag it, it disappears. The view is  synchronized on the other machine (in other words, the items double up then disappear on both machines).

 

Either this is a bug, or I am still not doing all that is necessary to set this up properly.

 

Incidentally, I tried this:

 

public MyItem() {

inventory = new InventoryBasic("", false, 9);

inventory.func_110134_a(this);

}

 

@Override

public void onInventoryChanged(InventoryBasic inventory) {

System.out.println("[MyItem.onInventoryChanged] this.inventory=" + this.inventory);

System.out.println("[MyItem.onInventoryChanged] inventory=" + inventory);

}

 

where MyItem implements IInvBasic. This doesn't have any effect on the behavior except that onInventoryChanged is called. I am not sure what I would do with this anyway.

 

 

Posted

Yes, the doubling up suggests things are being done on both sides when they should be done only on one.

I am registering myItem and an instance of MyGuiHandler in MyMod.preInit:

 

@EventHandler
public void preInit(FMLPreInitializationEvent event) {		
	MyItem myItem = new MyItem();
	myItem.setCreativeTab(CreativeTabs.tabMisc);
	myItem.setUnlocalizedName(myItem.getClass().getSimpleName());
	GameRegistry.registerItem(myItem, "myMod");		
	NetworkRegistry.INSTANCE.registerGuiHandler(this, new MyGuiHandler(myItem));
}

 

Is this doing anything on the client side that shouldn't be?

Apart from that, I'm not really doing anything else except opening the gui from MyItem.onItemRightClick()

For the sake of clarity, I have moved the gui handling to it's own class:

 

public class MyGuiHandler implements IGuiHandler {

MyItem myItem;

public MyGuiHandler(MyItem myItem) {
	this.myItem = myItem;
}

@Override
public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) {
	System.out.println("[MyGuiHandler.getServerGuiElement] id=" + id);
	if (id == 0) {
		return new MyContainer(player.inventory, myItem.inventory);
	}
	return null;
}

@Override
public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) {
	System.out.println("[MyGuiHandler.getClientGuiElement] id=" + id);
	new Throwable().printStackTrace();
	if (id == 0) {
		return new MyGui(player.inventory, myItem.inventory);
	}
	return null;
}
}

 

MyGuiHandler.getClientGuiElement() is being called twice. I have printed a stack trace from getClientGuiElement(). Only one call to EntityPlayer.openGui() originates from MyItem.onItemRightClick(). Is this normal behavior?

 

[MyGuiHandler.getClientGuiElement]
java.lang.Throwable
at container.test.MyGuiHandler.getClientGuiElement(MyGuiHandler.java:24)
at cpw.mods.fml.common.network.NetworkRegistry.getLocalGuiContainer(NetworkRegistry.java:263)
at cpw.mods.fml.common.network.internal.FMLNetworkHandler.openGui(FMLNetworkHandler.java:93)
at net.minecraft.entity.player.EntityPlayer.openGui(EntityPlayer.java:2514)
at container.test.MyItem.onItemRightClick(MyItem.java:21)
at net.minecraft.item.ItemStack.useItemRightClick(ItemStack.java:166)
at net.minecraft.client.multiplayer.PlayerControllerMP.sendUseItem(PlayerControllerMP.java:434)
at net.minecraft.client.Minecraft.func_147121_ag(Minecraft.java:1557)
at net.minecraft.client.Minecraft.runTick(Minecraft.java:2044)
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1039)
at net.minecraft.client.Minecraft.run(Minecraft.java:961)
at net.minecraft.client.main.Main.main(Main.java:164)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
[MyGuiHandler.getServerGuiElement]
[MyGuiHandler.getClientGuiElement]
java.lang.Throwable
at container.test.MyGuiHandler.getClientGuiElement(MyGuiHandler.java:24)
at cpw.mods.fml.common.network.NetworkRegistry.getLocalGuiContainer(NetworkRegistry.java:263)
at cpw.mods.fml.common.network.internal.FMLNetworkHandler.openGui(FMLNetworkHandler.java:93)
at net.minecraft.entity.player.EntityPlayer.openGui(EntityPlayer.java:2514)
at cpw.mods.fml.common.network.internal.OpenGuiHandler.channelRead0(OpenGuiHandler.java:16)
at cpw.mods.fml.common.network.internal.OpenGuiHandler.channelRead0(OpenGuiHandler.java:11)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:98)
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:101)
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111)
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785)
at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169)
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86)
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:247)
at net.minecraft.client.multiplayer.PlayerControllerMP.updateController(PlayerControllerMP.java:321)
at net.minecraft.client.Minecraft.runTick(Minecraft.java:1693)
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1039)
at net.minecraft.client.Minecraft.run(Minecraft.java:961)
at net.minecraft.client.main.Main.main(Main.java:164)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

Posted

i had a similar problem to this myself

it appears that for some stupid reason some of the custom inventory gets mixed up with the players armor slots,

my solution was to not use containers and to implement you own system from scratch,

took about a day to get working,

Posted

No, i was opening a new GUI for an enchanting table which has no inventory on the block, but uses placeholders like crafting and the original enchanting table

 

it would sort of work, but 4 of the slots where giving me my armor when i picked up from them (armor didnt show in the slot though )

and when i placed the items in the slots it went to my armor, even though it should have gone to my gui's temp inventory

 

giving an inventory to an item would be relatively easy

 

 

and to explain the apparent double clicking (getting 2 items in stead of one,) that is because you are manipulating the data on both client and server

I had that problem too

 

 

 

Posted

I solved the doubling up problem by testing for world.isRemote() in MyItem.onItemUse(), however, the GUIs where no longer synchronized when two players open the container in multiplayer, and items were still disappearing when moving them around in the inventory (in both GUIs).

 

On reflection, I think this has to do with the inventory being added to an Item.

 

Here is my test mod again.

 

MyItem (the test for isRemote is commented out because this way it works perfectly in multiplayer):

public class MyItem extends Item {

InventoryBasic inventory = new InventoryBasic("", false, 9);

@Override
public boolean onItemUse(ItemStack s, EntityPlayer p, World w, int x, int y, int z, int side, float sx, float sy, float sz) {
	// if (w.isRemote) {
	p.openGui(MyMod.instance, 0, w, x, y, z);
	return true;
	// }
	// return false;
}
}

 

MyContainer:

public class MyContainer extends Container {

public MyContainer(InventoryPlayer playerInventory, IInventory inventory) {

	for (int i = 0; i < inventory.getSizeInventory(); i++) {
		addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 18));
	}

	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 9; j++) {
			addSlotToContainer(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 49 + i * 18));
		}
	}

	for (int i = 0; i < 9; i++) {
		addSlotToContainer(new Slot(playerInventory, i, 8 + i * 18, 107));
	}
}

@Override
public boolean canInteractWith(EntityPlayer player) {
	return true;
}
}

 

MyGui:

public class MyGui extends GuiContainer {

public MyGui(MyContainer container) {
	super(container);
}

@Override
protected void drawGuiContainerBackgroundLayer(float p1, int p2, int p3) {
	int x = (width - xSize) / 2;
	int y = (height - ySize) / 2;
	mc.getTextureManager().bindTexture(new ResourceLocation("textures/gui/container/generic_54.png"));
	drawTexturedModalRect(x, y, 0, 0, xSize, 35);
	drawTexturedModalRect(x, y + 35, 0, 126, xSize, 96);
}
}

 

MyGuiHandler:

public class MyGuiHandler implements IGuiHandler {

IInventory inventory;

public MyGuiHandler(IInventory inventory) {
	this.inventory = inventory;
}

@Override
public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) {
	if (id == 0) {
		return new MyContainer(player.inventory, inventory);
	}
	return null;
}

@Override
public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) {
	if (id == 0) {
		return new MyGui(new MyContainer(player.inventory, inventory));
	}
	return null;
}
}

 

MyMod:

@Mod(modid = "myMod", name = "MyMod", version = "")
public class MyMod {

@Instance("myMod") public static MyMod instance;

@EventHandler
public void preInit(FMLPreInitializationEvent event) {
	MyItem myItem = new MyItem();
	myItem.setCreativeTab(CreativeTabs.tabMisc);
	myItem.setUnlocalizedName(myItem.getClass().getSimpleName());
	GameRegistry.registerItem(myItem, "myMod");
	NetworkRegistry.INSTANCE.registerGuiHandler(this, new MyGuiHandler(myItem.inventory));
}
}

 

I can't believe something so fundamental to the game is broken. I must be either doing something wrong or leaving something out. Has anyone succeeded in doing this?

Posted

ok, do you want each stack of the items to have a separate inventory , or do all stacks of the item to share the same inventory

 

I ask because they will currently be sharing the same inventory ,

 

to use separate inventories you will need to store the inventory in the ItemStack not the Item (Item is only referenced not instanced)

to store inventory on an ItemStack you will need to store it in the NBTTags

 

check out the following

itemstack.hasTagCompound();

itemstack.getTagCompound();

itemstack.setTagCompound(arg0);

 

 

as far as them being out of sync with one another you should not be changing the inventory on the client, you should only change it on the server

also if you are using a shared inventory you might need a container on the server, so it can notify all players who are viewing it

 

Posted

Ewe,

 

>or do all stacks of the item to share the same inventory?

 

Ideally, each item has its own inventory.

 

I've given MyItem an instance of MyInventory implementing IInventory instead of extending InventoryBasic. To read and write to the tag I need MyItem's stack. How can I get the stack when it is not passed to IInventory.getStackInSlot(int) and IInventory.setInventorySlotContents(int, ItemStack)? So far, I am passing the stack to MyInventory when MyItem's gui is opened, but this will not work when two players modify the inventory at the same time.

 

>you should not be changing the inventory on the client, you should only change it on the server

 

I am not sure where to do this. In the gui handler?, testing for world.isRemote when opening the gui? do I override something in Container? in my implementation of IInventory?

 

Here is MyInventory (inventory stack limit is 1, so decrStackSize is simplified. MyTagWrapper will read and write the tag) :

 

public class MyInventory implements IInventory {

private MyTagWrapper tag;
private String name;
private boolean hasName;

public MyInventory(int size) {
	tag = new MyTagWrapper(9);
}

public void setItemStack(ItemStack stack) {
	tag.setTag(stack.getTagCompound());
}

       ////// Implementation of IInventory

@Override
public ItemStack getStackInSlot(int i) {
	if (i < 0 || i > tag.getSize() - 1) {
		throw new IllegalArgumentException("Not in tag range: " + i);
	}
	return tag.getItemStack(i);
}

@Override
public ItemStack decrStackSize(int i, int amount) {
	ItemStack stack = tag.getItemStack(i);
	tag.setItemStack(i, null);
	return stack;
}

@Override
public ItemStack getStackInSlotOnClosing(int i) {
	if (tag.getItemStack(i) != null) {
		ItemStack itemstack = tag.getItemStack(i);
		tag.setItemStack(i, null);
		return itemstack;
	} else {
		return null;
	}
}

@Override
public void setInventorySlotContents(int i, ItemStack stack) {
	if (i < 0 || i > tag.getSize() - 1) {
		throw new IllegalArgumentException("Not in tag range: " + i);
	}
	tag.setItemStack(i, stack);
	markDirty();
}

@Override
public int getSizeInventory() {
	return tag.getSize();
}

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

@Override
public boolean hasCustomInventoryName() {
	return hasName;
}

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

@Override
public void markDirty() {
}

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

@Override
public void openInventory() {
}

@Override
public void closeInventory() {
}

@Override
public boolean isItemValidForSlot(int i, ItemStack stack) {
	return true;
}
}

 

 

Posted

Your first problem is in implementation. Only one instance of any item is ever created in a world. All other things like it are echos of that one, or phantoms. Therefore, they cannot have an inventory, since there is no way for that data to persist.

Enter ItemStacks, they represent concrete instances of a particular item with representation instance data (in NBT form). They could have an inventory just like a backpack or a bag and save and load the contents.

Also, it would always have a stacksize of one and unless you checked for it specifically, lose all of the contents on destruction. You would have your item (possibly implement IInventory and) use the ItemStack to load the slots dynamically on use, and refresh NBT data on closing the container.

That would be how I'd do it, but look at some backpack code if you want a better idea.

Posted

Thanks again for your reply.

 

>You would have your item (possibly implement IInventory and) use the ItemStack to load the slots

 

This is all clear to me. In my case, I have given my item a field named inventory which implements IInventory. This is the inventory that I pass to my container and my gui, and this inventory writes to ItemStack.stackTagCompound. My question is how to pass the stack to the inventory? IIventory has a getter and a setter which is called by the container:

 

ItemStack getStackInSlot(int p_70301_1_);
void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_);

 

neither of which include a parameter of type ItemStack which I can get the tag from. (The stack in setInventorySlotContents is what I am putting in the slot). Items don't know what stack they are in - they have to be told. In methods like Item.onItemUse() pass the stack as a parameter for this reason.

 

As a workaround, I am passing the stack (actually just the tag) to the inventory with my own method MyInventory.setTag(NBTTagCompound tag) when the gui is opened. The problem is that this sets a field in MyInventory which, because there is only one instance of the item in the game, will be shared between players, and we are back at square one. It's only a minor problem because the players would have to open the containers at the same time for there to be a conflict.

 

Thanks also for the suggestion of looking at the backpack mod, which is on GitHub. There are thousands of lines of code there. I will try to slug my way though it  :P

Posted

07,

 

I had a look at SevenCommons. If I understand, you would pass an itemStack maybe to the gui handller so that it can construct a new inventory and then the container every time the gui is opened? Something like:

 

@Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int s, float sx, float sy, float sz) {
MyMod.instance.guiHandler.setItemStack(stack);
player.openGui(MyMod.instance, 0, world, x, y, z);
return true;
}

 

 

public class MyGuiHandler implements IGuiHandler {

IInventory inventory;

public void setItemStack(ItemStack stack) {
	inventory = new ItemInventory(stack);
}

@Override
public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) {
	return new MyContainer(player.inventory, inventory);
}

@Override
public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) {
	return new MyGui(new MyContainer(player.inventory, inventory));
}
}

Posted

Yes, much better. I am no longer getting strange behaviour in my container. Before I was passing the same inventory object to both client and server sides. Thank you.

 

I still have one problem. I am writing to the tag but I seem to be getting a new empty tag object from getCurrentEquippedItem() every time I reopen the gui.

 

Should the gui be opened from the client side, the server side, or both in onItemUse()? If I open only from the server, getServerGuiElement() and getClientGuiElement() are called each once:

 

	@Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int s, float sx,
		float sy, float sz) {
	if (!world.isRemote) {
		player.openGui(MyMod.instance, 0, world, x, y, z);
		return true;
	}
	return false;
}

 

public class MyGuiHandler implements IGuiHandler {

@Override
public Object getServerGuiElement(int id, EntityPlayer player, World w, int x, int y, int z) {
	NBTTagCompound t = player.getCurrentEquippedItem().getTagCompound();
	System.out.println("[MyGuiHandler.getServerGuiElement] w=" + w + ", tag=@" + System.identityHashCode(t) + t);
	return new MyContainer(player.inventory, new MyInventory(player.getCurrentEquippedItem()));
}

@Override
public Object getClientGuiElement(int id, EntityPlayer player, World w, int x, int y, int z) {
	NBTTagCompound t = player.getCurrentEquippedItem().getTagCompound();
	System.out.println("[MyGuiHandler.getClientGuiElement] w=" + w + ", tag=@" + System.identityHashCode(t) + t); 
	return new MyGui(new MyContainer(player.inventory, new MyInventory(player.getCurrentEquippedItem())));
}
}

 

Console (opening gui twice):

 

[MyGuiHandler.getServerGuiElement] w=net.minecraft.world.WorldServer@6c0486cc, tag=@421020847{}
[MyGuiHandler.getClientGuiElement] w=net.minecraft.client.multiplayer.WorldClient@506382b1, tag=@317245315{}

[MyGuiHandler.getServerGuiElement] w=net.minecraft.world.WorldServer@6c0486cc, tag=@821869132{}
[MyGuiHandler.getClientGuiElement] w=net.minecraft.client.multiplayer.WorldClient@506382b1, tag=@539684020{}

 

I seem to be getting a different tag object each time.

 

Opening the gui from both sides gives:

 

[MyGuiHandler.getClientGuiElement] w=net.minecraft.client.multiplayer.WorldClient@9da1db, tag=@1217657581{}
[MyGuiHandler.getServerGuiElement] w=net.minecraft.world.WorldServer@1693401c, tag=@1166845275{}
[MyGuiHandler.getClientGuiElement] w=net.minecraft.client.multiplayer.WorldClient@9da1db, tag=@1217657581{}

[MyGuiHandler.getClientGuiElement] w=net.minecraft.client.multiplayer.WorldClient@9da1db, tag=@824254376{}
[MyGuiHandler.getServerGuiElement] w=net.minecraft.world.WorldServer@1693401c, tag=@1973774282{}
[MyGuiHandler.getClientGuiElement] w=net.minecraft.client.multiplayer.WorldClient@9da1db, tag=@824254376{}

Posted

From you javadoc:

 

>If the ItemStack being written to is in an inventory itself, {@link ItemInventory.WithInventory} should be used instead.

 

Just to be sure I understand, do you mean "if the ItemStack being written to is referenced by another inventory", or maybe "if you have already constructed an instance of ItemInventory with this ItemStack"?

 

If so, does this apply to my case? Would I use it something like this:

 

public class MyGuiHandler implements IGuiHandler {

       IInventory inv;

@Override
public Object getServerGuiElement(int id, EntityPlayer player, World w, int x, int y, int z) {
                inv =  new ItemInventory(player.getCurrentEquippedItem());
	return new MyContainer(player.inventory, inv);
}

@Override
public Object getClientGuiElement(int id, EntityPlayer player, World w, int x, int y, int z) {
                int slot = ?;
	return new MyGui(new MyContainer(player.inventory, new ItemInventory.WithInventory(inv, slot)));
}
}

 

I'm lost ???.

 

Posted

Finally! It works. Thank you diesieben07.

 

Here is my implementation of IInventory based on ItemInventory.WithInventory. It's a little different in that it writes to the tag at every call to setInventorySlotContents and does not maintain an ItemStack arrray. Note second line of setInventorySlotContents();

 

public class MyInventory implements IInventory {

// This wrapper encapsulates the complexities of writing to the tag
        private MyTagWrapper tag;
private int size = 9;
IInventory inventory;
ItemStack stack;
private int slot;

public MyInventory(IInventory inventory, int slot) {
	this.inventory = inventory;
	this.slot = slot;
	this.stack = inventory.getStackInSlot(slot);
	NBTTagCompound t = stack.getTagCompound();
	if (t == null) {
		t = new NBTTagCompound();
		stack.setTagCompound(t);
	}
	tag = new MyTagWrapper(t, size);
}

@Override
public ItemStack getStackInSlot(int i) {
	return tag.getItemStack(i);
}

        //Stack limit is 1
        @Override
public ItemStack decrStackSize(int i, int amount) {
	ItemStack stack = tag.getItemStack(i);
	tag.setItemStack(i, null);
	return stack;
}

@Override
public ItemStack getStackInSlotOnClosing(int i) {
	if (tag.getItemStack(i) != null) {
		ItemStack itemstack = tag.getItemStack(i);
		tag.setItemStack(i, null);
		return itemstack;
	} else {
		return null;
	}
}

@Override
public void setInventorySlotContents(int i, ItemStack stack) {
	tag.setItemStack(i, stack);
                //This is what was missing previously
	inventory.setInventorySlotContents(slot, this.stack);
}

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

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

@Override
public boolean hasCustomInventoryName() {
	return false;
}

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

@Override
public void markDirty() {
}

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

@Override
public void openInventory() {
}

@Override
public void closeInventory() {
}

@Override
public boolean isItemValidForSlot(int i, ItemStack stack) {
	return true;
}
}

 

Here is where it is constructed:

 

public class MyGuiHandler implements IGuiHandler {

@Override
public Object getServerGuiElement(int id, EntityPlayer player, World w, int x, int y, int z) {
	return new MyContainer(player.inventory, new MyInventory(player.inventory, player.inventory.currentItem));
}

@Override
public Object getClientGuiElement(int id, EntityPlayer player, World w, int x, int y, int z) {
	return new MyGui(new MyContainer(player.inventory, new MyInventory(player.inventory, player.inventory.currentItem)));
}
}

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi, I've been having trouble trying to use forge as it shows a black screen when I open the game, but I can still interact with it and hear the music.  I've done all of the step by steps and most common fixes like updating drivers, keeping up to date with Java, deleting and reinstalling minecraft, restarting my computer MANY times, even smaller things like splash.properties (I didn't have that file so I added it and set it to false thinking it would do something, definitely not) and making sure to prioritize my rtx 3070 in the settings but with no luck. Minecraft works as intended when I uninstall forge and I also don't have any mods currently, it just gives me this issue when I install forge. I also increased the ram usage, made sure my hardware isn't full or anything, and even changed the resolution in hopes it would fix things. I checked my antivirus and firewall but that isn't the issue either. Trust me, I've done everything I can think of. For some reason the black screen does flicker a little into the main menu, but obviously unplayable. I couldn't even make my way to the settings with how little it flickered. I'm not sure if it flickered randomly or if it was because I was messing around moving and clicking a bunch, I didn't really test it that much.  
    • I've had a really weird issue recently,  I wanted to add the Depper and Darker mod on my dedicated server (MC 1.21 with Fabric 0.16.9, hosted on nitroserv.com) but whenever I do add the mod the sever stops doing anything after listing the mods, and I get no crash or error or anything, just a stuck server. Here's a normal log of the server booting up: https://pastebin.com/JipFF2Eh and here's the log of the server doing the weird thing: https://pastebin.com/W4JBh3eX I just don't understand it. I've tried removing other mods (somewhat randomly) but deeper and darker still breaks my server whenever I add it. NitroServ support staff is about as confused as I am and I've had no response from the Deeper and Darker support staff... Now I know this is the Forge support not the Fabric support but I'm just trying to know if anyone has any kind of idea to fix this (aside from not using the mod obviously) Also I still have a bunch of errors and warnings whenever the server does start properly, are there any of them I should be worried about?
    • Delete the config of RandomTweaker (config folder) If there is no change, remove this mod
    • Hello! So i have been trying to make a mod that adds plant fiber to minecraft 1.16.5 (i believe there are mods that add plant fiber but not for 1.16.5) but the problem is that i want to modify the loot table of grass to always drop plant fiber but also keep the vanilla drops. Most common answer i have seen is GlobalLootModifiers. But my tiny brain cant understand any tutorials. So any help is appreciated.
    • Minecraft forge 1.12.2 does not load. Here is both logs. I assume its because of a mod that i have, idk. (L521)
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.