|
| 1 | +package org.ayosynk.landClaimPlugin.commands; |
| 2 | + |
| 3 | +import org.ayosynk.landClaimPlugin.LandClaimPlugin; |
| 4 | +import org.ayosynk.landClaimPlugin.managers.ClaimManager; |
| 5 | +import org.ayosynk.landClaimPlugin.managers.ConfigManager; |
| 6 | +import org.ayosynk.landClaimPlugin.models.ClaimProfile; |
| 7 | +import org.bukkit.Bukkit; |
| 8 | +import org.bukkit.entity.Player; |
| 9 | +import org.bukkit.plugin.Plugin; |
| 10 | +import org.incendo.cloud.Command; |
| 11 | +import org.incendo.cloud.paper.PaperCommandManager; |
| 12 | +import org.incendo.cloud.paper.util.sender.PlayerSource; |
| 13 | +import org.incendo.cloud.paper.util.sender.Source; |
| 14 | + |
| 15 | +import java.lang.reflect.Method; |
| 16 | + |
| 17 | +public class BuyCommand implements LandClaimCommand { |
| 18 | + |
| 19 | + private final LandClaimPlugin plugin; |
| 20 | + private final ClaimManager claimManager; |
| 21 | + private final ConfigManager configManager; |
| 22 | + |
| 23 | + public BuyCommand(LandClaimPlugin plugin, ClaimManager claimManager, ConfigManager configManager) { |
| 24 | + this.plugin = plugin; |
| 25 | + this.claimManager = claimManager; |
| 26 | + this.configManager = configManager; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void register(PaperCommandManager<Source> manager, Command.Builder<PlayerSource> claimBuilder) { |
| 31 | + Command.Builder<PlayerSource> buyBuilder = claimBuilder.literal("buy"); |
| 32 | + |
| 33 | + // /claim buy claim [amount] |
| 34 | + manager.command(buyBuilder.literal("claim") |
| 35 | + .optional("amount", org.incendo.cloud.parser.standard.IntegerParser.integerParser(1)) |
| 36 | + .handler(context -> { |
| 37 | + Player player = context.sender().source(); |
| 38 | + int amount = context.getOrDefault("amount", 1); |
| 39 | + if (amount <= 0) { |
| 40 | + player.sendMessage(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage() |
| 41 | + .deserialize("<red>Amount must be greater than 0.")); |
| 42 | + return; |
| 43 | + } |
| 44 | + double costPer = getCost("claimBlockCost", 50.0); |
| 45 | + double totalCost = costPer * amount; |
| 46 | + if (!chargePlayer(player, totalCost, "BUY_CLAIM_BLOCKS")) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + plugin.addBonusBlocks(player.getUniqueId(), amount).thenAccept(newTotal -> { |
| 51 | + player.sendMessage(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage() |
| 52 | + .deserialize("<green>Successfully purchased <gold>" + amount + "</gold> claim blocks! New limit: <gold>" + plugin.getClaimManager().getClaimLimit(player) + "</gold>")); |
| 53 | + }); |
| 54 | + })); |
| 55 | + |
| 56 | + // /claim buy role |
| 57 | + manager.command(buyBuilder.literal("role") |
| 58 | + .handler(context -> { |
| 59 | + Player player = context.sender().source(); |
| 60 | + ClaimProfile profile = claimManager.getActiveProfile(player); |
| 61 | + if (profile == null) { |
| 62 | + player.sendMessage(configManager.getMessage("no-profile")); |
| 63 | + return; |
| 64 | + } |
| 65 | + if (!profile.isOwner(player.getUniqueId())) { |
| 66 | + player.sendMessage(configManager.getMessage("not-owner")); |
| 67 | + return; |
| 68 | + } |
| 69 | + double cost = getCost("roleSlotCost", 150.0); |
| 70 | + if (!chargePlayer(player, cost, "BUY_ROLE_SLOT")) { |
| 71 | + return; |
| 72 | + } |
| 73 | + profile.setBonusRoleSlots(profile.getBonusRoleSlots() + 1); |
| 74 | + claimManager.saveAndSync(profile); |
| 75 | + player.sendMessage(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage() |
| 76 | + .deserialize("<green>Successfully purchased an additional custom role slot! Total bonus slots: <gold>" + profile.getBonusRoleSlots() + "</gold>")); |
| 77 | + })); |
| 78 | + |
| 79 | + // /claim buy member |
| 80 | + manager.command(buyBuilder.literal("member") |
| 81 | + .handler(context -> { |
| 82 | + Player player = context.sender().source(); |
| 83 | + ClaimProfile profile = claimManager.getActiveProfile(player); |
| 84 | + if (profile == null) { |
| 85 | + player.sendMessage(configManager.getMessage("no-profile")); |
| 86 | + return; |
| 87 | + } |
| 88 | + if (!profile.isOwner(player.getUniqueId())) { |
| 89 | + player.sendMessage(configManager.getMessage("not-owner")); |
| 90 | + return; |
| 91 | + } |
| 92 | + double cost = getCost("memberSlotCost", 50.0); |
| 93 | + if (!chargePlayer(player, cost, "BUY_MEMBER_SLOT")) { |
| 94 | + return; |
| 95 | + } |
| 96 | + profile.setBonusMemberSlots(profile.getBonusMemberSlots() + 1); |
| 97 | + claimManager.saveAndSync(profile); |
| 98 | + player.sendMessage(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage() |
| 99 | + .deserialize("<green>Successfully purchased an additional member slot! Total bonus slots: <gold>" + profile.getBonusMemberSlots() + "</gold>")); |
| 100 | + })); |
| 101 | + |
| 102 | + // /claim buy warp |
| 103 | + manager.command(buyBuilder.literal("warp") |
| 104 | + .handler(context -> { |
| 105 | + Player player = context.sender().source(); |
| 106 | + ClaimProfile profile = claimManager.getActiveProfile(player); |
| 107 | + if (profile == null) { |
| 108 | + player.sendMessage(configManager.getMessage("no-profile")); |
| 109 | + return; |
| 110 | + } |
| 111 | + if (!profile.isOwner(player.getUniqueId())) { |
| 112 | + player.sendMessage(configManager.getMessage("not-owner")); |
| 113 | + return; |
| 114 | + } |
| 115 | + double cost = getCost("warpSlotCost", 75.0); |
| 116 | + if (!chargePlayer(player, cost, "BUY_WARP_SLOT")) { |
| 117 | + return; |
| 118 | + } |
| 119 | + profile.setBonusWarpSlots(profile.getBonusWarpSlots() + 1); |
| 120 | + claimManager.saveAndSync(profile); |
| 121 | + player.sendMessage(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage() |
| 122 | + .deserialize("<green>Successfully purchased an additional warp slot! Total bonus slots: <gold>" + profile.getBonusWarpSlots() + "</gold>")); |
| 123 | + })); |
| 124 | + } |
| 125 | + |
| 126 | + private double getCost(String fieldName, double defaultValue) { |
| 127 | + try { |
| 128 | + Plugin ecoPlugin = Bukkit.getPluginManager().getPlugin("LandClaimPlugin-Economy"); |
| 129 | + if (ecoPlugin != null && ecoPlugin.isEnabled()) { |
| 130 | + Object config = ecoPlugin.getClass().getMethod("getEconomyConfig").invoke(ecoPlugin); |
| 131 | + return (double) config.getClass().getField(fieldName).get(config); |
| 132 | + } |
| 133 | + } catch (Exception ignored) {} |
| 134 | + return defaultValue; |
| 135 | + } |
| 136 | + |
| 137 | + private boolean chargePlayer(Player player, double cost, String actionType) { |
| 138 | + try { |
| 139 | + Plugin ecoPlugin = Bukkit.getPluginManager().getPlugin("LandClaimPlugin-Economy"); |
| 140 | + if (ecoPlugin == null || !ecoPlugin.isEnabled()) { |
| 141 | + player.sendMessage(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage() |
| 142 | + .deserialize("<red>Economy features are disabled.")); |
| 143 | + return false; |
| 144 | + } |
| 145 | + Class<?> ecoHookClass = Class.forName("org.ayosynk.landclaimeconomy.util.EconomyHook"); |
| 146 | + Method formatMethod = ecoHookClass.getMethod("format", double.class); |
| 147 | + |
| 148 | + // Check balance |
| 149 | + double balance = (double) ecoHookClass.getMethod("getBalance", org.bukkit.OfflinePlayer.class).invoke(null, player); |
| 150 | + if (balance < cost) { |
| 151 | + String costStr = (String) formatMethod.invoke(null, cost); |
| 152 | + String balStr = (String) formatMethod.invoke(null, balance); |
| 153 | + player.sendMessage(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage() |
| 154 | + .deserialize("<red>Insufficient funds! Cost: " + costStr + ", Balance: " + balStr)); |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + // Withdraw |
| 159 | + boolean success = (boolean) ecoHookClass.getMethod("withdraw", org.bukkit.OfflinePlayer.class, double.class).invoke(null, player, cost); |
| 160 | + if (!success) { |
| 161 | + player.sendMessage(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage() |
| 162 | + .deserialize("<red>Transaction failed.")); |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | + // Log transaction in database |
| 167 | + Object database = ecoPlugin.getClass().getMethod("getDatabase").invoke(ecoPlugin); |
| 168 | + Method logMethod = database.getClass().getMethod("logTransaction", String.class, String.class, String.class, double.class, String.class); |
| 169 | + logMethod.invoke(database, player.getUniqueId().toString(), null, actionType, cost, null); |
| 170 | + |
| 171 | + return true; |
| 172 | + } catch (Exception e) { |
| 173 | + plugin.getLogger().severe("Error charging player: " + e.getMessage()); |
| 174 | + e.printStackTrace(); |
| 175 | + player.sendMessage(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage() |
| 176 | + .deserialize("<red>An error occurred while processing the purchase.")); |
| 177 | + return false; |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments