Skip to content

Instantly share code, notes, and snippets.

@hamza-cskn
Created January 22, 2023 10:31
Show Gist options
  • Save hamza-cskn/539d271590697d5375ec5cfa257543c7 to your computer and use it in GitHub Desktop.
Save hamza-cskn/539d271590697d5375ec5cfa257543c7 to your computer and use it in GitHub Desktop.
Provides taking text input from players using chat. Extremely lightweight.
public final class ChatEntry implements Listener {
private static final Map<UUID, ChatEntry> entryMap = new HashMap<>();
public Consumer<AsyncPlayerChatEvent> action;
public ChatEntry(UUID uuid) {
entryMap.put(uuid, this);
Bukkit.getPluginManager().registerEvents(this, SeniorRegions.getInstance());
}
public ChatEntry(Player player) {
this(player.getUniqueId());
}
@EventHandler(ignoreCancelled = true)
public void handleChatEvent(AsyncPlayerChatEvent e) {
final Player sender = e.getPlayer();
final ChatEntry chatEntry = entryMap.get(sender.getUniqueId());
if (chatEntry == null || chatEntry.getAction() == null) return;
e.setCancelled(true);
if (!e.getMessage().equalsIgnoreCase("cancel"))
Bukkit.getScheduler().runTask(SeniorRegions.getInstance(), () -> chatEntry.getAction().accept(e));
unregisterEntryTask(sender.getUniqueId());
}
public static void unregisterEntryTask(UUID senderUniqueId) {
HandlerList.unregisterAll(entryMap.get(senderUniqueId));
entryMap.remove(senderUniqueId);
}
public void onResponse(Consumer<AsyncPlayerChatEvent> e) {
this.action = e;
}
public Consumer<AsyncPlayerChatEvent> getAction() {
return action;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment