mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-19 15:09:15 +00:00
添加清理物品函数和物品栏存在物品条件
This commit is contained in:
@@ -108,11 +108,6 @@ public class CheckItemExpansion extends PlaceholderExpansion {
|
||||
}
|
||||
|
||||
private int getItemCount(BukkitServerPlayer player, String[] param) {
|
||||
Key itemId = Key.of(param[0], param[1]);
|
||||
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);
|
||||
return player.clearOrCountMatchingInventoryItems(Key.of(param[0], param[1]), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +75,7 @@ import java.lang.ref.Reference;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class BukkitServerPlayer extends Player {
|
||||
public static final Key SELECTED_LOCALE_KEY = Key.of("craftengine:locale");
|
||||
@@ -1492,6 +1493,15 @@ public class BukkitServerPlayer extends Player {
|
||||
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
|
||||
public void addTrackedFurniture(int entityId, Furniture furniture) {
|
||||
this.trackedFurniture.put(entityId, new VirtualCullableObject(furniture));
|
||||
|
||||
@@ -226,6 +226,8 @@ public abstract class Player extends AbstractEntity implements NetWorkUser {
|
||||
|
||||
public abstract void clearTrackedBlockEntities();
|
||||
|
||||
public abstract int clearOrCountMatchingInventoryItems(Key itemId, int count);
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
}
|
||||
|
||||
@@ -31,4 +31,5 @@ public final class CommonConditions {
|
||||
public static final Key IS_NULL = Key.from("craftengine:is_null");
|
||||
public static final Key HAND = Key.from("craftengine:hand");
|
||||
public static final Key HAS_PLAYER = Key.from("craftengine:has_player");
|
||||
public static final Key INVENTORY_HAS_ITEM = Key.from("craftengine:inventory_has_item");
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,7 @@ public class EventConditions {
|
||||
register(CommonConditions.IS_NULL, new IsNullCondition.FactoryImpl<>());
|
||||
register(CommonConditions.HAND, new HandCondition.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) {
|
||||
|
||||
@@ -58,6 +58,7 @@ public class EventFunctions {
|
||||
register(CommonFunctions.SET_EXP, new SetExpFunction.FactoryImpl<>(EventConditions::fromMap));
|
||||
register(CommonFunctions.SET_LEVEL, new SetLevelFunction.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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,4 +49,5 @@ public final class CommonFunctions {
|
||||
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 PLAY_TOTEM_ANIMATION = Key.of("craftengine:play_totem_animation");
|
||||
public static final Key CLEAR_ITEM = Key.of("craftengine:clear_item");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user