Jump to content

[Solved][1.14.4] Adding Potion Effects to Armor Sets


Sauuuuucey

Recommended Posts

Hello all,

 

I'm looking to create a method that checks if armor items from a particular set are all equipped before granting some sort of potion effect so long as they're all worn, like a set bonus. I adapted a method from some older source code, but I'm not getting the desired outcome. Any suggestions?

 

Here's an example for reference:

Spoiler

public class SilverArmor extends ArmorItem 
{
	private final EffectInstance potionEffect;

	public SilverArmor(IArmorMaterial materialIn, EquipmentSlotType slot, final EffectInstance potionEffect ,Properties builder) {
		super(materialIn, slot, builder);
		this.potionEffect = potionEffect;
	}

Builder/Properties

Spoiler

    public void onArmorTick(World world, PlayerEntity player, ItemStack item) {
		if(player.inventory.armorItemInSlot(0) 
				!= null && player.inventory.armorItemInSlot(1) 
				!= null && player.inventory.armorItemInSlot(2) 
				!= null && player.inventory.armorItemInSlot(3) 
				!= null) 
		{
			if(player.inventory.armorItemInSlot(0).getItem() == ModItems.silver_chestplate && player.inventory.armorItemInSlot(1).getItem() == ModItems.silver_leggings && player.inventory.armorItemInSlot(2).getItem() == ModItems.silver_boots && player.inventory.armorItemInSlot(3).getItem() == ModItems.silver_helmet) {				
				player.addPotionEffect(new EffectInstance(potionEffect));
			}
		}
	}

Method that checks if all pieces are being worn

Spoiler

	@SubscribeEvent
	public static void onRegisterItems(final RegistryEvent.Register<Item> event) {
		final IForgeRegistry<Item> registry = event.getRegistry();
		
		registry.registerAll
		(
				new SilverArmor(ModArmorMaterials.silver, EquipmentSlotType.HEAD, new EffectInstance(Effects.INVISIBILITY), new Item.Properties().group(ModGroup.MOD_ITEM_GROUP)).setRegistryName("silver_helmet"),
				new SilverArmor(ModArmorMaterials.silver, EquipmentSlotType.CHEST, new EffectInstance(Effects.INVISIBILITY), new Item.Properties().group(ModGroup.MOD_ITEM_GROUP)).setRegistryName("silver_chestplate"),
				new SilverArmor(ModArmorMaterials.silver, EquipmentSlotType.LEGS, new EffectInstance(Effects.INVISIBILITY), new Item.Properties().group(ModGroup.MOD_ITEM_GROUP)).setRegistryName("silver_leggings"),
				new SilverArmor(ModArmorMaterials.silver, EquipmentSlotType.FEET, new EffectInstance(Effects.INVISIBILITY), new Item.Properties().group(ModGroup.MOD_ITEM_GROUP)).setRegistryName("silver_boots")
				
		);
	}

Registry with placeholder effect

 

Link to comment
Share on other sites

Figured it out after a bit of testing/digging through some references. Here's the updated method example in case anyone was wondering:

Spoiler

    @Override
    public void onArmorTick(ItemStack stack, World world, PlayerEntity player) {
		if(player.getItemStackFromSlot(EquipmentSlotType.HEAD).getItem() == ModItems.silver_helmet && 
			player.getItemStackFromSlot(EquipmentSlotType.CHEST).getItem() == ModItems.silver_chestplate && 
			player.getItemStackFromSlot(EquipmentSlotType.LEGS).getItem() == ModItems.silver_leggings && 
			player.getItemStackFromSlot(EquipmentSlotType.FEET).getItem() == ModItems.silver_boots) 
		{				
				player.addPotionEffect(new EffectInstance(Effects.INVISIBILITY));
		}
	}

 

 

Link to comment
Share on other sites

  • Sauuuuucey changed the title to [Solved][1.14.4] Adding Potion Effects to Armor Sets
  • 5 months later...
  • 4 months later...

I think you could also get away with leaving it more generic like this example:

 

    @Override
    public void onArmorTick(ItemStack stack, World world, PlayerEntity player) {
      if(
        !player.getItemStackFromSlot(EquipmentSlotType.HEAD).isEmpty() &&
        !player.getItemStackFromSlot(EquipmentSlotType.CHEST).isEmpty() &&
        !player.getItemStackFromSlot(EquipmentSlotType.LEGS).isEmpty() &&
        !player.getItemStackFromSlot(EquipmentSlotType.FEET).isEmpty()
      )
      {
        player.addPotionEffect(new EffectInstance(Effects.INVISIBILITY));
      }
    }

This way if you create more armors you don't have to write new conditions to check more items. Although, yours is better if you want to guarantee that all armor pieces match the suit in their slots.

Edited by TheSarlaacSweep
It's more of a variation rather than an improvement.
Link to comment
Share on other sites

15 hours ago, TheSarlaacSweep said:

I think you could also get away with leaving it more generic like this example:

 


    @Override
    public void onArmorTick(ItemStack stack, World world, PlayerEntity player) {
      if(
        !player.getItemStackFromSlot(EquipmentSlotType.HEAD).isEmpty() &&
        !player.getItemStackFromSlot(EquipmentSlotType.CHEST).isEmpty() &&
        !player.getItemStackFromSlot(EquipmentSlotType.LEGS).isEmpty() &&
        !player.getItemStackFromSlot(EquipmentSlotType.FEET).isEmpty()
      )
      {
        player.addPotionEffect(new EffectInstance(Effects.INVISIBILITY));
      }
    }

This way if you create more armors you don't have to write new conditions to check more items. Although, yours is better if you want to guarantee that all armor pieces match the suit in their slots.

Slots not being empty != the player is wearing the specified armor piece.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

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.