public class Foo implements Listener {
private static Map<Player, Predicate<AsyncPlayerChatEvent>> WAITING_FOR_INPUT = new HashMap<>();
@EventHandler
public void onInventoryClick(InventoryClickEvent e) {
[...]
else if(e.getClick() == ClickType.MIDDLE) {
ItemStack target = e.getCurrentItem();
Player player = e.getWhoClicked();
PlayerInventory inventory = player.getInventory();
if(target == null) return;
if(!target.hasItemMeta() || !target.getItemMeta().hasLore() || target.getItemMeta().getLore().size() < 3 || !target.getItemMeta().getLore().get(2).startsWith("§cPrix de vente : §e")) {
player.sendMessage("Cet item n'est pas vendable");
return;
}
if(!waitInput(player, event -> {
int requested;
try {
requested = Integer.parseInt(event.getMessage());
if(requested <= 0) throw new NumberFormatException();
}
catch(NumberFormatException e) {
if(event.getMessage().equalsIgnoreCase("exit"))
return true;
player.sendMessage("Veuillez enter un nombre valide");
return false;
}
Map<Integer, ? extends ItemStack> matches = inventory.all(target.getType());
int remaining = requested;
for(int slot : matches.keySet()) {
ItemStack item = matches.get(slot);
if(!item.isSimilar(target)) continue;
remaining -= item.getAmount();
inventory.clear(slot);
if(remaining < 0) {
item.setAmount(-remaining);
inventory.setItem(slot, item);
remaining = 0;
}
if(remaining == 0)
break;
}
float unitPrice = Float.parseFloat(target.getItemMeta().getLore().get(2).substring(20).replace("€", ""));
int taken = requested - remaining;
main.economy.depositPlayer(player, taken * unitPrice);
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 3, 3);
player.sendMessage("Vous avez vendu " + taken + "*" + target);
return true;
})) {
player.sendMessage("Une transaction est déjà en cours.");
return;
}
player.sendMessage("Veuillez entrer le nombre d'objets à vendre. Tapez 'exit' pour annuler.");
}
[...]
}
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent e) {
Player player = e.getPlayer();
synchronized(WAITING_FOR_INPUT) {
if(!WAITING_FOR_INPUT.containsKey(player)) return;
Predicate<AsyncPlayerChatEvent> handler = WAITING_FOR_INPUT.get(player);
if(handler.test(e))
WAITING_FOR_INPUT.remove(player);
}
}
public static boolean waitInput(Player player, Predicate<AsyncPlayerChatEvent> handler) {
if(WAITING_FOR_INPUT.containsKey(player)) return false;
WAITING_FOR_INPUT.put(player, handler);
return true;
}
}