From 2772c77247da7022539053e380beac8f2e1f6328 Mon Sep 17 00:00:00 2001 From: jhqwqmc Date: Tue, 30 Sep 2025 12:20:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(func):=20=E6=B7=BB=E5=8A=A0=E4=BC=A4?= =?UTF-8?q?=E5=AE=B3=E7=8E=A9=E5=AE=B6=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/user/BukkitServerPlayer.java | 13 ++++- .../core/entity/player/Player.java | 2 + .../plugin/context/event/EventFunctions.java | 1 + .../context/function/CommonFunctions.java | 1 + .../context/function/DamageFunction.java | 51 +++++++++++++++++++ 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/DamageFunction.java diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/user/BukkitServerPlayer.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/user/BukkitServerPlayer.java index 36ed88089..2b578908c 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/user/BukkitServerPlayer.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/user/BukkitServerPlayer.java @@ -5,6 +5,8 @@ import com.google.common.collect.Lists; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; +import io.papermc.paper.registry.RegistryAccess; +import io.papermc.paper.registry.RegistryKey; import net.kyori.adventure.text.Component; import net.momirealms.craftengine.bukkit.api.CraftEngineBlocks; import net.momirealms.craftengine.bukkit.block.entity.BlockEntityHolder; @@ -45,6 +47,8 @@ import org.bukkit.*; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeInstance; import org.bukkit.block.Block; +import org.bukkit.damage.DamageSource; +import org.bukkit.damage.DamageType; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.inventory.EquipmentSlot; @@ -506,7 +510,7 @@ public class BukkitServerPlayer extends Player { if (this.gameTicks % 20 == 0) { this.updateGUI(); } - if (this.isDestroyingBlock) { + if (this.isDestroyingBlock) { this.tickBlockDestroy(); } if (Config.predictBreaking() && !this.isDestroyingCustomBlock) { @@ -1133,4 +1137,11 @@ public class BukkitServerPlayer extends Player { Location location = new Location((org.bukkit.World) worldPosition.world().platformWorld(), worldPosition.x(), worldPosition.y(), worldPosition.z(), worldPosition.yRot(), worldPosition.xRot()); this.platformPlayer().teleportAsync(location, PlayerTeleportEvent.TeleportCause.PLUGIN); } + + @Override + public void damage(double amount, Key damageType) { + @SuppressWarnings("deprecation") + DamageType type = Registry.DAMAGE_TYPE.get(KeyUtils.toNamespacedKey(damageType)); + this.platformPlayer().damage(amount, DamageSource.builder(type != null ? type : DamageType.GENERIC).build()); + } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/entity/player/Player.java b/core/src/main/java/net/momirealms/craftengine/core/entity/player/Player.java index 6df8b25c7..f0da08202 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/entity/player/Player.java +++ b/core/src/main/java/net/momirealms/craftengine/core/entity/player/Player.java @@ -169,4 +169,6 @@ public abstract class Player extends AbstractEntity implements NetWorkUser { public abstract CooldownData cooldown(); public abstract void teleport(WorldPosition worldPosition); + + public abstract void damage(double amount, Key damageType); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventFunctions.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventFunctions.java index 8c36f5af6..e23bee3d9 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventFunctions.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventFunctions.java @@ -45,6 +45,7 @@ public class EventFunctions { register(CommonFunctions.TELEPORT, new TeleportFunction.FactoryImpl<>(EventConditions::fromMap)); register(CommonFunctions.SET_VARIABLE, new SetVariableFunction.FactoryImpl<>(EventConditions::fromMap)); register(CommonFunctions.TOAST, new ToastFunction.FactoryImpl<>(EventConditions::fromMap)); + register(CommonFunctions.DAMAGE, new DamageFunction.FactoryImpl<>(EventConditions::fromMap)); } public static void register(Key key, FunctionFactory factory) { diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/CommonFunctions.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/CommonFunctions.java index 143b02f1d..56c9ab931 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/CommonFunctions.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/CommonFunctions.java @@ -34,4 +34,5 @@ public final class CommonFunctions { public static final Key TELEPORT = Key.of("craftengine:teleport"); public static final Key TOAST = Key.of("craftengine:toast"); public static final Key SET_VARIABLE = Key.of("craftengine:set_variable"); + public static final Key DAMAGE = Key.of("craftengine:damage"); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/DamageFunction.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/DamageFunction.java new file mode 100644 index 000000000..6ebb22d35 --- /dev/null +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/DamageFunction.java @@ -0,0 +1,51 @@ +package net.momirealms.craftengine.core.plugin.context.function; + +import net.momirealms.craftengine.core.plugin.context.Condition; +import net.momirealms.craftengine.core.plugin.context.Context; +import net.momirealms.craftengine.core.plugin.context.number.NumberProvider; +import net.momirealms.craftengine.core.plugin.context.number.NumberProviders; +import net.momirealms.craftengine.core.plugin.context.selector.PlayerSelector; +import net.momirealms.craftengine.core.plugin.context.selector.PlayerSelectors; +import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; + +import java.util.List; +import java.util.Map; + +public class DamageFunction extends AbstractConditionalFunction { + private final PlayerSelector selector; + private final Key damageType; + private final NumberProvider amount; + + public DamageFunction(PlayerSelector selector, Key damageType, NumberProvider amount, List> predicates) { + super(predicates); + this.selector = selector; + this.damageType = damageType; + this.amount = amount; + } + + @Override + protected void runInternal(CTX ctx) { + selector.get(ctx).forEach(p -> p.damage(amount.getDouble(ctx), damageType)); + } + + @Override + public Key type() { + return CommonFunctions.DAMAGE; + } + + public static class FactoryImpl extends AbstractFactory { + + public FactoryImpl(java.util.function.Function, Condition> factory) { + super(factory); + } + + @Override + public Function create(Map arguments) { + PlayerSelector selector = PlayerSelectors.fromObject(arguments.getOrDefault("target", "self"), conditionFactory()); + Key damageType = Key.of(ResourceConfigUtils.getAsString(arguments.getOrDefault("damage-type", "generic"))); + NumberProvider amount = NumberProviders.fromObject(arguments.getOrDefault("amount", 1f)); + return new DamageFunction<>(selector, damageType, amount, getPredicates(arguments)); + } + } +}