9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-23 08:59:27 +00:00

允许传递 null 玩家

This commit is contained in:
jhqwqmc
2025-10-25 22:48:48 +08:00
parent e650cfaa02
commit 3f559012b4
5 changed files with 60 additions and 11 deletions

View File

@@ -14,12 +14,12 @@ import net.momirealms.craftengine.bukkit.util.KeyUtils;
import net.momirealms.craftengine.core.entity.EquipmentSlot;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.item.ItemWrapper;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.RandomUtils;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.sparrow.nbt.Tag;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
@@ -142,7 +142,7 @@ public class ComponentItemWrapper implements ItemWrapper<ItemStack> {
}
public void setNBTComponent(Object type, Object value) {
setComponentInternal(type, MRegistryOps.NBT, value);
setComponentInternal(type, MRegistryOps.NBT, value);
}
public void setSparrowNBTComponent(Object type, Tag value) {
@@ -209,7 +209,14 @@ public class ComponentItemWrapper implements ItemWrapper<ItemStack> {
}
@Override
public void hurtAndBreak(int amount, @NotNull Player player, @Nullable EquipmentSlot slot) {
public void hurtAndBreak(int amount, @Nullable Player player, @Nullable EquipmentSlot slot) {
if (player == null) {
if (this.hurt(amount)) {
this.shrink(1);
this.setJavaComponent(DataComponentTypes.DAMAGE, 0);
}
return;
}
FastNMS.INSTANCE.method$ItemStack$hurtAndBreak(
this.handle,
amount,
@@ -217,4 +224,22 @@ public class ComponentItemWrapper implements ItemWrapper<ItemStack> {
slot != null ? EquipmentSlotUtils.toNMSEquipmentSlot(slot) : null
);
}
private boolean hurt(int amount) {
if (!this.hasComponent(DataComponentTypes.MAX_DAMAGE) || this.hasComponent(DataComponentTypes.UNBREAKABLE) || !this.hasComponent(DataComponentTypes.DAMAGE)) return false;
if (amount > 0) {
int level = this.item.getEnchantmentLevel(Enchantment.UNBREAKING);
int ignoredDamage = 0;
for (int i = 0; level > 0 && i < amount; ++i) {
if (RandomUtils.generateRandomInt(0, level + 1) > 0) ++ignoredDamage;
}
amount -= ignoredDamage;
if (amount <= 0) return false;
}
Optional<Integer> optionalDamage = this.getJavaComponent(DataComponentTypes.DAMAGE);
int damage = optionalDamage.orElse(0) + amount;
this.setJavaComponent(DataComponentTypes.DAMAGE, damage);
Optional<Integer> optionalMaxDamage = this.getJavaComponent(DataComponentTypes.MAX_DAMAGE);
return damage >= optionalMaxDamage.orElseGet(() -> (int) this.item.getType().getMaxDurability());
}
}

View File

@@ -8,9 +8,10 @@ import net.momirealms.craftengine.bukkit.util.ItemStackUtils;
import net.momirealms.craftengine.core.entity.EquipmentSlot;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.item.ItemWrapper;
import net.momirealms.craftengine.core.util.RandomUtils;
import net.momirealms.sparrow.nbt.Tag;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class LegacyItemWrapper implements ItemWrapper<ItemStack> {
@@ -27,7 +28,7 @@ public class LegacyItemWrapper implements ItemWrapper<ItemStack> {
if (value instanceof Tag tag) {
finalNMSTag = MRegistryOps.SPARROW_NBT.convertTo(MRegistryOps.NBT, tag);
} else if (CoreReflections.clazz$Tag.isInstance(value)) {
finalNMSTag = value;
finalNMSTag = value;
} else {
finalNMSTag = MRegistryOps.JAVA.convertTo(MRegistryOps.NBT, value);
}
@@ -157,7 +158,14 @@ public class LegacyItemWrapper implements ItemWrapper<ItemStack> {
}
@Override
public void hurtAndBreak(int amount, @NotNull Player player, @Nullable EquipmentSlot slot) {
public void hurtAndBreak(int amount, @Nullable Player player, @Nullable EquipmentSlot slot) {
if (player == null) {
if (this.hurt(amount)) {
this.shrink(1);
this.setTag(0, "Damage");
}
return;
}
FastNMS.INSTANCE.method$ItemStack$hurtAndBreak(
this.nmsStack,
amount,
@@ -165,4 +173,21 @@ public class LegacyItemWrapper implements ItemWrapper<ItemStack> {
slot != null ? EquipmentSlotUtils.toNMSEquipmentSlot(slot) : null
);
}
private boolean hurt(int amount) {
if (ItemStackUtils.isEmpty(itemStack) || itemStack.getType().getMaxDurability() <= 0 || !hasTag("Unbreakable") || (boolean) getJavaTag("Unbreakable")) return false;
if (amount > 0) {
int level = this.itemStack.getEnchantmentLevel(Enchantment.UNBREAKING);
int ignoredDamage = 0;
for (int i = 0; level > 0 && i < amount; ++i) {
if (RandomUtils.generateRandomInt(0, level + 1) > 0) ++ignoredDamage;
}
amount -= ignoredDamage;
if (amount <= 0) return false;
}
int damage = this.hasTag("Damage") ? this.getJavaTag("Damage") : 0;
damage += amount;
this.setTag(damage, "Damage");
return damage >= this.itemStack.getType().getMaxDurability();
}
}

View File

@@ -504,7 +504,7 @@ public class AbstractItem<W extends ItemWrapper<I>, I> implements Item<I> {
}
@Override
public void hurtAndBreak(int amount, @NotNull Player player, @Nullable EquipmentSlot slot) {
public void hurtAndBreak(int amount, @Nullable Player player, @Nullable EquipmentSlot slot) {
this.item.hurtAndBreak(amount, player, slot);
}
}

View File

@@ -216,7 +216,7 @@ public interface Item<I> {
void shrink(int amount);
void hurtAndBreak(int amount, @NotNull Player player, @Nullable EquipmentSlot slot);
void hurtAndBreak(int amount, @Nullable Player player, @Nullable EquipmentSlot slot);
default Item<I> transmuteCopy(Key another) {
return transmuteCopy(another, this.count());

View File

@@ -2,7 +2,6 @@ package net.momirealms.craftengine.core.item;
import net.momirealms.craftengine.core.entity.EquipmentSlot;
import net.momirealms.craftengine.core.entity.player.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface ItemWrapper<I> {
@@ -19,5 +18,5 @@ public interface ItemWrapper<I> {
void shrink(int amount);
void hurtAndBreak(int amount, @NotNull Player player, @Nullable EquipmentSlot slot);
void hurtAndBreak(int amount, @Nullable Player player, @Nullable EquipmentSlot slot);
}