Jump to content

[1.8] Checking if an item is in a custom slot


HappyKiller1O1

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They are heartless scammers, they ruined my life, by making me develop an interest in investing my hard-earned money. I deposited 67,000 USD which was later turned into 110,450 USD including my payout bonus, there was an impressive improvement in a few weeks, later I had an emergency and needed money to pay my insurance fee, I tried reaching out to them to collect the money I invested, they cut the live chats and got me harassed then I realized that I was being scammed. I just wanted my money back! I was advised by a friend to seek help from a recovery firm to assist me in recovering my invested funds, God was so kind I came across some great positive reviews about BETAFORT ONLINE on how they helped scammed victims like me to recover their lost cryptocurrency assets, I took no delay and got in touch with BETAFORT ONLINE, the Expert immediately looked into my case and after providing all the required information they need, and it took them less then a day to recover all my funds. And now I really feel obligated to recommend BETAFORT ONLINE and their team, their recovery strategies, and for working relentlessly to help me recover my funds. Feel free to reach out to BETAFORT ONLINE via google search and they will guide you on how to recover your invested funds, I advise everyone to be careful with these heartless cryptocurrency scammers.  
    • yeah I believe so, its been something deep with the modpack I'm guessing. ill fix each error it states if its saying to replace or download a mod but I'm not sure where to go from here. I have been spending days and have yet to find the issue    
    • It worked, thank you very much 😭
    • https://spark.lucko.me/0ZaR1OAi2F I use spark mod to do a scan and send it here to search for help and if someone know about this issue I dont know to much about, but one thing i see in the record of the minecraft launcher when i play says: can't keep up server overloaded and etc, etc.  I assign 6GB ram to minecraft from the 16 GB of my pc, and fps is not a problem...(no to much) its something about tic rate/second and i am in SINGLE PLAYER. The funny thing is, minecraft vanilla (No mods) works normaly. My mods list (130): https://imgur.com/a/kY3yQr1  Datapacks: https://imgur.com/CTDBe7D ResourcePacks: https://imgur.com/AztTOOw so plis, is someone help me T_T a work  a loot of time in downloading and trying to all mods work but i dont know what to do anymore. Last thing  I SPEAK SPANISH so... sorry if my english is a little scuff or ... werever... bad. Thanks :3
    • Whenever I try to create an world my game crashes. I´ve tried to figure it out myself but wasnt able to do it.   Here are the logs
  • Topics

×
×
  • Create New...

Important Information

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