Skip to content

Instantly share code, notes, and snippets.

@Skyost
Last active June 10, 2017 12:39
Show Gist options
  • Save Skyost/4ce6a883a3b86ba0b12ba0169c7bb5eb to your computer and use it in GitHub Desktop.
Save Skyost/4ce6a883a3b86ba0b12ba0169c7bb5eb to your computer and use it in GitHub Desktop.
Demande une note pour le serveur aux joueurs lorsqu'ils se connectent.
package fr.skyost.rating;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class AskRating extends JavaPlugin implements Listener {
@Override
public final void onEnable() {
try {
Bukkit.getPluginManager().registerEvents(this, this);
}
catch(final Exception ex) {
ex.printStackTrace();
}
}
@EventHandler
public final void onPlayerJoin(final PlayerJoinEvent event) {
new Thread() {
@Override
public final void run() {
int note = ServerRating.INVALID_RESPONSE;
while(note == ServerRating.INVALID_RESPONSE) {
note = new ServerRating(AskRating.this, event.getPlayer()).askRating();
}
Bukkit.getConsoleSender().sendMessage("Nouvelle note : " + note + "/5");
}
}.start();
}
}
package fr.skyost.rating;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.Plugin;
public class ServerRating implements Listener {
public static final int INVALID_RESPONSE = -1;
public static final int OTHER_ERROR = -2;
private final Plugin plugin;
private final Player player;
private ValueCallable currentCallable;
public ServerRating(final Plugin plugin, final Player player) {
this.plugin = plugin;
this.player = player;
}
public int askRating() {
player.sendMessage(ChatColor.GOLD + "Veuillez noter notre serveur (sur 5) :");
try {
currentCallable = new ValueCallable();
Bukkit.getPluginManager().registerEvents(this, plugin);
final ValueCallableObject object = Executors.newFixedThreadPool(1).submit(currentCallable).get();
if(object.exception != null) {
throw new Exception(object.message, object.exception);
}
final int note = Integer.parseInt(object.message);
if(note > 5) {
throw new NumberFormatException();
}
AsyncPlayerChatEvent.getHandlerList().unregister(this);
currentCallable = null;
player.sendMessage(ChatColor.GREEN + "Merci !");
return note;
}
catch(final NumberFormatException ex) {
player.sendMessage(ChatColor.RED + "Nombre invalide !");
return INVALID_RESPONSE;
}
catch(final Exception ex) {
ex.printStackTrace();
player.sendMessage(ChatColor.RED + "Une erreur est survenue pendant l'envoi de votre note : " + ex.getClass().getName());
return OTHER_ERROR;
}
}
@EventHandler(priority = EventPriority.LOWEST)
public final void onAsyncPlayerChat(final AsyncPlayerChatEvent event) {
currentCallable.setMessage(event.getMessage());
event.setCancelled(true);
}
public class ValueCallable implements Callable<ValueCallableObject> {
private String message;
@Override
public final ValueCallableObject call() {
synchronized(this) {
try {
this.wait();
}
catch(final Exception ex) {
return new ValueCallableObject(ex, "Une erreur est survenue pendant l'attente de la réponse.");
}
return new ValueCallableObject(null, message);
}
}
public final String getMessage() {
return message;
}
public final void setMessage(final String message) {
synchronized(this) {
this.message = message;
this.notify();
}
}
}
public class ValueCallableObject {
public final Throwable exception;
private final String message;
public ValueCallableObject(final Throwable exception, final String message) {
this.exception = exception;
this.message = message;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment