|
| 1 | +package com.wwwaker.tutorial.item.custom; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableMap; |
| 4 | +import com.wwwaker.tutorial.item.ModArmorMaterials; |
| 5 | +import net.minecraft.entity.Entity; |
| 6 | +import net.minecraft.entity.effect.StatusEffect; |
| 7 | +import net.minecraft.entity.effect.StatusEffectInstance; |
| 8 | +import net.minecraft.entity.effect.StatusEffects; |
| 9 | +import net.minecraft.entity.player.PlayerEntity; |
| 10 | +import net.minecraft.item.ArmorItem; |
| 11 | +import net.minecraft.item.ArmorMaterial; |
| 12 | +import net.minecraft.item.ItemStack; |
| 13 | +import net.minecraft.world.World; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +public class ModArmorItem extends ArmorItem { |
| 20 | + private static final Map<ArmorMaterial, List<StatusEffectInstance>> MAP = |
| 21 | + (new ImmutableMap.Builder<ArmorMaterial, List<StatusEffectInstance>>()) |
| 22 | + .put(ModArmorMaterials.ICE_ETHER, |
| 23 | + Arrays.asList( |
| 24 | + new StatusEffectInstance(StatusEffects.SPEED, 1, 1,false,false,true), |
| 25 | + new StatusEffectInstance(StatusEffects.JUMP_BOOST, 1, 0,false,false,true) |
| 26 | + )).build(); |
| 27 | + |
| 28 | + public ModArmorItem(ArmorMaterial material, Type type, Settings settings) { |
| 29 | + super(material, type, settings); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) { |
| 34 | + if(!world.isClient){ |
| 35 | + if(entity instanceof PlayerEntity player && hasFullSuitableArmor(player)){ |
| 36 | + evaluateArmorEffect(player); |
| 37 | + } |
| 38 | + } |
| 39 | + super.inventoryTick(stack, world, entity, slot, selected); |
| 40 | + } |
| 41 | + |
| 42 | + private void evaluateArmorEffect(PlayerEntity player) { |
| 43 | + for(Map.Entry<ArmorMaterial, List<StatusEffectInstance>> entry : MAP.entrySet()) { |
| 44 | + ArmorMaterial material = entry.getKey(); |
| 45 | + List<StatusEffectInstance> effects = entry.getValue(); |
| 46 | + if(hasCorrectMaterialArmorOn(material, player)) { |
| 47 | + for(StatusEffectInstance effect : effects) { |
| 48 | + StatusEffect effects1 = effect.getEffectType(); |
| 49 | + if(!player.hasStatusEffect(effects1)){ |
| 50 | + player.addStatusEffect(effect); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private boolean hasCorrectMaterialArmorOn(ArmorMaterial material, PlayerEntity player) { |
| 58 | + for(ItemStack armorStack : player.getInventory().armor){ |
| 59 | + if(!(armorStack.getItem() instanceof ArmorItem)) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + } |
| 63 | + ArmorItem helmet = (ArmorItem) player.getInventory().getArmorStack(3).getItem(); |
| 64 | + ArmorItem chestplate = (ArmorItem) player.getInventory().getArmorStack(2).getItem(); |
| 65 | + ArmorItem leggings = (ArmorItem) player.getInventory().getArmorStack(1).getItem(); |
| 66 | + ArmorItem boots = (ArmorItem) player.getInventory().getArmorStack(0).getItem(); |
| 67 | + |
| 68 | + return helmet.getMaterial() == material |
| 69 | + && chestplate.getMaterial() == material |
| 70 | + && leggings.getMaterial() == material |
| 71 | + && boots.getMaterial() == material; |
| 72 | + } |
| 73 | + |
| 74 | + private boolean hasFullSuitableArmor(PlayerEntity player) { |
| 75 | + ItemStack helmet = player.getInventory().getArmorStack(3); |
| 76 | + ItemStack chestplate = player.getInventory().getArmorStack(2); |
| 77 | + ItemStack leggings = player.getInventory().getArmorStack(1); |
| 78 | + ItemStack boots = player.getInventory().getArmorStack(0); |
| 79 | + return !helmet.isEmpty() && !chestplate.isEmpty() && !leggings.isEmpty() && !boots.isEmpty(); |
| 80 | + } |
| 81 | +} |
0 commit comments