9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2026-01-03 22:26:16 +00:00

Merge pull request #217 from jhqwqmc/dev

feat(item): 添加物品invulnerable设置
This commit is contained in:
XiaoMoMi
2025-06-12 15:31:03 +08:00
committed by GitHub
7 changed files with 188 additions and 5 deletions

View File

@@ -28,12 +28,14 @@ import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
@@ -399,4 +401,17 @@ public class ItemEventListener implements Listener {
}
return false;
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onEntityDamage(EntityDamageEvent event) {
if (event.getEntityType() == EntityType.ITEM && event.getEntity() instanceof org.bukkit.entity.Item item) {
Optional.ofNullable(this.plugin.itemManager().wrap(item.getItemStack()))
.flatMap(Item::getCustomItem)
.ifPresent(it -> {
if (it.settings().invulnerable().contains(DamageCauseUtils.fromBukkit(event.getCause()))) {
event.setCancelled(true);
}
});
}
}
}

View File

@@ -124,7 +124,6 @@ public class PacketConsumers {
ADD_ENTITY_HANDLERS[MEntityTypes.BLOCK_DISPLAY$registryId] = simpleAddEntityHandler(BlockDisplayPacketHandler.INSTANCE);
ADD_ENTITY_HANDLERS[MEntityTypes.TEXT_DISPLAY$registryId] = simpleAddEntityHandler(TextDisplayPacketHandler.INSTANCE);
ADD_ENTITY_HANDLERS[MEntityTypes.ARMOR_STAND$registryId] = simpleAddEntityHandler(ArmorStandPacketHandler.INSTANCE);
ADD_ENTITY_HANDLERS[MEntityTypes.ITEM_DISPLAY$registryId] = simpleAddEntityHandler(ItemDisplayPacketHandler.INSTANCE);
ADD_ENTITY_HANDLERS[MEntityTypes.ITEM$registryId] = simpleAddEntityHandler(CommonItemPacketHandler.INSTANCE);
ADD_ENTITY_HANDLERS[MEntityTypes.ITEM_FRAME$registryId] = simpleAddEntityHandler(CommonItemPacketHandler.INSTANCE);
ADD_ENTITY_HANDLERS[MEntityTypes.GLOW_ITEM_FRAME$registryId] = simpleAddEntityHandler(CommonItemPacketHandler.INSTANCE);
@@ -152,6 +151,8 @@ public class PacketConsumers {
if (Config.hideBaseEntity() && !furniture.hasExternalModel()) {
event.setCancelled(true);
}
} else {
user.entityPacketHandlers().put(id, ItemDisplayPacketHandler.INSTANCE);
}
};
ADD_ENTITY_HANDLERS[MEntityTypes.INTERACTION$registryId] = (user, event) -> {

View File

@@ -0,0 +1,89 @@
package net.momirealms.craftengine.bukkit.util;
import net.momirealms.craftengine.core.util.DamageCause;
import org.bukkit.event.entity.EntityDamageEvent;
public class DamageCauseUtils {
private DamageCauseUtils() {}
@SuppressWarnings("deprecation")
public static EntityDamageEvent.DamageCause toBukkit(DamageCause cause) {
return switch (cause) {
case BLOCK_EXPLOSION -> EntityDamageEvent.DamageCause.BLOCK_EXPLOSION;
case CAMPFIRE -> EntityDamageEvent.DamageCause.CAMPFIRE;
case CONTACT -> EntityDamageEvent.DamageCause.CONTACT;
case CRAMMING -> EntityDamageEvent.DamageCause.CRAMMING;
case CUSTOM -> EntityDamageEvent.DamageCause.CUSTOM;
case DROWNING -> EntityDamageEvent.DamageCause.DROWNING;
case DRYOUT -> EntityDamageEvent.DamageCause.DRYOUT;
case ENTITY_ATTACK -> EntityDamageEvent.DamageCause.ENTITY_ATTACK;
case ENTITY_EXPLOSION -> EntityDamageEvent.DamageCause.ENTITY_EXPLOSION;
case ENTITY_SWEEP_ATTACK -> EntityDamageEvent.DamageCause.ENTITY_SWEEP_ATTACK;
case FALL -> EntityDamageEvent.DamageCause.FALL;
case FALLING_BLOCK -> EntityDamageEvent.DamageCause.FALLING_BLOCK;
case FIRE -> EntityDamageEvent.DamageCause.FIRE;
case FIRE_TICK -> EntityDamageEvent.DamageCause.FIRE_TICK;
case FLY_INTO_WALL -> EntityDamageEvent.DamageCause.FLY_INTO_WALL;
case FREEZE -> EntityDamageEvent.DamageCause.FREEZE;
case HOT_FLOOR -> EntityDamageEvent.DamageCause.HOT_FLOOR;
case KILL -> EntityDamageEvent.DamageCause.KILL;
case LAVA -> EntityDamageEvent.DamageCause.LAVA;
case LIGHTNING -> EntityDamageEvent.DamageCause.LIGHTNING;
case MAGIC -> EntityDamageEvent.DamageCause.MAGIC;
case MELTING -> EntityDamageEvent.DamageCause.MELTING;
case POISON -> EntityDamageEvent.DamageCause.POISON;
case PROJECTILE -> EntityDamageEvent.DamageCause.PROJECTILE;
case SONIC_BOOM -> EntityDamageEvent.DamageCause.SONIC_BOOM;
case STARVATION -> EntityDamageEvent.DamageCause.STARVATION;
case SUFFOCATION -> EntityDamageEvent.DamageCause.SUFFOCATION;
case SUICIDE -> EntityDamageEvent.DamageCause.SUICIDE;
case THORNS -> EntityDamageEvent.DamageCause.THORNS;
case VOID -> EntityDamageEvent.DamageCause.VOID;
case WITHER -> EntityDamageEvent.DamageCause.WITHER;
case WORLD_BORDER -> EntityDamageEvent.DamageCause.WORLD_BORDER;
case DRAGON_BREATH -> EntityDamageEvent.DamageCause.DRAGON_BREATH;
default -> throw new IllegalArgumentException("Unexpected value: " + cause);
};
}
@SuppressWarnings("deprecation")
public static DamageCause fromBukkit(EntityDamageEvent.DamageCause cause) {
return switch (cause) {
case BLOCK_EXPLOSION -> DamageCause.BLOCK_EXPLOSION;
case CAMPFIRE -> DamageCause.CAMPFIRE;
case CONTACT -> DamageCause.CONTACT;
case CRAMMING -> DamageCause.CRAMMING;
case CUSTOM -> DamageCause.CUSTOM;
case DROWNING -> DamageCause.DROWNING;
case DRYOUT -> DamageCause.DRYOUT;
case ENTITY_ATTACK -> DamageCause.ENTITY_ATTACK;
case ENTITY_EXPLOSION -> DamageCause.ENTITY_EXPLOSION;
case ENTITY_SWEEP_ATTACK -> DamageCause.ENTITY_SWEEP_ATTACK;
case FALL -> DamageCause.FALL;
case FALLING_BLOCK -> DamageCause.FALLING_BLOCK;
case FIRE -> DamageCause.FIRE;
case FIRE_TICK -> DamageCause.FIRE_TICK;
case FLY_INTO_WALL -> DamageCause.FLY_INTO_WALL;
case FREEZE -> DamageCause.FREEZE;
case HOT_FLOOR -> DamageCause.HOT_FLOOR;
case KILL -> DamageCause.KILL;
case LAVA -> DamageCause.LAVA;
case LIGHTNING -> DamageCause.LIGHTNING;
case MAGIC -> DamageCause.MAGIC;
case MELTING -> DamageCause.MELTING;
case POISON -> DamageCause.POISON;
case PROJECTILE -> DamageCause.PROJECTILE;
case SONIC_BOOM -> DamageCause.SONIC_BOOM;
case STARVATION -> DamageCause.STARVATION;
case SUFFOCATION -> DamageCause.SUFFOCATION;
case SUICIDE -> DamageCause.SUICIDE;
case THORNS -> DamageCause.THORNS;
case VOID -> DamageCause.VOID;
case WITHER -> DamageCause.WITHER;
case WORLD_BORDER -> DamageCause.WORLD_BORDER;
case DRAGON_BREATH -> DamageCause.DRAGON_BREATH;
default -> throw new IllegalArgumentException("Unexpected value: " + cause);
};
}
}

View File

@@ -153,6 +153,7 @@ warning.config.furniture.hitbox.invalid_type: "<yellow>Issue found in file <arg:
warning.config.furniture.hitbox.custom.invalid_entity: "<yellow>Issue found in file <arg:0> - The furniture '<arg:1>' is using a custom hitbox with invalid entity type '<arg:2>'.</yellow>"
warning.config.item.duplicate: "<yellow>Issue found in file <arg:0> - Duplicated item '<arg:1>'. Please check if there is the same configuration in other files.</yellow>"
warning.config.item.settings.unknown: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is using an unknown setting type '<arg:2>'.</yellow>"
warning.config.item.settings.invulnerable-unknown: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is using an unknown damage cause type '<arg:2>'.</yellow>"
warning.config.item.missing_material: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the required 'material' argument.</yellow>"
warning.config.item.invalid_material: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is using an invalid material type '<arg:2>'.</yellow>"
warning.config.item.invalid_custom_model_data: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is using a negative custom model data '<arg:2>' which is invalid.</yellow>"

View File

@@ -153,6 +153,7 @@ warning.config.furniture.hitbox.invalid_type: "<yellow>在文件 <arg:0> 发现
warning.config.furniture.hitbox.custom.invalid_entity: "<yellow>在文件 <arg:0> 发现问题 - 家具 '<arg:1>' 的自定义碰撞箱使用了无效的实体类型 '<arg:2>'</yellow>"
warning.config.item.duplicate: "<yellow>在文件 <arg:0> 发现问题 - 重复的物品 '<arg:1>' 请检查其他文件中是否存在相同配置</yellow>"
warning.config.item.settings.unknown: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 使用了未知的设置类型 '<arg:2>'</yellow>"
warning.config.item.settings.invulnerable-unknown: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 物品使用了未知的受伤害类型 '<arg:2>'</yellow>"
warning.config.item.missing_material: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 缺少必需的 'material' 参数</yellow>"
warning.config.item.invalid_material: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 使用了无效的材料类型 '<arg:2>'</yellow>"
warning.config.item.invalid_custom_model_data: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 使用了无效的负数模型值 '<arg:2>'.</yellow>"

View File

@@ -12,10 +12,7 @@ import net.momirealms.craftengine.core.item.setting.Helmet;
import net.momirealms.craftengine.core.pack.misc.EquipmentGeneration;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.sound.SoundData;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.craftengine.core.util.*;
import org.jetbrains.annotations.Nullable;
import org.joml.Quaternionf;
import org.joml.Vector3f;
@@ -38,6 +35,7 @@ public class ItemSettings {
FoodData foodData = null;
Key consumeReplacement = null;
Key craftRemainder = null;
List<DamageCause> invulnerable = List.of();
private ItemSettings() {}
@@ -76,6 +74,7 @@ public class ItemSettings {
newSettings.foodData = settings.foodData;
newSettings.consumeReplacement = settings.consumeReplacement;
newSettings.craftRemainder = settings.craftRemainder;
newSettings.invulnerable = settings.invulnerable;
return newSettings;
}
@@ -148,6 +147,10 @@ public class ItemSettings {
return equipment;
}
public List<DamageCause> invulnerable() {
return invulnerable;
}
public ItemSettings repairItems(List<AnvilRepairItem> items) {
this.anvilRepairItems = items;
return this;
@@ -213,6 +216,11 @@ public class ItemSettings {
return this;
}
public ItemSettings invulnerable(List<DamageCause> invulnerable) {
this.invulnerable = invulnerable;
return this;
}
@FunctionalInterface
public interface Modifier {
@@ -312,6 +320,16 @@ public class ItemSettings {
);
return settings -> settings.foodData(data);
}));
registerFactory("invulnerable", (value -> {
List<DamageCause> list = MiscUtils.getAsStringList(value).stream().map(it -> {
try {
return DamageCause.byName(it);
} catch (IllegalArgumentException e) {
throw new LocalizedResourceConfigException("warning.config.item.settings.invulnerable-unknown", it);
}
}).toList();
return settings -> settings.invulnerable(list);
}));
}
private static void registerFactory(String id, ItemSettings.Modifier.Factory factory) {

View File

@@ -0,0 +1,58 @@
package net.momirealms.craftengine.core.util;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
public enum DamageCause {
BLOCK_EXPLOSION,
CAMPFIRE,
CONTACT,
CRAMMING,
CUSTOM,
DROWNING,
DRYOUT,
ENTITY_ATTACK,
ENTITY_EXPLOSION,
ENTITY_SWEEP_ATTACK,
FALL,
FALLING_BLOCK,
FIRE,
FIRE_TICK,
FLY_INTO_WALL,
FREEZE,
HOT_FLOOR,
KILL,
LAVA,
LIGHTNING,
MAGIC,
MELTING,
POISON,
PROJECTILE,
SONIC_BOOM,
STARVATION,
SUFFOCATION,
SUICIDE,
THORNS,
VOID,
WITHER,
WORLD_BORDER,
@Deprecated
@SuppressWarnings("all")
DRAGON_BREATH;
public static final Map<String, DamageCause> BY_NAME = new HashMap<>();
static {
for (DamageCause cause : values()) {
BY_NAME.put(cause.name().toLowerCase(Locale.ROOT), cause);
BY_NAME.put(cause.name(), cause);
}
}
public static DamageCause byName(String name) {
return Optional.ofNullable(BY_NAME.get(name)).orElseThrow(() -> new IllegalArgumentException("Unknown damage cause: " + name));
}
}