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 15a23bcd1..1b94eb8c6 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 @@ -55,7 +55,8 @@ public class EventFunctions { register(CommonFunctions.WHEN, new WhenFunction.FactoryImpl<>(EventConditions::fromMap, EventFunctions::fromMap)); register(CommonFunctions.DAMAGE_ITEM, new DamageItemFunction.FactoryImpl<>(EventConditions::fromMap)); register(CommonFunctions.CYCLE_BLOCK_PROPERTY, new CycleBlockPropertyFunction.FactoryImpl<>(EventConditions::fromMap)); - register(CommonFunctions.EXP, new ExpFunction.FactoryImpl<>(EventConditions::fromMap)); + register(CommonFunctions.SET_EXP, new SetExpFunction.FactoryImpl<>(EventConditions::fromMap)); + register(CommonFunctions.SET_LEVEL, new SetLevelFunction.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 a8e0ee201..a49c1f6e9 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 @@ -46,5 +46,6 @@ public final class CommonFunctions { public static final Key DUMMY = Key.of("craftengine:dummy"); public static final Key DAMAGE_ITEM = Key.of("craftengine:damage_item"); public static final Key CYCLE_BLOCK_PROPERTY = Key.of("craftengine:cycle_block_property"); - public static final Key EXP = Key.of("craftengine:exp"); + public static final Key SET_EXP = Key.of("craftengine:set_exp"); + public static final Key SET_LEVEL = Key.of("craftengine:set_level"); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/ExpFunction.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/SetExpFunction.java similarity index 70% rename from core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/ExpFunction.java rename to core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/SetExpFunction.java index 663d4618f..b812016d2 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/ExpFunction.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/SetExpFunction.java @@ -14,12 +14,12 @@ import java.util.List; import java.util.Map; import java.util.function.BiConsumer; -public class ExpFunction extends AbstractConditionalFunction { +public class SetExpFunction extends AbstractConditionalFunction { private final PlayerSelector selector; private final NumberProvider count; private final BiConsumer operation; - public ExpFunction(List> predicates, PlayerSelector selector, NumberProvider count, BiConsumer operation) { + public SetExpFunction(List> predicates, PlayerSelector selector, NumberProvider count, BiConsumer operation) { super(predicates); this.selector = selector; this.count = count; @@ -35,18 +35,16 @@ public class ExpFunction extends AbstractConditionalFunctio @Override public Key type() { - return CommonFunctions.EXP; + return CommonFunctions.SET_EXP; } public static class FactoryImpl extends AbstractFactory { private static final BiConsumer ADD_POINTS = Player::giveExperiencePoints; - private static final BiConsumer ADD_LEVELS = Player::giveExperienceLevels; private static final BiConsumer SET_POINTS = (player, experience) -> { if (experience < player.getXpNeededForNextLevel()) { player.setExperiencePoints(experience); } }; - private static final BiConsumer SET_LEVELS = Player::setExperienceLevels; public FactoryImpl(java.util.function.Function, Condition> factory) { super(factory); @@ -56,10 +54,8 @@ public class ExpFunction extends AbstractConditionalFunctio public Function create(Map arguments) { PlayerSelector selector = PlayerSelectors.fromObject(arguments.getOrDefault("target", "self"), conditionFactory()); Object value = ResourceConfigUtils.requireNonNullOrThrow(arguments.get("count"), "warning.config.function.exp.missing_count"); - boolean set = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("set", false), "set"); - boolean level = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("level", false), "level"); - BiConsumer operation = level ? (set ? SET_LEVELS : ADD_LEVELS) : (set ? SET_POINTS : ADD_POINTS); - return new ExpFunction<>(getPredicates(arguments), selector, NumberProviders.fromObject(value), operation); + boolean add = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("add", false), "add"); + return new SetExpFunction<>(getPredicates(arguments), selector, NumberProviders.fromObject(value), add ? ADD_POINTS : SET_POINTS); } } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/SetLevelFunction.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/SetLevelFunction.java new file mode 100644 index 000000000..c553bc283 --- /dev/null +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/SetLevelFunction.java @@ -0,0 +1,57 @@ +package net.momirealms.craftengine.core.plugin.context.function; + +import net.momirealms.craftengine.core.entity.player.Player; +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; +import java.util.function.BiConsumer; + +public class SetLevelFunction extends AbstractConditionalFunction { + private final PlayerSelector selector; + private final NumberProvider count; + private final BiConsumer operation; + + public SetLevelFunction(List> predicates, PlayerSelector selector, NumberProvider count, BiConsumer operation) { + super(predicates); + this.selector = selector; + this.count = count; + this.operation = operation; + } + + @Override + protected void runInternal(CTX ctx) { + for (Player player : this.selector.get(ctx)) { + this.operation.accept(player, this.count.getInt(ctx)); + } + } + + @Override + public Key type() { + return CommonFunctions.SET_LEVEL; + } + + public static class FactoryImpl extends AbstractFactory { + private static final BiConsumer ADD_LEVELS = Player::giveExperienceLevels; + private static final BiConsumer SET_LEVELS = Player::setExperienceLevels; + + 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()); + Object value = ResourceConfigUtils.requireNonNullOrThrow(arguments.get("count"), "warning.config.function.exp.missing_count"); + boolean add = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("add", false), "add"); + return new SetLevelFunction<>(getPredicates(arguments), selector, NumberProviders.fromObject(value), add ? ADD_LEVELS : SET_LEVELS); + } + } +}