Files
src
build.gradle
Code
4 252
src/main/java/com/kodari/purchaseconfirmationbridge/PurchaseConfirmationBridge.java
package com.kodari.purchaseconfirmationbridge;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public final class PurchaseConfirmationBridge extends JavaPlugin implements CommandExecutor, Listener {
private final Map<UUID, PendingPurchase> pendingPurchases = new HashMap<>();
@Override
public void onEnable() {
saveDefaultConfig();
PluginCommand confirmBuyCommand = getCommand("confirmbuy");
if (confirmBuyCommand != null) {
confirmBuyCommand.setExecutor(this);
}
Bukkit.getPluginManager().registerEvents(this, this);
}
@Override
public void onDisable() {
pendingPurchases.clear();
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player && !sender.hasPermission("purchaseconfirmationbridge.use")) {
return true;
}
Player player;
String transactionId;
String playerCommand;
if (sender instanceof Player commandPlayer) {
if (args.length < 2) {
sendMessage(sender, "messages.usage");
return true;
}
player = commandPlayer;
transactionId = args[0];
playerCommand = joinArgs(args, 1);
} else {
if (args.length < 3) {
sendMessage(sender, "messages.console-usage");
return true;
}
player = Bukkit.getPlayerExact(args[0]);
if (player == null) {
sendMessage(sender, "messages.player-not-found");
return true;
}
transactionId = args[1];
playerCommand = joinArgs(args, 2);
}
playerCommand = playerCommand.replace("%player%", player.getName()).trim();
if (playerCommand.startsWith("/")) {
playerCommand = playerCommand.substring(1);
}
if (playerCommand.isEmpty() || playerCommand.contains("\n") || playerCommand.contains("\r")) {
sendMessage(sender, sender instanceof Player ? "messages.usage" : "messages.console-usage");
return true;
}
openConfirmation(player, transactionId, playerCommand);
return true;
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
Inventory topInventory = event.getView().getTopInventory();
if (!(topInventory.getHolder() instanceof ConfirmationHolder)) {
return;
}
event.setCancelled(true);
if (!(event.getWhoClicked() instanceof Player player) || event.getClickedInventory() != topInventory) {
return;
}
PendingPurchase pending = pendingPurchases.get(player.getUniqueId());
if (pending == null || pending.inventory != topInventory) {
return;
}
if (event.getRawSlot() == 3) {
pendingPurchases.remove(player.getUniqueId(), pending);
player.closeInventory();
player.performCommand(pending.playerCommand);
sendMessage(player, "messages.confirmed");
} else if (event.getRawSlot() == 5) {
pendingPurchases.remove(player.getUniqueId(), pending);
player.closeInventory();
sendMessage(player, "messages.cancelled");
}
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent event) {
if (!(event.getPlayer() instanceof Player player)
|| !(event.getInventory().getHolder() instanceof ConfirmationHolder)) {
return;
}
PendingPurchase pending = pendingPurchases.get(player.getUniqueId());
if (pending != null && pending.inventory == event.getInventory()) {
pendingPurchases.remove(player.getUniqueId(), pending);
}
}
private void openConfirmation(Player player, String transactionId, String playerCommand) {
ConfirmationHolder holder = new ConfirmationHolder();
Inventory inventory = Bukkit.createInventory(holder, 9, color(getConfig().getString("gui.title", "&8Confirm purchase")));
holder.inventory = inventory;
setButton(inventory, 3, "gui.confirm.material", "LIME_WOOL", "gui.confirm.name", "&aConfirm");
setButton(inventory, 5, "gui.cancel.material", "RED_WOOL", "gui.cancel.name", "&cCancel");
pendingPurchases.put(player.getUniqueId(), new PendingPurchase(transactionId, playerCommand, inventory));
player.openInventory(inventory);
sendMessage(player, "messages.opened");
}
private void setButton(Inventory inventory, int slot, String materialPath, String defaultMaterial,
String namePath, String defaultName) {
ItemStack item = XMaterial.matchXMaterial(getConfig().getString(materialPath, defaultMaterial))
.map(XMaterial::parseItem)
.orElse(null);
if (item == null) {
return;
}
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(color(getConfig().getString(namePath, defaultName)));
item.setItemMeta(meta);
}
inventory.setItem(slot, item);
}
private void sendMessage(CommandSender sender, String path) {
String message = getConfig().getString(path, "");
if (!message.isEmpty()) {
sender.sendMessage(color(message));
}
}
private String color(String message) {
return ChatColor.translateAlternateColorCodes('&', message);
}
private String joinArgs(String[] args, int start) {
StringBuilder result = new StringBuilder();
for (int i = start; i < args.length; i++) {
if (result.length() > 0) {
result.append(' ');
}
result.append(args[i]);
}
return result.toString();
}
private static final class ConfirmationHolder implements InventoryHolder {
private Inventory inventory;
@Override
public Inventory getInventory() {
return inventory;
}
}
private static final class PendingPurchase {
private final String transactionId;
private final String playerCommand;
private final Inventory inventory;
private PendingPurchase(String transactionId, String playerCommand, Inventory inventory) {
this.transactionId = transactionId;
this.playerCommand = playerCommand;
this.inventory = inventory;
}
}
}