Visitar URL original
全套盔甲增益 · wwwaker/TutorialMod@ea7b720 · GitHub
Skip to content

Commit ea7b720

Browse files
committed
全套盔甲增益
1 parent 4c4a642 commit ea7b720

3 files changed

Lines changed: 89 additions & 9 deletions

File tree

‎src/main/java/com/wwwaker/tutorial/item/ModArmorMaterials.java‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,22 @@
33
import com.wwwaker.tutorial.TutorialMod;
44
import net.minecraft.item.ArmorItem;
55
import net.minecraft.item.ArmorMaterial;
6-
import net.minecraft.item.Items;
76
import net.minecraft.recipe.Ingredient;
87
import net.minecraft.sound.SoundEvent;
98
import net.minecraft.sound.SoundEvents;
10-
import net.minecraft.util.Lazy;
119
import net.minecraft.util.StringIdentifiable;
1210
import net.minecraft.util.Util;
1311

1412
import java.util.EnumMap;
1513
import java.util.function.Supplier;
1614

1715
public enum ModArmorMaterials implements StringIdentifiable, ArmorMaterial {
18-
ICE_ETHER("ice_ether", 13, Util.make(new EnumMap(ArmorItem.Type.class), map -> {
19-
map.put(ArmorItem.Type.BOOTS, 3);
20-
map.put(ArmorItem.Type.LEGGINGS, 6);
21-
map.put(ArmorItem.Type.CHESTPLATE, 8);
22-
map.put(ArmorItem.Type.HELMET, 3);
23-
}), 36, SoundEvents.ITEM_ARMOR_EQUIP_NETHERITE, 1.0F, 0.1F, () -> Ingredient.ofItems(ModItems.ICE_ETHER));
16+
ICE_ETHER("ice_ether", 11, Util.make(new EnumMap(ArmorItem.Type.class), map -> {
17+
map.put(ArmorItem.Type.BOOTS, 2);
18+
map.put(ArmorItem.Type.LEGGINGS, 5);
19+
map.put(ArmorItem.Type.CHESTPLATE, 6);
20+
map.put(ArmorItem.Type.HELMET, 2);
21+
}), 36, SoundEvents.ITEM_ARMOR_EQUIP_NETHERITE, 1.5F, 0, () -> Ingredient.ofItems(ModItems.ICE_ETHER));
2422

2523
private static final EnumMap<ArmorItem.Type, Integer> BASE_DURABILITY = Util.make(new EnumMap(ArmorItem.Type.class), map -> {
2624
map.put(ArmorItem.Type.BOOTS, 13);

‎src/main/java/com/wwwaker/tutorial/item/ModItems.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.wwwaker.tutorial.item;
22

33
import com.wwwaker.tutorial.TutorialMod;
4+
import com.wwwaker.tutorial.item.custom.ModArmorItem;
45
import com.wwwaker.tutorial.item.custom.PickaxeAxeItem;
56
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroupEntries;
67
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
@@ -31,7 +32,7 @@ public class ModItems {
3132

3233
public static final Item FIRE_ETHER_PICKAXE_AXE = registerItems("fire_ether_pickaxe_axe", new PickaxeAxeItem(ModToolMaterials.FIRE_ETHER, 3, -3.0F, new Item.Settings()));
3334

34-
public static final Item ICE_ETHER_HELMET = registerItems("ice_ether_helmet", new ArmorItem(ModArmorMaterials.ICE_ETHER, ArmorItem.Type.HELMET, new Item.Settings()));
35+
public static final Item ICE_ETHER_HELMET = registerItems("ice_ether_helmet", new ModArmorItem(ModArmorMaterials.ICE_ETHER, ArmorItem.Type.HELMET, new Item.Settings()));
3536
public static final Item ICE_ETHER_CHESTPLATE = registerItems("ice_ether_chestplate", new ArmorItem(ModArmorMaterials.ICE_ETHER, ArmorItem.Type.CHESTPLATE, new Item.Settings()));
3637
public static final Item ICE_ETHER_LEGGINGS = registerItems("ice_ether_leggings", new ArmorItem(ModArmorMaterials.ICE_ETHER, ArmorItem.Type.LEGGINGS, new Item.Settings()));
3738
public static final Item ICE_ETHER_BOOTS = registerItems("ice_ether_boots", new ArmorItem(ModArmorMaterials.ICE_ETHER, ArmorItem.Type.BOOTS, new Item.Settings()));
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)