Jump to content

Recommended Posts

Posted

In the next Forge release will it be possible to create an Interface for the following method in ItemStack.class:

onFoodEaten.

 

Want to do stuff when a item is eaten but don't want to edit those classes or ItemStack.class.

Posted

I made a Thirst Mod with another friend. I just want that when you drink from a either a potion, milk bucket or soup to add stats to the thirst bar. I was previously able to do it by editing their respective classes but I figured out that with the following code I was able to add stats from ItemStack.class/onFoodEaten:

 

if(getItem()) == Item.potion) {
       Utilities.getStats.addStats(10, 2f);
}

 

I don't want to edit any classes so thats why I was asking for an interface. Otherwise the only way is to edit ItemStack.class or the drink classes. Thanks

Posted

Why didn't I think of that earlier. Java Reflection works perfectly except for the conflict that pops up when the game is loading. The code for anyone else who may need it.

 

 

 

try {
		Class item = Item.class;
		Field soup = item.getField("bowlSoup");
		Item soupObj = (new ItemSoupMod(26, ).setIconCoord(8, 4).setItemName("mushroomStew");
		soup.set(Item.bowlSoup, soupObj);
	} catch (SecurityException e) {
		e.printStackTrace();
	} catch (NoSuchFieldException e) {
		e.printStackTrace();
	} catch (IllegalArgumentException e) {
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		e.printStackTrace();
	}

 

 

 

You've got your thanks OverMind. Thanks a lot.

Posted

Why didn't I think of that earlier. Java Reflection works perfectly except for the conflict that pops up when the game is loading. The code for anyone else who may need it.

 

 

 

try {
		Class item = Item.class;
		Field soup = item.getField("bowlSoup");
		Item soupObj = (new ItemSoupMod(26, ).setIconCoord(8, 4).setItemName("mushroomStew");
		soup.set(Item.bowlSoup, soupObj);
	} catch (SecurityException e) {
		e.printStackTrace();
	} catch (NoSuchFieldException e) {
		e.printStackTrace();
	} catch (IllegalArgumentException e) {
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		e.printStackTrace();
	}

 

 

 

You've got your thanks OverMind. Thanks a lot.

 

as a general rule of thumb for programming like this, is it not acceptable to just use just one catch(Exception e) for the try-catch methods? i thought Exception caught all Errors?

Posted

You will not get that conflict if you null out the id cell of the itemList first.  ;)

 

Do note, will not work with other mods that do the same, but unsure if any exist, so probably good for now.

Posted

Okay done and all finished. Thanks so much!

 

Heres the code for anyone:

 

 

try {
		Item.itemsList[282] = null;
		Item.itemsList[373] = null;
		Item.itemsList[335] = null;

		Class item = Item.class;
		Field soup = item.getField("bowlSoup");
		Item soupObj = (new ItemSoupMod(26, ).setIconCoord(8, 4).setItemName("mushroomStew");
		soup.set(Item.bowlSoup, soupObj);

		Class itemPotion = ItemPotion.class;
		Field potion = itemPotion.getField("potion");
		ItemPotion potionObj = (ItemPotion)(new ItemPotionMod(117)).setIconCoord(13, .setItemName("potion");
		potion.set(ItemPotion.potion, potionObj);

		Field bucket = item.getField("bucketMilk");
		Item bucketMilk = (new ItemBucketMilkMod(79)).setIconCoord(13, 4).setItemName("milk").setContainerItem(Item.bucketEmpty);
		bucket.set(Item.bucketMilk, bucketMilk);
	} catch (Exception e) {
		e.printStackTrace();
	}

 

Nulling it out means that this method will also work with Blocks without the game crashing. Sweet!

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.