Jump to content

OnSlotChanged /onItemArmorEquip event


toss

Recommended Posts

Hey I was thinking that it could be great if forge can handle these 2 events

 

They would be called when a slot from the player inventory or armor inventory changed

 

Because actually, the only way I found to check armor changes is to check the player stuff every tick, which is quite hardcodded

 

(I hope my English wasn't too awful)

Link to comment
Share on other sites

It's not hard to check. PlayerTickEvent or LivingUpdateEvent, with a flag set to true. It would look something like this:

 

private boolean flag = true;

@SubscribeEvent
public void onPlayerTick(TickEvent.PlayerTickEvent event)
{
if(event.phase == TickEvent.Phase.START && event.side == Side.SERVER)
{
	if(flag)
	{
		if(event.player.inventory.armorInventory[0] == MyItem)
		{
			flag = false;
			// DO STUFF
		}
	}
	else
	{
		if(event.player.inventory.armorInventory[0] == null)
		{
			flag = true;
		}
	}
}
}

 

This is a very minimal amount of processing required to be done every tick, I doubt it would ever interfere with performance.

BEFORE ASKING FOR HELP READ THE EAQ!

 

I'll help if I can. Apologies if I do something obviously stupid. :D

 

If you don't know basic Java yet, go and follow these tutorials.

Link to comment
Share on other sites

public Item[] armor = new Item[4];


public void updateArmor()
{
	 for (int i = 0; i < 4; i++)
	 {
		 ItemStack is = this.player.inventory.armorInventory[i];
		 if (is != null)
		 {
			 Item item = is.getItem();
			 if (item != this.armor[i])
			 {
				 if (item instanceof ItemArmorM)
				 {
                                                  //the player equipped my item
				 }
				 this.armor[i] = item;
			 }	 
		 }
		 else if (is == null && this.armor[i] != null)
		 {
			 if (this.armor[i] instanceof ItemArmorM)
			 {
                                        //The player unequipped my item
			 }
			 this.armor[i] = null;
		 }
	 }		
}

 

I never said this code was hard to made...

 

I'm just suggesting to create this event, which would necessarily improve game performance

 

EDIT: i mean, if a player install 10 armor mods, it is non sense checking this 10 differents times :(

Link to comment
Share on other sites

A more effective solution would be:

A. add an event PlayerEvent.armorChangeEvent with properties Item oldArmor, newArmor

B. Post the even in these methods:

  • InventoryPlayer#clearInventory - armor removed
  • InventoryPlayer#copyInventory - armor changed
  • InventoryPlayer#damageArmor - armor removed (if damaged beyond repair)
  • InventoryPlayer#decrStackSize - when stack is armorInventory slot
  • InventoryPlayer#dropAllItems - obviously, armor removed
  • InventoryPlayer#getStackInSlotOnClosing - why this removes armor is a mystery
  • InventoryPlayer#setInventorySlotContents - change, add, or remove
  • InventoryPlayer#readFromNBT - donning armor

 

Then armor can listen for the change and add/remove effects.

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



×
×
  • Create New...

Important Information

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