|
1 | 1 | package world.bentobox.level.commands; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | 3 | import java.util.List; |
5 | 4 | import java.util.Optional; |
6 | 5 |
|
7 | 6 | import org.bukkit.Material; |
8 | 7 | import org.bukkit.inventory.ItemStack; |
9 | 8 |
|
10 | 9 | import world.bentobox.bentobox.api.commands.CompositeCommand; |
| 10 | +import world.bentobox.bentobox.api.commands.ConfirmableCommand; |
11 | 11 | import world.bentobox.bentobox.api.localization.TextVariables; |
12 | 12 | import world.bentobox.bentobox.api.user.User; |
13 | 13 | import world.bentobox.bentobox.database.objects.Island; |
| 14 | +import world.bentobox.bentobox.util.Util; |
14 | 15 | import world.bentobox.level.Level; |
15 | 16 | import world.bentobox.level.panels.DonationPanel; |
16 | 17 | import world.bentobox.level.util.Utils; |
|
21 | 22 | * |
22 | 23 | * @author tastybento |
23 | 24 | */ |
24 | | -public class IslandDonateCommand extends CompositeCommand { |
| 25 | +public class IslandDonateCommand extends ConfirmableCommand { |
25 | 26 |
|
26 | 27 | private final Level addon; |
27 | 28 |
|
@@ -79,56 +80,79 @@ private boolean handleHandDonation(User user, Island island, List<String> args) |
79 | 80 | return false; |
80 | 81 | } |
81 | 82 |
|
82 | | - Material material = hand.getType(); |
83 | | - Integer blockValue = addon.getBlockConfig().getValue(getWorld(), material); |
| 83 | + final Material material = hand.getType(); |
| 84 | + final Integer blockValue = addon.getBlockConfig().getValue(getWorld(), material); |
84 | 85 | if (blockValue == null || blockValue <= 0) { |
85 | 86 | user.sendMessage("island.donate.no-value"); |
86 | 87 | return false; |
87 | 88 | } |
88 | 89 |
|
89 | | - // Determine amount |
90 | | - int amount = hand.getAmount(); |
| 90 | + int requested = hand.getAmount(); |
91 | 91 | if (args.size() > 1) { |
| 92 | + if ("help".equalsIgnoreCase(args.get(1))) { |
| 93 | + showHelp(this, user); |
| 94 | + return true; |
| 95 | + } |
92 | 96 | try { |
93 | | - amount = Integer.parseInt(args.get(1)); |
94 | | - if (amount < 1) { |
| 97 | + requested = Integer.parseInt(args.get(1)); |
| 98 | + if (requested < 1) { |
95 | 99 | user.sendMessage("island.donate.invalid-amount"); |
96 | 100 | return false; |
97 | 101 | } |
98 | | - amount = Math.min(amount, hand.getAmount()); |
99 | 102 | } catch (NumberFormatException e) { |
100 | 103 | user.sendMessage("island.donate.invalid-amount"); |
101 | 104 | return false; |
102 | 105 | } |
103 | 106 | } |
104 | 107 |
|
105 | | - // Calculate points |
| 108 | + final int previewAmount = Math.min(requested, hand.getAmount()); |
| 109 | + final long previewPoints = (long) previewAmount * blockValue; |
| 110 | + final int finalRequested = requested; |
| 111 | + |
| 112 | + String prompt = user.getTranslation("island.donate.hand.confirm-prompt", |
| 113 | + TextVariables.NUMBER, String.valueOf(previewAmount), |
| 114 | + "[material]", Utils.prettifyObject(material, user), |
| 115 | + "[points]", Utils.formatNumber(user, previewPoints)); |
| 116 | + |
| 117 | + askConfirmation(user, prompt, () -> performHandDonation(user, island, material, blockValue, finalRequested)); |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + private void performHandDonation(User user, Island island, Material material, int blockValue, int requested) { |
| 122 | + ItemStack currentHand = user.getPlayer().getInventory().getItemInMainHand(); |
| 123 | + if (currentHand.getType() != material || currentHand.getAmount() == 0) { |
| 124 | + user.sendMessage("island.donate.hand.not-block"); |
| 125 | + return; |
| 126 | + } |
| 127 | + int amount = Math.min(requested, currentHand.getAmount()); |
106 | 128 | long points = (long) amount * blockValue; |
107 | 129 |
|
108 | | - // Remove items from hand |
109 | | - if (amount >= hand.getAmount()) { |
| 130 | + if (amount >= currentHand.getAmount()) { |
110 | 131 | user.getPlayer().getInventory().setItemInMainHand(null); |
111 | 132 | } else { |
112 | | - hand.setAmount(hand.getAmount() - amount); |
| 133 | + currentHand.setAmount(currentHand.getAmount() - amount); |
113 | 134 | } |
114 | 135 |
|
115 | | - // Record the donation |
116 | 136 | addon.getManager().donateBlocks(island, user.getUniqueId(), material.name(), amount, points); |
117 | 137 |
|
118 | | - // Notify the player |
119 | 138 | user.sendMessage("island.donate.hand.success", |
120 | 139 | TextVariables.NUMBER, String.valueOf(amount), |
121 | 140 | "[material]", Utils.prettifyObject(material, user), |
122 | | - "[points]", String.valueOf(points)); |
123 | | - |
124 | | - return true; |
| 141 | + "[points]", Utils.formatNumber(user, points)); |
125 | 142 | } |
126 | 143 |
|
127 | 144 | @Override |
128 | 145 | public Optional<List<String>> tabComplete(User user, String alias, List<String> args) { |
129 | | - if (args.size() == 1) { |
130 | | - return Optional.of(List.of("hand")); |
| 146 | + String lastArg = !args.isEmpty() ? args.get(args.size() - 1) : ""; |
| 147 | + if (args.size() <= 2) { |
| 148 | + return Optional.of(Util.tabLimit(List.of("hand"), lastArg)); |
| 149 | + } |
| 150 | + if (args.size() == 3 && "hand".equalsIgnoreCase(args.get(1)) && user.isPlayer()) { |
| 151 | + int held = user.getPlayer().getInventory().getItemInMainHand().getAmount(); |
| 152 | + if (held > 0) { |
| 153 | + return Optional.of(Util.tabLimit(List.of(String.valueOf(held)), lastArg)); |
| 154 | + } |
131 | 155 | } |
132 | | - return Optional.of(new ArrayList<>()); |
| 156 | + return Optional.of(List.of()); |
133 | 157 | } |
134 | 158 | } |
0 commit comments