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

改善get item手感

This commit is contained in:
XiaoMoMi
2025-08-11 02:01:49 +08:00
parent c536a6c184
commit 668f3d24c8
12 changed files with 76 additions and 39 deletions

View File

@@ -335,7 +335,7 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
@Override
public ItemStack buildCustomItemStack(Key id, Player player) {
return Optional.ofNullable(this.customItems.get(id)).map(it -> it.buildItemStack(new ItemBuildContext(player, ContextHolder.EMPTY), 1)).orElse(null);
return Optional.ofNullable(this.customItemsById.get(id)).map(it -> it.buildItemStack(new ItemBuildContext(player, ContextHolder.EMPTY), 1)).orElse(null);
}
@Override
@@ -349,12 +349,12 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
@Override
public Item<ItemStack> createCustomWrappedItem(Key id, Player player) {
return Optional.ofNullable(customItems.get(id)).map(it -> it.buildItem(player)).orElse(null);
return Optional.ofNullable(customItemsById.get(id)).map(it -> it.buildItem(player)).orElse(null);
}
@Override
public Item<ItemStack> createWrappedItem(Key id, @Nullable Player player) {
CustomItem<ItemStack> customItem = this.customItems.get(id);
CustomItem<ItemStack> customItem = this.customItemsById.get(id);
if (customItem != null) {
return customItem.buildItem(player);
}

View File

@@ -1,8 +1,12 @@
package net.momirealms.craftengine.bukkit.plugin.command.feature;
import net.kyori.adventure.text.Component;
import net.momirealms.craftengine.bukkit.api.CraftEngineItems;
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature;
import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer;
import net.momirealms.craftengine.bukkit.util.PlayerUtils;
import net.momirealms.craftengine.core.item.CustomItem;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager;
import net.momirealms.craftengine.core.plugin.command.FlagKeys;
@@ -47,12 +51,18 @@ public class GetItemCommand extends BukkitCommandFeature<CommandSender> {
int amount = context.getOrDefault("amount", 1);
boolean toInv = context.flags().hasFlag(FlagKeys.TO_INVENTORY);
NamespacedKey namespacedKey = context.get("id");
Key key = Key.of(namespacedKey.namespace(), namespacedKey.value());
ItemStack builtItem = plugin().itemManager().buildCustomItemStack(key, plugin().adapt(context.sender()));
if (builtItem == null) {
handleFeedback(context, MessageConstants.COMMAND_ITEM_GET_FAILURE_NOT_EXIST, Component.text(key.toString()));
return;
Key itemId = Key.of(namespacedKey.namespace(), namespacedKey.value());
CustomItem<ItemStack> customItem = CraftEngineItems.byId(itemId);
if (customItem == null) {
customItem = BukkitItemManager.instance().getCustomItemByPathOnly(itemId.value()).orElse(null);
if (customItem == null) {
handleFeedback(context, MessageConstants.COMMAND_ITEM_GET_FAILURE_NOT_EXIST, Component.text(itemId.toString()));
return;
} else {
itemId = customItem.id();
}
}
ItemStack builtItem = customItem.buildItemStack(plugin().adapt(context.sender()));
int amountToGive = amount;
int maxStack = builtItem.getMaxStackSize();
while (amountToGive > 0) {
@@ -66,7 +76,7 @@ public class GetItemCommand extends BukkitCommandFeature<CommandSender> {
PlayerUtils.dropItem(player, more, false, true, false);
}
}
handleFeedback(context, MessageConstants.COMMAND_ITEM_GET_SUCCESS, Component.text(amount), Component.text(key.toString()));
handleFeedback(context, MessageConstants.COMMAND_ITEM_GET_SUCCESS, Component.text(amount), Component.text(itemId.toString()));
});
}

View File

@@ -1,6 +1,7 @@
package net.momirealms.craftengine.bukkit.plugin.command.feature;
import net.kyori.adventure.text.Component;
import net.momirealms.craftengine.bukkit.api.CraftEngineItems;
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature;
import net.momirealms.craftengine.bukkit.util.PlayerUtils;
@@ -54,16 +55,20 @@ public class GiveItemCommand extends BukkitCommandFeature<CommandSender> {
int amount = context.getOrDefault("amount", 1);
boolean toInv = context.flags().hasFlag(FlagKeys.TO_INVENTORY);
NamespacedKey namespacedKey = context.get("id");
Key key = Key.of(namespacedKey.namespace(), namespacedKey.value());
Optional<CustomItem<ItemStack>> optionalItem = BukkitItemManager.instance().getCustomItem(key);
if (optionalItem.isEmpty()) {
handleFeedback(context, MessageConstants.COMMAND_ITEM_GIVE_FAILURE_NOT_EXIST, Component.text(key.toString()));
return;
Key itemId = Key.of(namespacedKey.namespace(), namespacedKey.value());
CustomItem<ItemStack> customItem = CraftEngineItems.byId(itemId);
if (customItem == null) {
customItem = BukkitItemManager.instance().getCustomItemByPathOnly(itemId.value()).orElse(null);
if (customItem == null) {
handleFeedback(context, MessageConstants.COMMAND_ITEM_GIVE_FAILURE_NOT_EXIST, Component.text(itemId.toString()));
return;
} else {
itemId = customItem.id();
}
}
Collection<Player> players = selector.values();
for (Player player : players) {
ItemStack builtItem = optionalItem.get().buildItemStack(plugin().adapt(player));
ItemStack builtItem = customItem.buildItemStack(plugin().adapt(player));
if (builtItem == null) {
return;
}
@@ -90,9 +95,9 @@ public class GiveItemCommand extends BukkitCommandFeature<CommandSender> {
}
}
if (players.size() == 1) {
handleFeedback(context, MessageConstants.COMMAND_ITEM_GIVE_SUCCESS_SINGLE, Component.text(amount), Component.text(key.toString()), Component.text(players.iterator().next().getName()));
handleFeedback(context, MessageConstants.COMMAND_ITEM_GIVE_SUCCESS_SINGLE, Component.text(amount), Component.text(itemId.toString()), Component.text(players.iterator().next().getName()));
} else if (players.size() > 1) {
handleFeedback(context, MessageConstants.COMMAND_ITEM_GIVE_SUCCESS_MULTIPLE, Component.text(amount), Component.text(key.toString()), Component.text(players.size()));
handleFeedback(context, MessageConstants.COMMAND_ITEM_GIVE_SUCCESS_MULTIPLE, Component.text(amount), Component.text(itemId.toString()), Component.text(players.size()));
}
});
}