9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-24 09:29:33 +00:00

Merge pull request #393 from jhqwqmc/func

feat(func): 添加伤害玩家函数
This commit is contained in:
XiaoMoMi
2025-09-30 17:23:33 +08:00
committed by GitHub
5 changed files with 67 additions and 1 deletions

View File

@@ -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());
}
}

View File

@@ -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);
}

View File

@@ -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<PlayerOptionalContext> factory) {

View File

@@ -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");
}

View File

@@ -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<CTX extends Context> extends AbstractConditionalFunction<CTX> {
private final PlayerSelector<CTX> selector;
private final Key damageType;
private final NumberProvider amount;
public DamageFunction(PlayerSelector<CTX> selector, Key damageType, NumberProvider amount, List<Condition<CTX>> 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<CTX extends Context> extends AbstractFactory<CTX> {
public FactoryImpl(java.util.function.Function<Map<String, Object>, Condition<CTX>> factory) {
super(factory);
}
@Override
public Function<CTX> create(Map<String, Object> arguments) {
PlayerSelector<CTX> 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));
}
}
}