9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-26 10:29:20 +00:00

回退错误的实现,添加限制数量函数

This commit is contained in:
jhqwqmc
2025-09-23 14:33:19 +08:00
parent 5558a6d673
commit ec0c61324d
5 changed files with 73 additions and 98 deletions

View File

@@ -0,0 +1,65 @@
package net.momirealms.craftengine.core.loot.function;
import net.momirealms.craftengine.core.item.Item;
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.util.Key;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class LimitCountFunction<T> extends AbstractLootConditionalFunction<T> {
public static final Factory<?> FACTORY = new Factory<>();
@Nullable
private final NumberProvider min;
@Nullable
private final NumberProvider max;
public LimitCountFunction(List<Condition<LootContext>> predicates, @Nullable NumberProvider min, @Nullable NumberProvider max) {
super(predicates);
this.min = min;
this.max = max;
}
@Override
public Key type() {
return LootFunctions.LIMIT_COUNT;
}
@Override
protected Item<T> applyInternal(Item<T> item, LootContext context) {
int amount = item.count();
if (min != null) {
int minAmount = min.getInt(context);
if (amount < minAmount) {
item.count(minAmount);
}
}
if (max != null) {
int maxAmount = max.getInt(context);
if (amount > maxAmount) {
item.count(maxAmount);
}
}
return item;
}
public static class Factory<A> implements LootFunctionFactory<A> {
@SuppressWarnings("unchecked")
@Override
public LootFunction<A> create(Map<String, Object> arguments) {
Object min = arguments.get("min");
Object max = arguments.get("max");
List<Condition<LootContext>> conditions = Optional.ofNullable(arguments.get("conditions"))
.map(it -> LootConditions.fromMapList((List<Map<String, Object>>) it))
.orElse(Collections.emptyList());
return new LimitCountFunction<>(conditions, min == null ? null : NumberProviders.fromObject(min), max == null ? null : NumberProviders.fromObject(max));
}
}
}

View File

@@ -20,12 +20,14 @@ public class LootFunctions {
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");
public static final Key LIMIT_COUNT = Key.from("craftengine:limit_count");
static {
register(SET_COUNT, SetCountFunction.FACTORY);
register(EXPLOSION_DECAY, ExplosionDecayFunction.FACTORY);
register(APPLY_BONUS, ApplyBonusCountFunction.FACTORY);
register(DROP_EXP, DropExpFunction.FACTORY);
register(LIMIT_COUNT, LimitCountFunction.FACTORY);
}
public static <T> void register(Key key, LootFunctionFactory<T> factory) {