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

添加apply_data战利品函数

This commit is contained in:
XiaoMoMi
2025-11-08 17:44:39 +08:00
parent a2e5443ecc
commit 4fa34bf366
4 changed files with 65 additions and 0 deletions

View File

@@ -374,6 +374,7 @@ warning.config.loot_table.function.apply_bonus.missing_enchantment: "<yellow>Iss
warning.config.loot_table.function.apply_bonus.missing_formula: "<yellow>Issue found in file <arg:0> - '<arg:1>' has a misconfigured loot table, function 'apply_bonus' is missing the required 'formula' argument.</yellow>"
warning.config.loot_table.function.drop_exp.missing_count: "<yellow>Issue found in file <arg:0> - '<arg:1>' has a misconfigured loot table, function 'drop_exp' is missing the required 'count' argument.</yellow>"
warning.config.loot_table.function.set_count.missing_count: "<yellow>Issue found in file <arg:0> - '<arg:1>' has a misconfigured loot table, function 'set_count' is missing the required 'count' argument.</yellow>"
warning.config.loot_table.function.apply_data.missing_data: "<yellow>Issue found in file <arg:0> - '<arg:1>' has a misconfigured loot table, function 'apply_data' is missing the required 'data' argument.</yellow>"
warning.config.loot_table.entry.missing_type: "<yellow>Issue found in file <arg:0> - '<arg:1>' has a misconfigured loot table, one of the entries is missing the required 'type' argument.</yellow>"
warning.config.loot_table.entry.invalid_type: "<yellow>Issue found in file <arg:0> - '<arg:1>' has a misconfigured loot table, one of the entries is using an invalid entry type '<arg:2>'.</yellow>"
warning.config.loot_table.entry.exp.missing_count: "<yellow>Issue found in file <arg:0> - '<arg:1>' has a misconfigured loot table, entry 'exp' is missing the required 'count' argument.</yellow>"

View File

@@ -73,6 +73,10 @@ public class UseOnContext {
return this.level;
}
public World getWorld() {
return this.level;
}
public Direction getHorizontalDirection() {
return this.player == null ? Direction.NORTH : this.player.getDirection();
}

View File

@@ -0,0 +1,58 @@
package net.momirealms.craftengine.core.loot.function;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.item.modifier.ItemDataModifier;
import net.momirealms.craftengine.core.item.recipe.result.ApplyItemDataPostProcessor;
import net.momirealms.craftengine.core.loot.LootConditions;
import net.momirealms.craftengine.core.loot.LootContext;
import net.momirealms.craftengine.core.plugin.context.Condition;
import net.momirealms.craftengine.core.plugin.context.number.NumberProvider;
import net.momirealms.craftengine.core.plugin.context.number.NumberProviders;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.*;
public class ApplyDataFunction<T> extends AbstractLootConditionalFunction<T> {
public static final Factory<?> FACTORY = new Factory<>();
private final ItemDataModifier<?>[] modifiers;
public ApplyDataFunction(List<Condition<LootContext>> conditions, ItemDataModifier<?>[] modifiers) {
super(conditions);
this.modifiers = modifiers;
}
@Override
public Key type() {
return LootFunctions.APPLY_DATA;
}
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
protected Item<T> applyInternal(Item<T> item, LootContext context) {
ItemBuildContext ctx = ItemBuildContext.of(context.player());
for (ItemDataModifier modifier : this.modifiers) {
item = modifier.apply(item, ctx);
}
return item;
}
public static class Factory<A> implements LootFunctionFactory<A> {
@SuppressWarnings("unchecked")
@Override
public LootFunction<A> create(Map<String, Object> arguments) {
List<ItemDataModifier<?>> modifiers = new ArrayList<>();
Map<String, Object> data = ResourceConfigUtils.getAsMap(ResourceConfigUtils.requireNonNullOrThrow(arguments.get("data"), "warning.config.loot_table.function.apply_data.missing_data"), "data");
for (Map.Entry<String, Object> entry : data.entrySet()) {
Optional.ofNullable(BuiltInRegistries.ITEM_DATA_MODIFIER_FACTORY.getValue(Key.withDefaultNamespace(entry.getKey(), Key.DEFAULT_NAMESPACE)))
.ifPresent(factory -> modifiers.add(factory.create(entry.getValue())));
}
List<Condition<LootContext>> conditions = Optional.ofNullable(arguments.get("conditions"))
.map(it -> LootConditions.fromMapList((List<Map<String, Object>>) it))
.orElse(Collections.emptyList());
return new ApplyDataFunction<>(conditions, modifiers.toArray(new ItemDataModifier[0]));
}
}
}

View File

@@ -17,6 +17,7 @@ import java.util.function.BiFunction;
public class LootFunctions {
public static final Key APPLY_BONUS = Key.from("craftengine:apply_bonus");
public static final Key APPLY_DATA = Key.from("craftengine:apply_data");
public static final Key SET_COUNT = Key.from("craftengine:set_count");
public static final Key EXPLOSION_DECAY = Key.from("craftengine:explosion_decay");
public static final Key DROP_EXP = Key.from("craftengine:drop_exp");
@@ -24,6 +25,7 @@ public class LootFunctions {
static {
register(SET_COUNT, SetCountFunction.FACTORY);
register(APPLY_DATA, ApplyDataFunction.FACTORY);
register(EXPLOSION_DECAY, ExplosionDecayFunction.FACTORY);
register(APPLY_BONUS, ApplyBonusCountFunction.FACTORY);
register(DROP_EXP, DropExpFunction.FACTORY);