Added effect for Stalinium chestplate and leggings
This commit is contained in:
@@ -45,11 +45,11 @@ public class ModItems {
|
||||
new Item.Properties().durability(ArmorItem.Type.HELMET.getDurability(19))));
|
||||
|
||||
public static final DeferredItem<ArmorItem> 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<ArmorItem> 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<ArmorItem> STALINIUM_BOOTS = ITEMS.register("stalinium_boots",
|
||||
|
||||
+107
@@ -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<Holder<ArmorMaterial>, List<MobEffectInstance>> MATERIAL_TO_EFFECT_MAP =
|
||||
(new ImmutableMap.Builder<Holder<ArmorMaterial>, List<MobEffectInstance>>())
|
||||
.put(ModArmorMaterials.STALINIUM_ARMOR_MATERIAL,
|
||||
List.of(new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 100, 0, false, false)))
|
||||
.build();
|
||||
|
||||
public StaliniumChestplateLeggingsItem(Holder<ArmorMaterial> 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<Holder<ArmorMaterial>, List<MobEffectInstance>> entry : MATERIAL_TO_EFFECT_MAP.entrySet()) {
|
||||
Holder<ArmorMaterial> mapArmorMaterial = entry.getKey();
|
||||
List<MobEffectInstance> mapEffect = entry.getValue();
|
||||
|
||||
if (hasPlayerCorrectArmorOn(mapArmorMaterial, player)) {
|
||||
addEffectToPlayer(player, mapEffect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addEffectToPlayer(Player player, List<MobEffectInstance> mapEffect) {
|
||||
AABB box = player.getBoundingBox().inflate(5.0);
|
||||
List<Player> 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<ArmorMaterial> 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user