Jump to content

[1.7.10] Checking all armor a player is wearing at a given tick


onContentStop

Recommended Posts

I'm making a simple mod that adds new ores and I want to apply a custom status effect only when the player is wearing a full set of my armor. However, I've only succeeded in checking if ONE armor piece is equipped by putting the code in the custom helmet class, chestplate class, etc. How do I check the full equip list of a player in one tick and apply status effects based on that?

Link to comment
Share on other sites

you could subscribe to LivingUpdateEvent and, if event.livingEntity is of type EntityPlayer, then you cast it to EntityPlayer, get EntityPlayer.inventory.armorInventory and check each slot.

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Link to comment
Share on other sites

Thanks for replying, but I realized I was being unnecessarily stupid. I made a general Armor class and overrode onArmorTick() there to check each individual slot. Here's the code in progress:

@Override
public void onArmorTick (World world, EntityPlayer player, ItemStack stack) {
	int aAmt = 0;//amethyst = regen
	int aCap = 3;
	int sAmt = 0;//sapphire = resist
	int sCap = 3;
	int rAmt = 0;//ruby = strength
	int rCap = 3;
	int tAmt = 0;//topaz = haste
	int tCap = 3;

	//checks for amethyst
	if(player.getCurrentArmor(0) != null && player.getCurrentArmor(0).getItem().equals(oncontentstop.amethystBoots))
		aAmt++;
	if (player.getCurrentArmor(1) != null && player.getCurrentArmor(1).getItem().equals(oncontentstop.amethystLeggings))	
		aAmt++;
	if(player.getCurrentArmor(2) != null && player.getCurrentArmor(2).getItem().equals(oncontentstop.amethystChestplate))
		aAmt++;	
	if(player.getCurrentArmor(3) != null && player.getCurrentArmor(3).getItem().equals(oncontentstop.amethystHelmet))
		aAmt++;
	if(aAmt > 0)
		player.addPotionEffect(new PotionEffect(Potion.regeneration.id, 5, aAmt > aCap ? aAmt - 1 : aCap));

	//checks for sapphire
	if(player.getCurrentArmor(0) != null && player.getCurrentArmor(0).getItem().equals(oncontentstop.sapphireBoots))
		sAmt++;
	if(player.getCurrentArmor(1) != null && player.getCurrentArmor(1).getItem().equals(oncontentstop.sapphireLeggings))
		sAmt++;
	if(player.getCurrentArmor(2) != null && player.getCurrentArmor(2).getItem().equals(oncontentstop.sapphireChestplate))
		sAmt++;
	if(player.getCurrentArmor(3) != null && player.getCurrentArmor(3).getItem().equals(oncontentstop.sapphireHelmet))
		sAmt++;
	if(sAmt > 0)
		player.addPotionEffect(new PotionEffect(Potion.resistance.id, 5, sAmt > sCap ? sAmt - 1 : sCap));

	//checks for ruby
	if(player.getCurrentArmor(0) != null && player.getCurrentArmor(0).getItem().equals(oncontentstop.rubyBoots))
		rAmt++;
	if(player.getCurrentArmor(1) != null && player.getCurrentArmor(1).getItem().equals(oncontentstop.rubyLeggings))
		rAmt++;
	if(player.getCurrentArmor(2) != null && player.getCurrentArmor(2).getItem().equals(oncontentstop.rubyChestplate))
		rAmt++;
	if(player.getCurrentArmor(3) != null && player.getCurrentArmor(3).getItem().equals(oncontentstop.rubyHelmet))
		rAmt++;
	if(rAmt > 0)
		player.addPotionEffect(new PotionEffect(Potion.damageBoost.id, 5, rAmt > rCap ? rAmt - 1 : rCap));

	//checks for topaz
	if(player.getCurrentArmor(0) != null && player.getCurrentArmor(0).getItem().equals(oncontentstop.topazBoots))
		tAmt++;
	if(player.getCurrentArmor(1) != null && player.getCurrentArmor(1).getItem().equals(oncontentstop.topazLeggings))
		tAmt++;
	if(player.getCurrentArmor(2) != null && player.getCurrentArmor(2).getItem().equals(oncontentstop.topazChestplate))
		tAmt++;
	if(player.getCurrentArmor(3) != null && player.getCurrentArmor(3).getItem().equals(oncontentstop.topazHelmet))
		tAmt++;
	if(tAmt > 0)
		player.addPotionEffect(new PotionEffect(Potion.digSpeed.id, 5, tAmt > tCap ? tAmt - 1 : tCap));
}

But just to make sure, am I making any grave mistakes here?

 

edit: added code

Link to comment
Share on other sites

I think this will run once for every armor piece at every tick, causing it to run 4 times if the player has the item that implements that method in every armor slot.

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Link to comment
Share on other sites

what I was suggesting was something like this:

 

an approach to the custom armor class. I don't know how yours is. this is just an idea that would work well with my suggestion

public class MyCustomArmorClass extends ItemArmor
{
public static final int AMETHYST_ID = 0;
public static final int SAPPHIRE_ID = 1;
public static final int RUBY_ID = 2;
public static final int TOPAZ_ID = 3;

public static final int[] gemEffects;

static
{
	gemEffects = new int[4];
	gemEffects[AMETHYST_ID] = Potion.regeneration.id;
	gemEffects[sAPPHIRE_ID] = Potion.resistance.id;
	gemEffects[RUBY_ID] = Potion.damageBoost.id;
	gemEffects[sAPPHIRE_ID] = Potion.digSpeed.id;
}

public final int ID;

public MyCustomArmorClass(ArmorMaterial armorMaterial, int armorType, int renderIndex, int gemID)
{
	super(armorMaterial, armorType, renderIndex);
	this.ID = gemID;
}

        //and the rest of your code
}

 

then the event handler class:

public class ArmorEventHandler
{
public ArmorEventHandler(){	}

@SubscribeEvent
public void onLivingUpdate(LivingUpdateEvent event)
{
	if(event.entityLiving instanceof EntityPlayer)
	{
		EntityPlayer player = (EntityPlayer)event.entityLiving;

		ItemStack[] armorInv = player.inventory.armorInventory;

		int[] armorCount = new int[4];

		for(ItemStack armorStack : armorInv)
		{
			if(armorStack != null && armorStack.getItem() instanceof MyCustomArmorClass)
			{
				MyCustomArmorClass armorItem = (MyCustomArmorClass)armorStack.getItem();
				armorCount[armorItem.ID]++;
			}
		}

		for(int i = 0; i < 4; ++i)
		{
			int count = armorCount[i];
			if(count > 0)
			{
				player.addPotionEffect(new PotionEffect(MyCustomArmorClass.gemEffects[i], 1, count > 3 ? count : 3));
			}
		}
	}
}
}

 

and don't forget to register an instance of your event handler class to the MinecraftForge.EVENT_BUS. i think maybe because of the fact that this would run only once per tick it would be more efficient and less prone to unwanted side effects.

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

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.