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

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.

  • Author

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

  • Author

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.

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?

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.

Item.java has an array of like 32000 in size or so, where all items are linked to by ID, just set the cell of the ID you are overwriting to null before you new your item.  :)

  • Author

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!

  • Author

Yeah okay, I'll be using it for the cauldron cause we have our own clean water cauldron. No more base classes edited!

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.