From 5b801b134ba80572481c3faea3fa1f453e3c62c4 Mon Sep 17 00:00:00 2001 From: KrisHD1337 <165958879+krishd1337@users.noreply.github.com> Date: Wed, 28 May 2025 14:16:27 +0200 Subject: [PATCH] Added effect for Stalinium chestplate and leggings --- gradle.properties | 2 +- .../krituximon/stalinium/item/ModItems.java | 4 +- .../StaliniumChestplateLeggingsItem.java | 107 ++++++++++++++++++ 3 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/krituximon/stalinium/item/custom/StaliniumChestplateLeggingsItem.java diff --git a/gradle.properties b/gradle.properties index 20e2558..6554403 100644 --- a/gradle.properties +++ b/gradle.properties @@ -34,7 +34,7 @@ mod_name=Stalinium # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=MIT # The mod version. See https://semver.org/ -mod_version=0.0.17 +mod_version=0.0.18 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/net/krituximon/stalinium/item/ModItems.java b/src/main/java/net/krituximon/stalinium/item/ModItems.java index 01c9ce7..57819f0 100644 --- a/src/main/java/net/krituximon/stalinium/item/ModItems.java +++ b/src/main/java/net/krituximon/stalinium/item/ModItems.java @@ -45,11 +45,11 @@ public class ModItems { new Item.Properties().durability(ArmorItem.Type.HELMET.getDurability(19)))); public static final DeferredItem STALINIUM_CHESTPLATE = ITEMS.register("stalinium_chestplate", - () -> new ArmorItem(ModArmorMaterials.STALINIUM_ARMOR_MATERIAL, ArmorItem.Type.CHESTPLATE, + () -> new StaliniumChestplateLeggingsItem(ModArmorMaterials.STALINIUM_ARMOR_MATERIAL, ArmorItem.Type.CHESTPLATE, new Item.Properties().durability(ArmorItem.Type.CHESTPLATE.getDurability(19)))); public static final DeferredItem STALINIUM_LEGGINGS = ITEMS.register("stalinium_leggings", - () -> new ArmorItem(ModArmorMaterials.STALINIUM_ARMOR_MATERIAL, ArmorItem.Type.LEGGINGS, + () -> new StaliniumChestplateLeggingsItem(ModArmorMaterials.STALINIUM_ARMOR_MATERIAL, ArmorItem.Type.LEGGINGS, new Item.Properties().durability(ArmorItem.Type.LEGGINGS.getDurability(19)))); public static final DeferredItem STALINIUM_BOOTS = ITEMS.register("stalinium_boots", diff --git a/src/main/java/net/krituximon/stalinium/item/custom/StaliniumChestplateLeggingsItem.java b/src/main/java/net/krituximon/stalinium/item/custom/StaliniumChestplateLeggingsItem.java new file mode 100644 index 0000000..c701f0d --- /dev/null +++ b/src/main/java/net/krituximon/stalinium/item/custom/StaliniumChestplateLeggingsItem.java @@ -0,0 +1,107 @@ +package net.krituximon.stalinium.item.custom; + +import com.google.common.collect.ImmutableMap; +import net.krituximon.stalinium.item.ModArmorMaterials; +import net.minecraft.core.Holder; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.effect.MobEffectInstance; +import net.minecraft.world.effect.MobEffects; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ArmorItem; +import net.minecraft.world.item.ArmorMaterial; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraft.world.phys.AABB; + +import java.util.List; +import java.util.Map; + +public class StaliniumChestplateLeggingsItem extends ArmorItem { + private static final Map, List> MATERIAL_TO_EFFECT_MAP = + (new ImmutableMap.Builder, List>()) + .put(ModArmorMaterials.STALINIUM_ARMOR_MATERIAL, + List.of(new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 100, 0, false, false))) + .build(); + + public StaliniumChestplateLeggingsItem(Holder material, Type type, Properties properties) { + super(material, type, properties); + } + + @Override + public void inventoryTick(ItemStack stack, Level level, Entity entity, int slotId, boolean isSelected) { + if (entity instanceof Player player && !level.isClientSide() && hasChestAndLeggingsOn(player)) { + evaluateArmorEffects(player); + } + } + + private void evaluateArmorEffects(Player player) { + for (Map.Entry, List> entry : MATERIAL_TO_EFFECT_MAP.entrySet()) { + Holder mapArmorMaterial = entry.getKey(); + List mapEffect = entry.getValue(); + + if (hasPlayerCorrectArmorOn(mapArmorMaterial, player)) { + addEffectToPlayer(player, mapEffect); + } + } + } + + private void addEffectToPlayer(Player player, List mapEffect) { + AABB box = player.getBoundingBox().inflate(5.0); + List allies = player.level().getEntitiesOfClass( + Player.class, + box, + p -> p instanceof ServerPlayer && player.isAlliedTo(p) + ); + + for (MobEffectInstance effect : mapEffect) { + player.addEffect(new MobEffectInstance( + effect.getEffect(), + effect.getDuration(), + effect.getAmplifier(), + effect.isAmbient(), + effect.isVisible())); + for (Player ally : allies) { + ally.addEffect(new MobEffectInstance( + effect.getEffect(), + effect.getDuration(), + effect.getAmplifier(), + effect.isAmbient(), + effect.isVisible() + )); + } + } + } + + private boolean hasPlayerCorrectArmorOn(Holder mapArmorMaterial, Player player) { + ItemStack leggingsStack = player.getInventory().getArmor(1); + ItemStack chestplateStack = player.getInventory().getArmor(2); + + if (leggingsStack.isEmpty() || chestplateStack.isEmpty()) { + return false; + } + + ArmorItem leggingsItem = (ArmorItem) leggingsStack.getItem(); + ArmorItem chestplateItem = (ArmorItem) chestplateStack.getItem(); + + return leggingsItem.getMaterial() == mapArmorMaterial + && chestplateItem.getMaterial() == mapArmorMaterial; + } + + private boolean hasChestAndLeggingsOn(Player player) { + ItemStack leggings = player.getInventory().getArmor(1); + ItemStack chestplate = player.getInventory().getArmor(2); + + return !leggings.isEmpty() && !chestplate.isEmpty(); + } + + @Override + public boolean isDamageable(ItemStack stack) { + return false; + } + + @Override + public boolean isDamaged(ItemStack stack) { + return false; + } +} \ No newline at end of file