Jump to content

Recommended Posts

Posted

So, I made a custom inventory for the player, that houses a custom slot. I would like to know how I would check if my custom slot contains a certain item. I would normally do this in a onUpdate method (like in a TileEntity), but there is no update method that I've found within the Container class. Where would I look to accomplish this?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Posted

Container is something created when you need lookup on inventory, Inventory itself exists always (not counting few cases). You simply access inventory and check if item is in inventory.

1.7.10 is no longer supported by forge, you are on your own.

Posted

Because it's my own inventory, I have my own inventory class. :P How exactly would I go about checking though? I know I can use "getStackInSlot" and use my slot ID, then check for the item. But, where would I be checking for that?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Posted

That really depends: what is the point of knowing what item is in there? Are you trying to replicate the vanilla Item code where #onUpdate is called each tick for items in your inventory, or you just want to know if something is in the slot?

 

For the first case, subscribe to PlayerTickEvent and cycle through your player's inventory each tick calling Item#onUpdate for any non-null stacks.

 

For the second case, you could override setStackInSlot in your inventory class and have it notify the player each time something is added or removed, but again, what are you going to do with that? For most use cases, you will probably still need a tick handler to manage it, but it really depends.

Posted

I believe the second is best in my case. When the item is added, I would like to increase my maxWeight variable in the ExtendedPlayer by a certain amount. And when removed, decrease it by the same amount. So, using a tick handler would be useless I believe.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Posted

Alright, it works with the detection and such. But, I need to get the server side player in order to use my ExtendedPlayer. How would I be able to set that?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Posted

Override #setInventorySlotContents like I mentioned before, and in your IInventory implementation, store the instance of the player to which it belongs (or a reference to that player's IEEP), i.e. require an EntityPlayer be passed to the constructor and store that instance - then you can access the player any time directly from the inventory, so when the slot contents change, you can fetch the player's IEEP and adjust it.

Posted

Alright, that worked fine. Except for this persistent error that happens when I log in, or open the custom inventory after putting the item in and logging off and on.

 

Error:

[21:07:20] [Netty Server IO #1/ERROR] [FML]: There was a critical exception handling a packet on channel WeightLimit
java.lang.NullPointerException
at net.minecraftforge.fml.common.network.FMLOutboundHandler$OutboundTarget$4.selectNetworks(FMLOutboundHandler.java:112) ~[FMLOutboundHandler$OutboundTarget$4.class:?]
at net.minecraftforge.fml.common.network.FMLOutboundHandler.write(FMLOutboundHandler.java:276) ~[FMLOutboundHandler.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeWrite(DefaultChannelHandlerContext.java:645) ~[DefaultChannelHandlerContext.class:4.0.15.Final]
at io.netty.channel.DefaultChannelHandlerContext.write(DefaultChannelHandlerContext.java:699) ~[DefaultChannelHandlerContext.class:4.0.15.Final]
at io.netty.channel.DefaultChannelHandlerContext.write(DefaultChannelHandlerContext.java:638) ~[DefaultChannelHandlerContext.class:4.0.15.Final]
at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:115) ~[MessageToMessageEncoder.class:4.0.15.Final]
at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116) ~[MessageToMessageCodec.class:4.0.15.Final]
at io.netty.channel.DefaultChannelHandlerContext.invokeWrite(DefaultChannelHandlerContext.java:645) ~[DefaultChannelHandlerContext.class:4.0.15.Final]
at io.netty.channel.DefaultChannelHandlerContext.write(DefaultChannelHandlerContext.java:699) ~[DefaultChannelHandlerContext.class:4.0.15.Final]
at io.netty.channel.DefaultChannelHandlerContext.writeAndFlush(DefaultChannelHandlerContext.java:689) ~[DefaultChannelHandlerContext.class:4.0.15.Final]
at io.netty.channel.DefaultChannelHandlerContext.writeAndFlush(DefaultChannelHandlerContext.java:718) ~[DefaultChannelHandlerContext.class:4.0.15.Final]
at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:893) ~[DefaultChannelPipeline.class:4.0.15.Final]
at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:240) ~[AbstractChannel.class:4.0.15.Final]
at net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper.sendTo(SimpleNetworkWrapper.java:196) ~[simpleNetworkWrapper.class:?]
at com.happykiller.weightlimit.player.ExtendedPlayer.sync(ExtendedPlayer.java:118) ~[ExtendedPlayer.class:?]
at com.happykiller.weightlimit.player.ExtendedPlayer.setMaxWeight(ExtendedPlayer.java:111) ~[ExtendedPlayer.class:?]
at com.happykiller.weightlimit.player.inventory.InventoryWeightLimit.setInventorySlotContents(InventoryWeightLimit.java:110) ~[inventoryWeightLimit.class:?]
at com.happykiller.weightlimit.player.inventory.InventoryWeightLimit.readFromNBT(InventoryWeightLimit.java:180) ~[inventoryWeightLimit.class:?]
at com.happykiller.weightlimit.player.ExtendedPlayer.loadNBTData(ExtendedPlayer.java:58) ~[ExtendedPlayer.class:?]
at net.minecraft.entity.Entity.readFromNBT(Entity.java:1710) ~[Entity.class:?]
at net.minecraft.server.management.ServerConfigurationManager.readPlayerDataFromFile(ServerConfigurationManager.java:300) ~[serverConfigurationManager.class:?]
at net.minecraft.server.management.ServerConfigurationManager.initializeConnectionToPlayer(ServerConfigurationManager.java:123) ~[serverConfigurationManager.class:?]
at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.completeServerSideConnection(NetworkDispatcher.java:239) ~[NetworkDispatcher.class:?]
at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.access$100(NetworkDispatcher.java:51) ~[NetworkDispatcher.class:?]
at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:190) ~[NetworkDispatcher$1.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:270) ~[NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:208) ~[NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:798) ~[MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:669) ~[MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:171) ~[integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:540) ~[MinecraftServer.class:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]

 

It leads to my sync method in the ExtendedPlayer. Which simply sends the client update packet. :/

 

Here's the setInventorySlotContents:

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

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

	this.markDirty();

	if(getStackInSlot(SLOT_BACKPACK) != null && getStackInSlot(SLOT_BACKPACK).getItem() instanceof ItemBackpack) {
		if(!player.worldObj.isRemote) {

			System.out.println("World is not remote");

			ExtendedPlayer props = ExtendedPlayer.get(player);

			if(props.get(player) != null) {

				System.out.println("Player props are not null");

				if(getStackInSlot(SLOT_BACKPACK).getItem() instanceof ItemSmallBackpack) {
					System.out.println("Small Backpack detected!");

					props.setMaxWeight(200);
				}
			}
		}

		System.out.println("BACKPACK IN SLOT!");
	}else {
		if(!player.worldObj.isRemote) {

			ExtendedPlayer props = ExtendedPlayer.get(player);

			if(props.get(player) != null) {
				props.setMaxWeight(150);
			}
		}
	}
}

 

Any idea?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Posted

It looks like you are calling #setInventorySlotContents when loading the inventory from NBT - you should just set the stack directly:

this.inventory[i] = stack;

Otherwise, you are forcing some funky stuff to happen: the inventory is stored in the IEEP class for the player, which is loading itself from NBT and asks the inventory to read itself from NBT, too, but that tries to reference the IEEP class as well as do other things like #markDirty which should never happen until the inventory is actually finished creating itself.

 

TL;DR - using #setter type methods when loading from NBT can actually cause bad things to happen :P

Posted

I'm really sorry to act so ignorant, but are you talking about how I am using #setInventorySlotContents in the #loadFromNBT method in my inventory class?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Posted

Ohhhh, alright so I should manually loop through the slots and fill them with a for() loop?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Posted

I changed it and it seems to be working. :) Here's my new #readFromNBT (just so you can see if there is something that may cause problems in the future):

 

public void readFromNBT(NBTTagCompound tag) {
NBTTagList items = tag.getTagList(tagName, tag.getId());

for(int i = 0; i < items.tagCount(); i++) {
	NBTTagCompound item = items.getCompoundTagAt(i);
            int j = item.getByte("Slot");
            ItemStack stack = ItemStack.loadItemStackFromNBT(item);
            
            if(stack != null) {
            	if(j >= 0 && j < this.inventory.length) {
            		this.inventory[j] = stack;
            	}
            }
       }
}

 

Thank you again for helping me coolAlias! Like I am completely honest, most of my knowledge has come from your tutorials, and your generous help on both this, and the MinecraftForum. Thanks for always helping in a kind manner. :)

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

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.

×
×
  • Create New...

Important Information

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