Jump to content

Recommended Posts

Posted

Hey all, I'm having a little trouble getting the unlocalised name of an itemstack passed in to a function.

 

I've tried a few methods like:

String itemName = inputStack.getItem().getUnlocalizedName();

and

String itemName = GameData.getItemRegistry().getItemName(inputStack.getItem()); 

 

but the most I have had is a NullPointer exception for the former and "item.null" for the latter.

I've been testing it with:

public static String getItemType(ItemStack inputStack)
{
//see examples above
return itemName;
}

and

System.out.println(getItemType(new ItemStack(Items.emerald,64)));

 

Any ideas what's going on? I'm a little bit baffled..

 

For more information: I need to be able to identify the string of what the itemstack contains and I'll be using a switch case to select which offset to apply to the nonary integer, corresponding to Emerald blocks, items, Gold blocks, ingots or nuggets, as laid out here:

https://github.com/Minothor/EmeraldCasino/wiki/Financial

 

Many thanks in advance!

-Mino

Posted

I'm afraid it's still not working, sorry. Here's the full code (now that I'm back at home (it's an uncommitted change at the moment):

 

package EmeraldCasino.financial;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

public final class ItemHandler {
private static final Block BLOCKEMERALD = Blocks.emerald_block;
private static final Item EMERALD = Items.emerald;
private static final Block BLOCKGOLD = Blocks.gold_block;
private static final Item GOLD = Items.gold_ingot;
private static final Item NUGGETGOLD = Items.gold_nugget;


private ItemHandler() {
}

public static int depositItems(List<ItemStack> inputItems)
{	
	int result = 0;
	int emBlocks = 0;
	int emItems =  0;
	int goBlocks =  0;
	int goItems  =  0;
	int goNuggets  =  0;
	// TODO Verify Item types from the list.
	for (ItemStack itemStack : inputItems) {
		Item itemType = itemStack.getItem();
		int itemNum = itemStack.stackSize;


		if(itemType == Item.getItemFromBlock(BLOCKEMERALD)){
			emBlocks+=itemNum;
			System.out.println("Added "+itemNum+" Emerald Blocks");
		} else	if(itemType == EMERALD){
			emItems+=itemNum;
			System.out.println("Added "+itemNum+" Emeralds");
		} else if(itemType == Item.getItemFromBlock(BLOCKGOLD)){
			goBlocks+=itemNum;
			System.out.println("Added "+itemNum+" Gold Blocks");
		} else if(itemType == GOLD){
			goItems+=itemNum;
			System.out.println("Added "+itemNum+" Gold Ingots");
		} else if(itemType == NUGGETGOLD){
			goNuggets+=itemNum;
			System.out.println("Added "+itemNum+" Gold Nuggets");
		}

	}


	//CleanUp time:		
	//Checking if Emeralds can be compressed into one stack
			if (emItems>{
				emBlocks+=(emItems/9);
				emItems%=9;
			}
			//Checking if Gold Nuggets can be compressed into Blocks
			if (goNuggets>{
				goItems+=(goNuggets/9);
				goNuggets%=9;
			}
	//Checking if Gold can be compressed into one stack
			if (goItems>{
				goBlocks+=(goItems/9);
				goItems%=9;
			}


			result=Nonary.toNonary(emBlocks*6561);
			result=Nonary.add(result, Nonary.toNonary(emItems*729));
			result=Nonary.add(result, Nonary.toNonary(goBlocks*81));
			result=Nonary.add(result, Nonary.toNonary(goItems*9));
			result=Nonary.add(result, Nonary.toNonary(goNuggets));

	return result;
}

/**
 * 
 * @param inputNonary The Nonary value of items being withdrawn.
 * @return List of ItemStacks
 */
public static List<ItemStack> withdrawItems(int inputNonary)
{	
	System.out.println("Integer "+inputNonary+" Translates into:");
	List<ItemStack> ReturnItems = new ArrayList<>(5);

	//safety checks
	int emBlocks = (inputNonary/10000);
	if(emBlocks<0) emBlocks=0;

	int emItems =  (inputNonary-=(emBlocks*10000))/1000;
	if(emItems<0) emItems=0;

	int goBlocks =  (inputNonary-=(emItems*1000))/100;
	if(goBlocks<0) goBlocks=0;

	int goItems  =  (inputNonary-=(goBlocks*100))/10;
	if(goItems<0) goItems=0;

	int goNuggets  =  (inputNonary-=(goItems*10));
	if(goNuggets<0) goNuggets=0;

	//CleanUp time:
	//Checking if Emeralds can be compressed into one stack
			if (((emBlocks*9)+emItems)<=64){
				emItems+=(emBlocks*9);
				emBlocks=0;
			}
	//Checking if Gold can be compressed into one stack
			if (((goBlocks*9)+goItems)<=64){
				goItems+=(goBlocks*9);
				goBlocks=0;
			}


	System.out.println("Emeralds:\nBlocks: "+emBlocks+"\nItems: "+emItems);
	System.out.println("Gold:\nBlocks: "+goBlocks+"\nIngots: "+goItems+"\nNuggets: "+goNuggets);


	while(emBlocks>64){
		ReturnItems.add(new ItemStack(BLOCKEMERALD, 64));
		emBlocks-=64;
	}
	if(emBlocks>0)
	ReturnItems.add(new ItemStack(BLOCKEMERALD, emBlocks));

	if(emItems>0)
	ReturnItems.add(new ItemStack(EMERALD, emItems));

	if(goBlocks>0)
	ReturnItems.add(new ItemStack(BLOCKGOLD, goBlocks));

	if(goItems>0)
	ReturnItems.add(new ItemStack(GOLD, goItems));

	if(goNuggets>0)
	ReturnItems.add(new ItemStack(NUGGETGOLD, goNuggets));

	return ReturnItems;
}

public static void main(String[] args) {
	long start = System.currentTimeMillis();
	System.out.println("Returned Nonary: "+depositItems(withdrawItems(62001)));
	long end = System.currentTimeMillis();
	System.out.println((end - start) + " ms");
}
}

 

package EmeraldCasino.financial;
/**
* Base-9 calculations library for working with values based off gold and emerald items.
* @author Minothor
* @version 1.0
*/
public final class Nonary {

private Nonary() {}

private static int changeBase (int value, int base1, int base2){
	/*
	 * Credit for this simple function goes to http://professorjava.weebly.com/
	 * for their easy to follow base conversion calculator.
	 */
	int intermediary = Integer.parseInt(""+value, base1);
	int result = Integer.valueOf(Integer.toString(intermediary, base2));
	return result;
}

/**
 * converts a Decimal integer into it's equivalent Nonary form.
 * @param decimal Integer to convert.
 * @return returns the Nonary equivalent.
 */
public static int toNonary(int decimal){
	return changeBase(decimal, 10, 9);
}

/**
 * converts a Nonary integer into it's equivalent Decimal form.
 * @param nonary Integer to convert.
 * @return the Decimal equivalent.
 */
public static int toDecimal(int nonary){
	return changeBase(nonary, 9, 10);
}

/**
 * Adds the second value to the first.
 * (done since operand overloading isn't possible in Java)
 * @param value1 Nonary integer.
 * @param value2 Nonary integer.
 * @return Integer sum of inputs.
 */
public static int add(int value1, int value2){
	int result =  toDecimal(value1)+toDecimal(value2);
	return toNonary(result);
}

/**
 * Substracts the second value from the first.
 * (done since operand overloading isn't possible in Java)
 * @param value1 Nonary integer.
 * @param value2 Nonary integer.
 * @return Integer result of subtraction.
 */
public static int subtract(int value1, int value2){
	int result =  toDecimal(value1)-toDecimal(value2);
	return toNonary(result);
}

/**
 * Multiplies first value by the second.
 * (done since operand overloading isn't possible in Java)
 * @param value1 Nonary integer.
 * @param value2 Nonary integer.
 * @return Integer product of multiplication.
 */
public static int multiply(int value1, int value2){
	int result = toDecimal(value1)*toDecimal(value2);
	return toNonary(result);
}

/**
 * Divides first value by the second.
 * (done since operand overloading isn't possible in Java)
 * @param value1 Nonary integer.
 * @param value2 Nonary integer.
 * @return Integer result of division.
 */
public static int divide(int value1, int value2){
	int result = toDecimal(value1)/toDecimal(value2);
	return toNonary(result);
}

/**
 * Remainder of dividing the first value by the second.
 * (done since operand overloading isn't possible in Java)
 * @param value1 Nonary integer.
 * @param value2 Nonary integer.
 * @return Integer result of modulo.
 */
public static int modulo(int value1, int value2){
	int result = toDecimal(value1)%toDecimal(value2);
	return toNonary(result);
}
}

 

the console output it from ItemHandler's main is as follows:

Integer 62001 Translates into:

Emeralds:

Blocks: 0

Items: 56

Gold:

Blocks: 0

Ingots: 0

Nuggets: 1

Added 56 Emerald Blocks

Added 1 Emerald Blocks

Returned Nonary: 630000

1 ms

 

This output remains the same whether I use the constants declared at the top or "Items.emerald" etc declared directly after the "=="

 

 

 

Any idea what I'm screwing up here?

Posted

First off, is there some reason why you are throwing around all the 'final' & 'static' uses?  Unless you have to have those, you really shouldn't use them.

 

 

Did you include what is calling depositItems?  I didn't see the code below.  Are you absolutely sure you have anything other than what it found in the list you sent it?

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

I'm using Final and Static because the Financial package is aimed at being a helper library that shouldn't have multiple instances. It's use is purely to perform operations on nonary integers and convert between Itemstacks and a nonary representation of the item values.

 

the piece that's calling both functions is in the temporary Main() function at the bottom of ItemHandler.java (in-class testing, will be removed)

System.out.println("Returned Nonary: "+depositItems(withdrawItems(62001)));

Posted

Sorry to bump this up so soon, but having swapped around the if() tests for emerald blocks and nuggets, it's become clear that it is just executing the first if(), whichever it is.

The actual class that it's testing for doesn't seem to matter and using the .getClass() inherited from Object only results in a NullPointer, I'm getting really, really confused now..

 

Is ItemStack.getItem() unused or am I using it wrong?

Posted

Your code assumes that your statically initialized final references will be set after the Blocks class static final initializers have been set. There is no way to guarantee this is true. So, you'd best not use them that way. In the proper event handler, grab the values you need and assign them That way, you will know they already have their proper values from the registry.

Posted

Cheers Sequiturian, I'll test it via a temporary block in game later today.

Also, simply put Sieben, it never even crossed my mind that I was referencing constants in the first place, which, in hindsight, was pretty stupid, you're right. I'll fix that when I get home.

 

Edit: cheers all, I tested it in the PostInit phase and it's working fine! A big thanks to everyone who responded!

 

Integer 62001 Translates into:

Items.emeralds:

Blocks: 0

Items: 56

Gold:

Blocks: 0

Ingots: 0

Nuggets: 1

Added 56 Items.emeralds

Added 1 Gold Nuggets

Returned Nonary: 62001

1 ms

[12:48:19] [Client thread/INFO]: Forge Mod Loader has successfully loaded 4 mods

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



×
×
  • Create New...

Important Information

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