9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2025-12-28 03:19:12 +00:00

improve give item

This commit is contained in:
XiaoMoMi
2024-07-25 21:47:47 +08:00
parent 6a4b1dce5d
commit 15a85648b1
4 changed files with 21 additions and 3 deletions

View File

@@ -448,6 +448,7 @@ public class BukkitActionManager implements ActionManager<Player> {
if (args instanceof Section section) {
String id = section.getString("item");
int amount = section.getInt("amount", 1);
boolean toInventory = section.getBoolean("to-inventory", false);
return context -> {
if (Math.random() > chance) return;
Player player = context.getHolder();
@@ -460,7 +461,11 @@ public class BukkitActionManager implements ActionManager<Player> {
amountToGive -= perStackSize;
ItemStack more = itemStack.clone();
more.setAmount(perStackSize);
PlayerUtils.dropItem(player, itemStack, true, true, false);
if (toInventory) {
PlayerUtils.giveItem(player, itemStack, itemStack.getAmount());
} else {
PlayerUtils.dropItem(player, itemStack, true, true, false);
}
}
}
};

View File

@@ -59,8 +59,10 @@ public class GetItemCommand extends BukkitCommandFeature<CommandSender> {
}))
.optional("amount", IntegerParser.integerParser(1, 6400))
.flag(manager.flagBuilder("silent").withAliases("s").build())
.flag(manager.flagBuilder("to-inventory").withAliases("t").build())
.handler(context -> {
final int amount = context.getOrDefault("amount", 1);
boolean toInv = context.flags().hasFlag("to-inventory");
final String id = context.get("id");
final Player player = context.sender();
try {
@@ -75,7 +77,11 @@ public class GetItemCommand extends BukkitCommandFeature<CommandSender> {
amountToGive -= perStackSize;
ItemStack more = itemStack.clone();
more.setAmount(perStackSize);
PlayerUtils.dropItem(player, more, false, true, false);
if (toInv) {
PlayerUtils.putItemsToInventory(player.getInventory(), more, more.getAmount());
} else {
PlayerUtils.dropItem(player, more, false, true, false);
}
}
handleFeedback(context, MessageConstants.COMMAND_ITEM_GET_SUCCESS, Component.text(amount), Component.text(id));
} catch (NullPointerException e) {

View File

@@ -60,10 +60,12 @@ public class GiveItemCommand extends BukkitCommandFeature<CommandSender> {
}))
.optional("amount", IntegerParser.integerParser(1, 6400))
.flag(manager.flagBuilder("silent").withAliases("s").build())
.flag(manager.flagBuilder("to-inventory").withAliases("t").build())
.handler(context -> {
final Player player = context.get("player");
final int amount = context.getOrDefault("amount", 1);
final String id = context.get("id");
boolean toInv = context.flags().hasFlag("to-inventory");
try {
ItemStack itemStack = BukkitCustomFishingPlugin.getInstance().getItemManager().buildInternal(Context.player(player).arg(ContextKeys.ID, id), id);
if (itemStack == null) {
@@ -76,7 +78,11 @@ public class GiveItemCommand extends BukkitCommandFeature<CommandSender> {
amountToGive -= perStackSize;
ItemStack more = itemStack.clone();
more.setAmount(perStackSize);
PlayerUtils.dropItem(player, more, false, true, false);
if (toInv) {
PlayerUtils.putItemsToInventory(player.getInventory(), more, more.getAmount());
} else {
PlayerUtils.dropItem(player, more, false, true, false);
}
}
handleFeedback(context, MessageConstants.COMMAND_ITEM_GIVE_SUCCESS, Component.text(player.getName()), Component.text(amount), Component.text(id));
} catch (NullPointerException e) {

View File

@@ -38,6 +38,7 @@ public class PlayerUtils {
Location location = player.getLocation().clone();
Item item = player.getWorld().dropItem(player.getEyeLocation().clone().subtract(new Vector(0,0.3,0)), itemStack);
item.setPickupDelay(noPickUpDelay ? 0 : 40);
item.setOwner(player.getUniqueId());
if (retainOwnership) {
item.setThrower(player.getUniqueId());
}