Add API method for protection amount on IArmorApiarist (#122)

This commit is contained in:
FourIsTheNumber authored and GitHub committed 2026-06-15 01:19:34 -04:00
1 parent bbb374cd5e
commit 974e44f5cd
2 files changed
+18 -13

No files matched your search

@@ -25,6 +25,19 @@ public interface IArmorApiarist {
*/
boolean protectEntity(EntityLivingBase entity, ItemStack armor, String cause, boolean doProtect);
/**
* Called to determine how many pieces an armor "counts for". For instance, if this returns 4, the single piece will
* act as full protection.
*
* @param entity Entity being attacked
* @param armor Armor item
* @param cause Optional cause of attack, such as a bee effect identifier
* @return Amount of effective protected slots
*/
default int getProtectionCount(EntityLivingBase entity, ItemStack armor, String cause) {
return 1;
}
/**
* Called when the apiarist's armor acts as protection against an attack.
*
@@ -10,7 +10,6 @@ package forestry.apiculture;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import forestry.api.apiculture.IArmorApiarist;
@@ -20,21 +19,14 @@ public class ArmorApiaristHelper implements IArmorApiaristHelper {
@Override
public boolean isArmorApiarist(ItemStack stack, EntityLivingBase entity, String cause, boolean doProtect) {
if (stack == null) {
return false;
}
Item item = stack.getItem();
if (!(item instanceof IArmorApiarist)) {
return false;
}
if (stack == null || !(stack.getItem() instanceof IArmorApiarist armor)) return false;
IArmorApiarist armorApiarist = (IArmorApiarist) item;
try {
return armorApiarist.protectEntity(entity, stack, cause, doProtect);
return armor.protectEntity(entity, stack, cause, doProtect);
} catch (Throwable ignored) { // protectEntity is new to the API and may not be implemented by the armor
if (entity instanceof EntityPlayer) {
return armorApiarist.protectPlayer((EntityPlayer) entity, stack, cause, doProtect);
return armor.protectPlayer((EntityPlayer) entity, stack, cause, doProtect);
} else {
return true; // fallback on assuming IArmorApiarist will protect the entity
}
@@ -48,11 +40,11 @@ public class ArmorApiaristHelper implements IArmorApiaristHelper {
for (int i = 1; i <= 4; i++) {
ItemStack armorItem = entity.getEquipmentInSlot(i);
if (isArmorApiarist(armorItem, entity, cause, doProtect)) {
count++;
count += ((IArmorApiarist) armorItem.getItem()).getProtectionCount(entity, armorItem, cause);
}
}
return count;
return Math.min(count, 4);
}
@Override