1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| public class UndyingEventHandler {
public static void register() {
ServerTickEvents.END_SERVER_TICK.register((MinecraftServer server) -> { for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) { if (hasAtLeastTwoPieces(player)) { if (!player.hasStatusEffect(ModStatusEffects.UNDYING)) { player.addStatusEffect(new StatusEffectInstance( ModStatusEffects.UNDYING, 200, 0, false, false, true )); } } else { if (player.hasStatusEffect(ModStatusEffects.UNDYING)) { player.removeStatusEffect(ModStatusEffects.UNDYING); } } } });
ServerLivingEntityEvents.ALLOW_DAMAGE.register((entity, source, amount) -> { if (!(entity instanceof PlayerEntity player)) return true;
StatusEffectInstance effect = player.getStatusEffect(ModStatusEffects.UNDYING); if (effect == null) return true;
if (player.getStackInHand(Hand.OFF_HAND).isOf(Items.TOTEM_OF_UNDYING)) { return true; }
if (amount >= player.getHealth()) { if (hasAtLeastTwoPieces(player)) { player.setHealth(10.0F); player.removeStatusEffect(ModStatusEffects.UNDYING); player.addStatusEffect(new StatusEffectInstance(ModStatusEffects.UNDYING, 200));
ItemStack toBreak = getHighestDurabilityArmor(player); if (!toBreak.isEmpty()) { String brokenName = toBreak.getName().getString(); toBreak.damage(toBreak.getMaxDamage(), player, p -> p.sendEquipmentBreakStatus(getSlotForItem(toBreak)));
player.sendMessage(Text.literal("§c你的 " + brokenName + " 已破碎! ! !"), false); }
player.getWorld().playSound(null, player.getBlockPos(), SoundEvents.ENTITY_ITEM_BREAK, SoundCategory.PLAYERS, 1.0F, 1.0F);
if (player.getWorld() instanceof ServerWorld serverWorld) { serverWorld.spawnParticles( ModParticleTypes.DARK_CRYSTAL_DIAMOND_ARMOR_BROKEN, player.getX(), player.getBodyY(0.5), player.getZ(), 120, 1.0, 0.5, 1.0, 0.0 ); }
return false; } } return true; }); } private static boolean hasAtLeastTwoPieces(PlayerEntity player) { int count = 0; if (player.getEquippedStack(EquipmentSlot.HEAD).isOf(ModItems.LAPIS_LAZULI_COPPER_HELMET)) count++; if (player.getEquippedStack(EquipmentSlot.CHEST).isOf(ModItems.LAPIS_LAZULI_COPPER_CHESTPLATE)) count++; if (player.getEquippedStack(EquipmentSlot.LEGS).isOf(ModItems.LAPIS_LAZULI_COPPER_LEGGINGS)) count++; if (player.getEquippedStack(EquipmentSlot.FEET).isOf(ModItems.LAPIS_LAZULI_COPPER_BOOTS)) count++; return count >= 2; }
private static ItemStack getHighestDurabilityArmor(PlayerEntity player) { ItemStack[] armors = { player.getEquippedStack(EquipmentSlot.HEAD), player.getEquippedStack(EquipmentSlot.CHEST), player.getEquippedStack(EquipmentSlot.LEGS), player.getEquippedStack(EquipmentSlot.FEET) }; ItemStack highest = ItemStack.EMPTY; int maxDurability = -1; for (ItemStack armor : armors) { if (!armor.isEmpty() && armor.getItem() instanceof ArmorItem) {
if (armor.isOf(ModItems.LAPIS_LAZULI_COPPER_HELMET) || armor.isOf(ModItems.LAPIS_LAZULI_COPPER_CHESTPLATE) || armor.isOf(ModItems.LAPIS_LAZULI_COPPER_LEGGINGS) || armor.isOf(ModItems.LAPIS_LAZULI_COPPER_BOOTS)) {
int remaining = armor.getMaxDamage() - armor.getDamage(); if (remaining > maxDurability) { highest = armor; maxDurability = remaining; } } } } return highest; }
private static EquipmentSlot getSlotForItem(ItemStack stack) { if (stack.getItem() instanceof ArmorItem armor) { return armor.getSlotType(); } return EquipmentSlot.MAINHAND; } }
|