Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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.

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.

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.

  • Author

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.

  • Author

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.

  • Author

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.

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.

  • Author

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.

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.

  • Author

Thanks for the help. :P Will help me a lot throughout my current mod creation. :)

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.