Jump to content

[1.8][SOLVED] setInventorySlotContents getting the wrong item


HappyKiller1O1

Recommended Posts

So, in my Inventory class for my custom inventory, I am using #setInventorySlotContents to detect an item in the slot, and perform an action accordingly. Problem is, it is only detecting one item. here is what my method looks like:

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

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

	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);
				} 

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

					props.setMaxWeight(250);
				}

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

					props.setMaxWeight(300);
				}
			}
		}

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

			ExtendedPlayer props = ExtendedPlayer.get(player);

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

	this.markDirty();
}

 

The problem is where I am checking for an item in "SLOT_BACKPACK" (it is 0. I just use a constant for organization). The items I check for all extend a base called "ItemBackpack", and I would think using "instanceof" to check for it would be enough. But no matter what of the three items I put, it will always seem to return as the ItemSmallBackpack being in the slot. Am I checking for the items wrong? Would using a switch fix the problem?

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

Why not create a getMaxWeight() method to ItemBackpack, get the item, cast it, then ask the backpack what it's max weight is.

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

In addition to what Draco said, why not use the 'slot' and 'stack' parameters from the method? Is 'SLOT_BACKPACK' the only slot that will affect weight? If so:

if (slot == SLOT_BACKPACK) {
  // you already have the ItemStack in the slot, it is 'stack':
  if (stack == null || !(stack.getItem() instanceof ItemBackpack)) {
    // reset player's carry weight
  } else {
     // example only
     player.carryWeight += ((ItemBackpack) stack.getItem()).getCarryWeight(stack);
  }
}

Even better would be to create an interface such as ICarryWeight - then you could have any number of item types that provide carry weight modifiers.

 

Even better than that would be to create a SharedMonsterAttribute for carry weight, then those same items could be in ANY armor or held-item slot (and possibly Baubles slots, haven't worked with it though) and they would affect your carry weight.

Link to comment
Share on other sites

I was working on this at 4am, so no wonder I forgot about the parameters. :P I honestly have never understood how Interfaces truly work. I know they can help me greatly, but all I know is they are abstract, and used to implement methods I am guessing you call in other classes?

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, I researched interfaces a tad bit, and made one for my base class. And, after all of that, THE SAME ERROR PERSISTS. Like I said, I have three Backpacks that extend the base of ItemBackpack (ItemSmallBackpack, ItemMediumBackpack, and ItemLargeBackpack). Now, I would think, considering these are three separate classes, it wouldn't be too difficult to actually get the right item, I would be wrong. Here are the item classes, the base, the interface, and the new #setInventorySlotContents. Thank you for any help you can provide.

 

ItemSmallBackpack:

package com.happykiller.weightlimit.items;

public class ItemSmallBackpack extends ItemBackpack {

public ItemSmallBackpack() {
	this.setMaxCarryWeight(200);
}
}

 

ItemMediumBackpack:

package com.happykiller.weightlimit.items;

public class ItemMediumBackpack extends ItemBackpack {

public ItemMediumBackpack() {
	this.setMaxCarryWeight(280);
}
}

 

ItemLargeBackpack:

package com.happykiller.weightlimit.items;

public class ItemLargeBackpack extends ItemBackpack {

public ItemLargeBackpack() {
	this.setMaxCarryWeight(380);
}
}

 

ItemBackpack:

package com.happykiller.weightlimit.items;

import java.util.List;

import com.happykiller.weightlimit.main.interfaces.ICarryWeightModifier;
import com.happykiller.weightlimit.player.ExtendedPlayer;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class ItemBackpack extends Item implements ICarryWeightModifier {

protected int maxWeightModifier = 150;

public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
	if(!world.isRemote) {
		ExtendedPlayer props = ExtendedPlayer.get(player);

		if(!player.isSneaking()) {
			props.addWeight(1);
			System.out.println("Current Weight: " + props.getCurrentWeight());
		}else {
			props.removeAllWeight();
			System.out.println("Current Weight: " + props.getCurrentWeight());
		}
	}

	return stack;
}

@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean advanced) {

}

public int setMaxCarryWeight(int newMaxWeight) {
	if(newMaxWeight > 0) {
		this.maxWeightModifier = newMaxWeight;
	}else {
		this.maxWeightModifier = 150;
	}

	return this.maxWeightModifier;
}

public int getMaxCarryWeight() {
	return this.maxWeightModifier;
}

public int getMaxCarryWeightForStack(ItemStack stack) {
	int stackModifier = 150;

	if(stack.getItem() instanceof ItemBackpack) {
		stackModifier = ((ItemBackpack)stack.getItem()).getMaxCarryWeight();
	}else {
		System.out.println("ERROR: Stack was not an extension of ItemBackpack in SLOT_BACKPACK!");
	}

	return stackModifier;
}
}

 

ICarryWeightModifier:

package com.happykiller.weightlimit.main.interfaces;

import net.minecraft.item.ItemStack;

public interface ICarryWeightModifier {

public int setMaxCarryWeight(int newMaxWeight);

public int getMaxCarryWeightForStack(ItemStack stack);

public int getMaxCarryWeight();
}

 

New #setInventorySlotContents:

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

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

	if(slot == SLOT_BACKPACK) {
		if(!player.worldObj.isRemote) {

			ExtendedPlayer props = ExtendedPlayer.get(player);

			if(props.get(player) != null) {
				if(stack == null || !(stack.getItem() instanceof ItemBackpack)) {
					props.setMaxWeight(150);
				}else {
					int newMaxWeight = ((ItemBackpack)stack.getItem()).getMaxCarryWeightForStack(stack);

					System.out.println("Stack in use: " + stack.getItem());

					System.out.println("Max weight from interface is: " + newMaxWeight);

					props.setMaxWeight(newMaxWeight);
				}
			}
		}
	}

	this.markDirty();
}

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

My God, all this time and effort to realize I was registering all three items with the ItemSmallBackpack class. Thank you for showing me an easier way of modifying weight limits, sorry for my idiocy!

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 was working on this at 4am, so no wonder I forgot about the parameters. :P I honestly have never understood how Interfaces truly work. I know they can help me greatly, but all I know is they are abstract, and used to implement methods I am guessing you call in other classes?

 

Interfaces exist to allow multiple objects with different inheritance hierarchies to be treated as "the same" due to the contract granted by the interface.

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

Yes, I went through some Java docs online to figure it out after hearing I should use it. :P Basically, if we have an interface called "IVehicle", and our class "Car" implements it, if a method requires a class from IVehicle as a param, you can give it the Car class and it works, correct?

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

Yes, I went through some Java docs online to figure it out after hearing I should use it. :P Basically, if we have an interface called "IVehicle", and our class "Car" implements it, if a method requires a class from IVehicle as a param, you can give it the Car class and it works, correct?

 

Yep. You could also pass CardboardBoxSpaceShip as long as it also implements IVehicle.

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