9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-19 15:09:15 +00:00
This commit is contained in:
jhqwqmc
2025-11-24 20:58:56 +08:00
parent a2334adbc6
commit d4b7d0a332
4 changed files with 66 additions and 11 deletions

View File

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

View File

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

View File

@@ -14,12 +14,12 @@ import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
public class ExpFunction<CTX extends Context> extends AbstractConditionalFunction<CTX> {
public class SetExpFunction<CTX extends Context> extends AbstractConditionalFunction<CTX> {
private final PlayerSelector<CTX> selector;
private final NumberProvider count;
private final BiConsumer<Player, Integer> operation;
public ExpFunction(List<Condition<CTX>> predicates, PlayerSelector<CTX> selector, NumberProvider count, BiConsumer<Player, Integer> operation) {
public SetExpFunction(List<Condition<CTX>> predicates, PlayerSelector<CTX> selector, NumberProvider count, BiConsumer<Player, Integer> operation) {
super(predicates);
this.selector = selector;
this.count = count;
@@ -35,18 +35,16 @@ public class ExpFunction<CTX extends Context> extends AbstractConditionalFunctio
@Override
public Key type() {
return CommonFunctions.EXP;
return CommonFunctions.SET_EXP;
}
public static class FactoryImpl<CTX extends Context> extends AbstractFactory<CTX> {
private static final BiConsumer<Player, Integer> ADD_POINTS = Player::giveExperiencePoints;
private static final BiConsumer<Player, Integer> ADD_LEVELS = Player::giveExperienceLevels;
private static final BiConsumer<Player, Integer> SET_POINTS = (player, experience) -> {
if (experience < player.getXpNeededForNextLevel()) {
player.setExperiencePoints(experience);
}
};
private static final BiConsumer<Player, Integer> SET_LEVELS = Player::setExperienceLevels;
public FactoryImpl(java.util.function.Function<Map<String, Object>, Condition<CTX>> factory) {
super(factory);
@@ -56,10 +54,8 @@ public class ExpFunction<CTX extends Context> extends AbstractConditionalFunctio
public Function<CTX> create(Map<String, Object> arguments) {
PlayerSelector<CTX> 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<Player, Integer> 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);
}
}
}

View File

@@ -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<CTX extends Context> extends AbstractConditionalFunction<CTX> {
private final PlayerSelector<CTX> selector;
private final NumberProvider count;
private final BiConsumer<Player, Integer> operation;
public SetLevelFunction(List<Condition<CTX>> predicates, PlayerSelector<CTX> selector, NumberProvider count, BiConsumer<Player, Integer> 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<CTX extends Context> extends AbstractFactory<CTX> {
private static final BiConsumer<Player, Integer> ADD_LEVELS = Player::giveExperienceLevels;
private static final BiConsumer<Player, Integer> SET_LEVELS = Player::setExperienceLevels;
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());
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);
}
}
}