Skip to content

Instantly share code, notes, and snippets.

@noobmobile
Last active January 28, 2022 00:07
Show Gist options
  • Save noobmobile/f1597cceaa6494e189e7f04d6ad353a0 to your computer and use it in GitHub Desktop.
Save noobmobile/f1597cceaa6494e189e7f04d6ad353a0 to your computer and use it in GitHub Desktop.
Classe para perguntar para o jogador e retornar a resposta
import com.dont.minas.Terminal;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.WeakHashMap;
import java.util.function.BiConsumer;
public class ChatAsker {
private final static WeakHashMap<Player, ChatAsker> askers = new WeakHashMap<>();
static {
Terminal main = Terminal.getPlugin(Terminal.class);
Bukkit.getPluginManager().registerEvents(new Listener() {
@EventHandler
public void onChat(AsyncPlayerChatEvent e) {
ChatAsker asker = askers.get(e.getPlayer());
if (asker != null) {
e.setCancelled(true);
BiConsumer<Player, String> consumer = asker.getOnComplete();
String message = e.getMessage();
Player player = e.getPlayer();
askers.remove(player);
consumer.accept(player, message);
}
}
@EventHandler
public void onQuit(PlayerQuitEvent e) {
askers.remove(e.getPlayer());
}
}, main);
}
private String[] messages;
private BiConsumer<Player, String> onComplete;
private ChatAsker(Builder builder) {
messages = builder.messages;
onComplete = builder.onComplete;
}
public void addPlayer(Player player) {
askers.put(player, this);
player.sendMessage(messages);
}
public BiConsumer<Player, String> getOnComplete() {
return onComplete;
}
public String[] getMessages() {
return messages;
}
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private String[] messages;
private BiConsumer<Player, String> onComplete;
private Builder() {
}
public Builder messages(String... messages) {
this.messages = messages;
return this;
}
public Builder onComplete(BiConsumer<Player, String> onComplete) {
this.onComplete = onComplete;
return this;
}
public ChatAsker build() {
return new ChatAsker(this);
}
}
}
private final ChatAsker MULTIPLIER_ASKER = ChatAsker.builder()
.messages("", "§a Digite o multiplicador de compra que você deseja", " §aselecionar.", " §aLembre-se que o seu limite é 64x.", " §aPara cancelar a ação digite \"cancelar\"", "")
.onComplete((player, message) -> {
if (message.equalsIgnoreCase("cancelar")) {
player.sendMessage("§eCancelado com sucesso.");
}
User user = manager.get(player.getName());
int val = -1;
try {
val = Integer.parseInt(message);
if (val <= 0) throw new Exception();
} catch (Exception e) {
player.sendMessage("§cQuantidade inválida.");
return;
}
user.setMultiplier(val);
player.sendMessage("§aMultiplicador de Compra definido com sucesso.");
}).build();
MULTIPLIER_ASKER.addPlayer(player)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment