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

添加exp函数

This commit is contained in:
jhqwqmc
2025-11-24 18:22:52 +08:00
parent 4c9058528f
commit ebcc6fa724
7 changed files with 110 additions and 5 deletions

View File

@@ -35,10 +35,7 @@ import net.momirealms.craftengine.core.plugin.locale.TranslationManager;
import net.momirealms.craftengine.core.plugin.network.ConnectionState; import net.momirealms.craftengine.core.plugin.network.ConnectionState;
import net.momirealms.craftengine.core.plugin.network.EntityPacketHandler; import net.momirealms.craftengine.core.plugin.network.EntityPacketHandler;
import net.momirealms.craftengine.core.sound.SoundSource; import net.momirealms.craftengine.core.sound.SoundSource;
import net.momirealms.craftengine.core.util.Direction; import net.momirealms.craftengine.core.util.*;
import net.momirealms.craftengine.core.util.IntIdentityList;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.craftengine.core.world.*; import net.momirealms.craftengine.core.world.*;
import net.momirealms.craftengine.core.world.World; import net.momirealms.craftengine.core.world.World;
import net.momirealms.craftengine.core.world.chunk.ChunkStatus; import net.momirealms.craftengine.core.world.chunk.ChunkStatus;
@@ -1253,4 +1250,32 @@ public class BukkitServerPlayer extends Player {
platformPlayer().getPersistentDataContainer().remove(KeyUtils.toNamespacedKey(SELECTED_LOCALE_KEY)); platformPlayer().getPersistentDataContainer().remove(KeyUtils.toNamespacedKey(SELECTED_LOCALE_KEY));
} }
} }
@Override
public void giveExperiencePoints(int xpPoints) {
platformPlayer().giveExp(xpPoints);
}
@Override
public void giveExperienceLevels(int levels) {
platformPlayer().giveExpLevels(levels);
}
@Override
public int getXpNeededForNextLevel() {
return platformPlayer().getExperiencePointsNeededForNextLevel();
}
@Override
public void setExperiencePoints(int experiencePoints) {
float xpNeededForNextLevel = this.getXpNeededForNextLevel();
float maxProgressThreshold = (xpNeededForNextLevel - 1.0F) / xpNeededForNextLevel;
float experienceProgress = MiscUtils.clamp(experiencePoints / xpNeededForNextLevel, 0.0F, maxProgressThreshold);
platformPlayer().setExp(experienceProgress);
}
@Override
public void setExperienceLevels(int level) {
platformPlayer().setLevel(level);
}
} }

View File

@@ -492,6 +492,7 @@ warning.config.function.if_else.missing_rules: "<yellow>Issue found in file <arg
warning.config.function.update_block_property.missing_properties: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'properties' argument for 'update_block_property' function.</yellow>" warning.config.function.update_block_property.missing_properties: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'properties' argument for 'update_block_property' function.</yellow>"
warning.config.function.transform_block.missing_block: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'block' argument for 'transform_block' function.</yellow>" warning.config.function.transform_block.missing_block: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'block' argument for 'transform_block' function.</yellow>"
warning.config.function.cycle_block_property.missing_property: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'property' argument for 'cycle_block_property' function.</yellow>" warning.config.function.cycle_block_property.missing_property: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'property' argument for 'cycle_block_property' function.</yellow>"
warning.config.function.exp.missing_count: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'count' argument for 'exp' function.</yellow>"
warning.config.selector.missing_type: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'type' argument for selector.</yellow>" warning.config.selector.missing_type: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'type' argument for selector.</yellow>"
warning.config.selector.invalid_type: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is using an invalid selector type '<arg:2>'.</yellow>" warning.config.selector.invalid_type: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is using an invalid selector type '<arg:2>'.</yellow>"
warning.config.selector.invalid_target: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is using an invalid selector target '<arg:2>'.</yellow>" warning.config.selector.invalid_target: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is using an invalid selector target '<arg:2>'.</yellow>"

View File

@@ -388,6 +388,7 @@ warning.config.loot_table.function.apply_bonus.missing_enchantment: "<yellow>在
warning.config.loot_table.function.apply_bonus.missing_formula: "<yellow>在文件 <arg:0> 发现问题 - '<arg:1>' 的战利品表配置错误 'apply_bonus' 函数缺少必需的 'formula' 参数</yellow>" warning.config.loot_table.function.apply_bonus.missing_formula: "<yellow>在文件 <arg:0> 发现问题 - '<arg:1>' 的战利品表配置错误 'apply_bonus' 函数缺少必需的 'formula' 参数</yellow>"
warning.config.loot_table.function.drop_exp.missing_count: "<yellow>在文件 <arg:0> 发现问题 - '<arg:1>' 的战利品表配置错误 'drop_exp' 函数缺少必需的 'count' 参数</yellow>" warning.config.loot_table.function.drop_exp.missing_count: "<yellow>在文件 <arg:0> 发现问题 - '<arg:1>' 的战利品表配置错误 'drop_exp' 函数缺少必需的 'count' 参数</yellow>"
warning.config.loot_table.function.set_count.missing_count: "<yellow>在文件 <arg:0> 发现问题 - '<arg:1>' 的战利品表配置错误 'set_count' 函数缺少必需的 'count' 参数</yellow>" warning.config.loot_table.function.set_count.missing_count: "<yellow>在文件 <arg:0> 发现问题 - '<arg:1>' 的战利品表配置错误 'set_count' 函数缺少必需的 'count' 参数</yellow>"
warning.config.loot_table.function.apply_data.missing_data: "<yellow>在文件 <arg:0> 发现问题 - '<arg:1>' 的战利品表配置错误 'apply_data' 函数缺少必需的 'data' 参数</yellow>"
warning.config.loot_table.entry.missing_type: "<yellow>在文件 <arg:0> 发现问题 - '<arg:1>' 的战利品表配置错误 某个条目缺少必需的 'type' 参数</yellow>" warning.config.loot_table.entry.missing_type: "<yellow>在文件 <arg:0> 发现问题 - '<arg:1>' 的战利品表配置错误 某个条目缺少必需的 'type' 参数</yellow>"
warning.config.loot_table.entry.invalid_type: "<yellow>在文件 <arg:0> 发现问题 - '<arg:1>' 的战利品表配置错误 某个条目使用了无效的条目类型 '<arg:2>'</yellow>" warning.config.loot_table.entry.invalid_type: "<yellow>在文件 <arg:0> 发现问题 - '<arg:1>' 的战利品表配置错误 某个条目使用了无效的条目类型 '<arg:2>'</yellow>"
warning.config.loot_table.entry.exp.missing_count: "<yellow>在文件 <arg:0> 发现问题 - '<arg:1>' 的战利品表配置错误 'exp' 条目缺少必需的 'count' 参数</yellow>" warning.config.loot_table.entry.exp.missing_count: "<yellow>在文件 <arg:0> 发现问题 - '<arg:1>' 的战利品表配置错误 'exp' 条目缺少必需的 'count' 参数</yellow>"
@@ -490,6 +491,7 @@ warning.config.function.when.missing_source: "<yellow>在文件 <arg:0> 发现
warning.config.function.if_else.missing_rules: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 缺少 'if_else' 函数所需的 'rules' 参数</yellow>" warning.config.function.if_else.missing_rules: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 缺少 'if_else' 函数所需的 'rules' 参数</yellow>"
warning.config.function.update_block_property.missing_properties: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 缺少 'update_block_property' 函数所需的 'properties' 参数</yellow>" warning.config.function.update_block_property.missing_properties: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 缺少 'update_block_property' 函数所需的 'properties' 参数</yellow>"
warning.config.function.transform_block.missing_block: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 缺少 'transform_block' 函数所需的 'block' 参数</yellow>" warning.config.function.transform_block.missing_block: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 缺少 'transform_block' 函数所需的 'block' 参数</yellow>"
warning.config.function.exp.missing_count: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 缺少 'exp' 函数所需的 'count' 参数</yellow>"
warning.config.function.cycle_block_property.missing_property: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 缺少 'cycle_block_property' 函数所需的 'property' 参数</yellow>" warning.config.function.cycle_block_property.missing_property: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 缺少 'cycle_block_property' 函数所需的 'property' 参数</yellow>"
warning.config.selector.missing_type: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 缺少选择器必需的 'type' 参数</yellow>" warning.config.selector.missing_type: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 缺少选择器必需的 'type' 参数</yellow>"
warning.config.selector.invalid_type: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 使用了无效的选择器类型 '<arg:2>'</yellow>" warning.config.selector.invalid_type: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 使用了无效的选择器类型 '<arg:2>'</yellow>"
@@ -509,4 +511,4 @@ warning.config.resource_pack.invalid_overlay_format: "<yellow>在 config.yml 的
warning.config.equipment.duplicate: "<yellow>在文件 <arg:0> 发现问题 - 重复的装备配置 '<arg:1>'. 请检查其他文件中是否存在相同配置</yellow>" warning.config.equipment.duplicate: "<yellow>在文件 <arg:0> 发现问题 - 重复的装备配置 '<arg:1>'. 请检查其他文件中是否存在相同配置</yellow>"
warning.config.equipment.missing_type: "<yellow>在文件 <arg:0> 发现问题 - 装备 '<arg:1>' 缺少必需的 'type' 参数</yellow>" warning.config.equipment.missing_type: "<yellow>在文件 <arg:0> 发现问题 - 装备 '<arg:1>' 缺少必需的 'type' 参数</yellow>"
warning.config.equipment.invalid_type: "<yellow>在文件 <arg:0> 发现问题 - 装备 '<arg:1>' 使用了无效的 'type' 参数</yellow>" warning.config.equipment.invalid_type: "<yellow>在文件 <arg:0> 发现问题 - 装备 '<arg:1>' 使用了无效的 'type' 参数</yellow>"
warning.config.equipment.invalid_sacrificed_armor: "<yellow>在 config.yml 的 'equipment.sacrificed-vanilla-armor' 处发现问题 - 无效的原版盔甲类型 '<arg:0>'</yellow>" warning.config.equipment.invalid_sacrificed_armor: "<yellow>在 config.yml 的 'equipment.sacrificed-vanilla-armor' 处发现问题 - 无效的原版盔甲类型 '<arg:0>'</yellow>"

View File

@@ -184,6 +184,16 @@ public abstract class Player extends AbstractEntity implements NetWorkUser {
public abstract void setSelectedLocale(@Nullable Locale locale); public abstract void setSelectedLocale(@Nullable Locale locale);
public abstract void giveExperiencePoints(int xpPoints);
public abstract void giveExperienceLevels(int levels);
public abstract int getXpNeededForNextLevel();
public abstract void setExperiencePoints(int experiencePoints);
public abstract void setExperienceLevels(int level);
@Override @Override
public void remove() { public void remove() {
} }

View File

@@ -55,6 +55,7 @@ public class EventFunctions {
register(CommonFunctions.WHEN, new WhenFunction.FactoryImpl<>(EventConditions::fromMap, EventFunctions::fromMap)); register(CommonFunctions.WHEN, new WhenFunction.FactoryImpl<>(EventConditions::fromMap, EventFunctions::fromMap));
register(CommonFunctions.DAMAGE_ITEM, new DamageItemFunction.FactoryImpl<>(EventConditions::fromMap)); register(CommonFunctions.DAMAGE_ITEM, new DamageItemFunction.FactoryImpl<>(EventConditions::fromMap));
register(CommonFunctions.CYCLE_BLOCK_PROPERTY, new CycleBlockPropertyFunction.FactoryImpl<>(EventConditions::fromMap)); register(CommonFunctions.CYCLE_BLOCK_PROPERTY, new CycleBlockPropertyFunction.FactoryImpl<>(EventConditions::fromMap));
register(CommonFunctions.EXP, new ExpFunction.FactoryImpl<>(EventConditions::fromMap));
} }
public static void register(Key key, FunctionFactory<Context> factory) { public static void register(Key key, FunctionFactory<Context> factory) {

View File

@@ -46,4 +46,5 @@ public final class CommonFunctions {
public static final Key DUMMY = Key.of("craftengine:dummy"); public static final Key DUMMY = Key.of("craftengine:dummy");
public static final Key DAMAGE_ITEM = Key.of("craftengine:damage_item"); 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 CYCLE_BLOCK_PROPERTY = Key.of("craftengine:cycle_block_property");
public static final Key EXP = Key.of("craftengine:exp");
} }

View File

@@ -0,0 +1,65 @@
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 ExpFunction<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) {
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.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);
}
@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 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);
}
}
}