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-12-05 02:46:10 +08:00
parent 97f45c81eb
commit 5902d3c955
9 changed files with 122 additions and 6 deletions

View File

@@ -108,11 +108,6 @@ public class CheckItemExpansion extends PlaceholderExpansion {
} }
private int getItemCount(BukkitServerPlayer player, String[] param) { private int getItemCount(BukkitServerPlayer player, String[] param) {
Key itemId = Key.of(param[0], param[1]); return player.clearOrCountMatchingInventoryItems(Key.of(param[0], param[1]), 0);
Predicate<Object> predicate = nmsStack -> this.plugin.itemManager().wrap(ItemStackUtils.asCraftMirror(nmsStack)).id().equals(itemId);
Object inventory = FastNMS.INSTANCE.method$Player$getInventory(player.serverPlayer());
Object inventoryMenu = FastNMS.INSTANCE.field$Player$inventoryMenu(player.serverPlayer());
Object craftSlots = FastNMS.INSTANCE.method$InventoryMenu$getCraftSlots(inventoryMenu);
return FastNMS.INSTANCE.method$Inventory$clearOrCountMatchingItems(inventory, predicate, 0, craftSlots);
} }
} }

View File

@@ -75,6 +75,7 @@ import java.lang.ref.Reference;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
public class BukkitServerPlayer extends Player { public class BukkitServerPlayer extends Player {
public static final Key SELECTED_LOCALE_KEY = Key.of("craftengine:locale"); public static final Key SELECTED_LOCALE_KEY = Key.of("craftengine:locale");
@@ -1492,6 +1493,15 @@ public class BukkitServerPlayer extends Player {
this.trackedBlockEntityRenderers.clear(); this.trackedBlockEntityRenderers.clear();
} }
@Override
public int clearOrCountMatchingInventoryItems(Key itemId, int count) {
Predicate<Object> predicate = nmsStack -> this.plugin.itemManager().wrap(ItemStackUtils.asCraftMirror(nmsStack)).id().equals(itemId);
Object inventory = FastNMS.INSTANCE.method$Player$getInventory(serverPlayer());
Object inventoryMenu = FastNMS.INSTANCE.field$Player$inventoryMenu(serverPlayer());
Object craftSlots = FastNMS.INSTANCE.method$InventoryMenu$getCraftSlots(inventoryMenu);
return FastNMS.INSTANCE.method$Inventory$clearOrCountMatchingItems(inventory, predicate, count, craftSlots);
}
@Override @Override
public void addTrackedFurniture(int entityId, Furniture furniture) { public void addTrackedFurniture(int entityId, Furniture furniture) {
this.trackedFurniture.put(entityId, new VirtualCullableObject(furniture)); this.trackedFurniture.put(entityId, new VirtualCullableObject(furniture));

View File

@@ -226,6 +226,8 @@ public abstract class Player extends AbstractEntity implements NetWorkUser {
public abstract void clearTrackedBlockEntities(); public abstract void clearTrackedBlockEntities();
public abstract int clearOrCountMatchingInventoryItems(Key itemId, int count);
@Override @Override
public void remove() { public void remove() {
} }

View File

@@ -31,4 +31,5 @@ public final class CommonConditions {
public static final Key IS_NULL = Key.from("craftengine:is_null"); public static final Key IS_NULL = Key.from("craftengine:is_null");
public static final Key HAND = Key.from("craftengine:hand"); public static final Key HAND = Key.from("craftengine:hand");
public static final Key HAS_PLAYER = Key.from("craftengine:has_player"); public static final Key HAS_PLAYER = Key.from("craftengine:has_player");
public static final Key INVENTORY_HAS_ITEM = Key.from("craftengine:inventory_has_item");
} }

View File

@@ -0,0 +1,51 @@
package net.momirealms.craftengine.core.plugin.context.condition;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.item.Item;
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.parameter.DirectContextParameters;
import net.momirealms.craftengine.core.util.ItemUtils;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.Optional;
public class InventoryHasItemCondition<CTX extends Context> implements Condition<CTX> {
private final Key itemId;
private final NumberProvider count;
public InventoryHasItemCondition(Key itemId, NumberProvider count) {
this.itemId = itemId;
this.count = count;
}
@Override
public Key type() {
return CommonConditions.INVENTORY_HAS_ITEM;
}
@Override
public boolean test(CTX ctx) {
Optional<Player> optionalPlayer = ctx.getOptionalParameter(DirectContextParameters.PLAYER);
if (optionalPlayer.isEmpty()) {
return false;
}
Player player = optionalPlayer.get();
return player.clearOrCountMatchingInventoryItems(this.itemId, 0) >= this.count.getInt(ctx);
}
public static class FactoryImpl<CTX extends Context> implements ConditionFactory<CTX> {
@Override
public Condition<CTX> create(Map<String, Object> arguments) {
Key itemId = Key.of(ResourceConfigUtils.requireNonEmptyStringOrThrow(ResourceConfigUtils.get(arguments, "id", "item"), "warning.config.condition.inventory_has_item.missing_item_id"));
NumberProvider count = NumberProviders.fromObject(arguments.getOrDefault("count", 1));
return new InventoryHasItemCondition<>(itemId, count);
}
}
}

View File

@@ -40,6 +40,7 @@ public class EventConditions {
register(CommonConditions.IS_NULL, new IsNullCondition.FactoryImpl<>()); register(CommonConditions.IS_NULL, new IsNullCondition.FactoryImpl<>());
register(CommonConditions.HAND, new HandCondition.FactoryImpl<>()); register(CommonConditions.HAND, new HandCondition.FactoryImpl<>());
register(CommonConditions.ON_COOLDOWN, new OnCooldownCondition.FactoryImpl<>()); register(CommonConditions.ON_COOLDOWN, new OnCooldownCondition.FactoryImpl<>());
register(CommonConditions.INVENTORY_HAS_ITEM, new InventoryHasItemCondition.FactoryImpl<>());
} }
public static void register(Key key, ConditionFactory<Context> factory) { public static void register(Key key, ConditionFactory<Context> factory) {

View File

@@ -58,6 +58,7 @@ public class EventFunctions {
register(CommonFunctions.SET_EXP, new SetExpFunction.FactoryImpl<>(EventConditions::fromMap)); register(CommonFunctions.SET_EXP, new SetExpFunction.FactoryImpl<>(EventConditions::fromMap));
register(CommonFunctions.SET_LEVEL, new SetLevelFunction.FactoryImpl<>(EventConditions::fromMap)); register(CommonFunctions.SET_LEVEL, new SetLevelFunction.FactoryImpl<>(EventConditions::fromMap));
register(CommonFunctions.PLAY_TOTEM_ANIMATION, new PlayTotemAnimationFunction.FactoryImpl<>(EventConditions::fromMap)); register(CommonFunctions.PLAY_TOTEM_ANIMATION, new PlayTotemAnimationFunction.FactoryImpl<>(EventConditions::fromMap));
register(CommonFunctions.CLEAR_ITEM, new ClearItemFunction.FactoryImpl<>(EventConditions::fromMap));
} }
public static void register(Key key, FunctionFactory<Context> factory) { public static void register(Key key, FunctionFactory<Context> factory) {

View File

@@ -0,0 +1,54 @@
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.parameter.DirectContextParameters;
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.Optional;
public class ClearItemFunction<CTX extends Context> extends AbstractConditionalFunction<CTX> {
private final Key itemId;
private final NumberProvider count;
public ClearItemFunction(List<Condition<CTX>> predicates, Key itemId, NumberProvider count) {
super(predicates);
this.itemId = itemId;
this.count = count;
}
@Override
protected void runInternal(CTX ctx) {
Optional<Player> optionalPlayer = ctx.getOptionalParameter(DirectContextParameters.PLAYER);
if (optionalPlayer.isEmpty()) {
return;
}
Player player = optionalPlayer.get();
player.clearOrCountMatchingInventoryItems(itemId, count.getInt(ctx));
}
@Override
public Key type() {
return CommonFunctions.CLEAR_ITEM;
}
public static class FactoryImpl<CTX extends Context> extends AbstractFactory<CTX> {
public FactoryImpl(java.util.function.Function<Map<String, Object>, Condition<CTX>> factory) {
super(factory);
}
@Override
public Function<CTX> create(Map<String, Object> arguments) {
Key itemId = Key.of(ResourceConfigUtils.requireNonEmptyStringOrThrow(ResourceConfigUtils.get(arguments, "id", "item"), "warning.config.function.clear_item.missing_item_id"));
NumberProvider count = NumberProviders.fromObject(arguments.getOrDefault("count", 1));
return new ClearItemFunction<>(getPredicates(arguments), itemId, count);
}
}
}

View File

@@ -49,4 +49,5 @@ public final class CommonFunctions {
public static final Key SET_EXP = Key.of("craftengine:set_exp"); public static final Key SET_EXP = Key.of("craftengine:set_exp");
public static final Key SET_LEVEL = Key.of("craftengine:set_level"); public static final Key SET_LEVEL = Key.of("craftengine:set_level");
public static final Key PLAY_TOTEM_ANIMATION = Key.of("craftengine:play_totem_animation"); public static final Key PLAY_TOTEM_ANIMATION = Key.of("craftengine:play_totem_animation");
public static final Key CLEAR_ITEM = Key.of("craftengine:clear_item");
} }