Integrate Questioner into Logblock
You do not need the extra plugin any more. If you want no questions asked, just disable it in the config.
This commit is contained in:
1 parent
fd450aee80
commit
cd2dbc0813
6 files changed
+161
-16
No files matched your search
Binary file not shown.
@@ -45,13 +45,6 @@
|
||||
<version>1.13-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>questioner</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/LogBlockQuestioner.jar</systemPath>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sk89q.worldedit</groupId>
|
||||
<artifactId>worldedit-core</artifactId>
|
||||
|
||||
@@ -5,7 +5,6 @@ import de.diddiz.LogBlock.QueryParams.Order;
|
||||
import de.diddiz.LogBlock.QueryParams.SummarizationMode;
|
||||
import de.diddiz.LogBlock.config.Config;
|
||||
import de.diddiz.LogBlock.config.WorldConfig;
|
||||
import de.diddiz.LogBlockQuestioner.LogBlockQuestioner;
|
||||
import de.diddiz.util.Utils;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
@@ -40,12 +39,10 @@ import static de.diddiz.util.Utils.listing;
|
||||
public class CommandsHandler implements CommandExecutor {
|
||||
private final LogBlock logblock;
|
||||
private final BukkitScheduler scheduler;
|
||||
private final LogBlockQuestioner questioner;
|
||||
|
||||
CommandsHandler(LogBlock logblock) {
|
||||
this.logblock = logblock;
|
||||
scheduler = logblock.getServer().getScheduler();
|
||||
questioner = (LogBlockQuestioner) logblock.getServer().getPluginManager().getPlugin("LogBlockQuestioner");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -691,16 +688,16 @@ public class CommandsHandler implements CommandExecutor {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!params.silent && askRollbacks && questioner != null && sender instanceof Player && !questioner.ask((Player) sender, "Are you sure you want to continue?", "yes", "no").equals("yes")) {
|
||||
if (!params.silent && askRollbacks && sender instanceof Player && !logblock.getQuestioner().ask((Player) sender, "Are you sure you want to continue?", "yes", "no").equals("yes")) {
|
||||
sender.sendMessage(ChatColor.RED + "Rollback aborted");
|
||||
return;
|
||||
}
|
||||
editor.start();
|
||||
getSession(sender).lookupCache = editor.errors;
|
||||
sender.sendMessage(ChatColor.GREEN + "Rollback finished successfully (" + editor.getElapsedTime() + " ms, " + editor.getSuccesses() + "/" + changes + " blocks" + (editor.getErrors() > 0 ? ", " + ChatColor.RED + editor.getErrors() + " errors" + ChatColor.GREEN : "") + (editor.getBlacklistCollisions() > 0 ? ", " + editor.getBlacklistCollisions() + " blacklist collisions" : "") + ")");
|
||||
if (!params.silent && askClearLogAfterRollback && logblock.hasPermission(sender, "logblock.clearlog") && questioner != null && sender instanceof Player) {
|
||||
if (!params.silent && askClearLogAfterRollback && logblock.hasPermission(sender, "logblock.clearlog") && sender instanceof Player) {
|
||||
Thread.sleep(1000);
|
||||
if (questioner.ask((Player) sender, "Do you want to delete the rollbacked log?", "yes", "no").equals("yes")) {
|
||||
if (logblock.getQuestioner().ask((Player) sender, "Do you want to delete the rollbacked log?", "yes", "no").equals("yes")) {
|
||||
params.silent = true;
|
||||
new CommandClearLog(sender, params, false);
|
||||
} else {
|
||||
@@ -774,7 +771,7 @@ public class CommandsHandler implements CommandExecutor {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!params.silent && askRedos && questioner != null && sender instanceof Player && !questioner.ask((Player) sender, "Are you sure you want to continue?", "yes", "no").equals("yes")) {
|
||||
if (!params.silent && askRedos && sender instanceof Player && !logblock.getQuestioner().ask((Player) sender, "Are you sure you want to continue?", "yes", "no").equals("yes")) {
|
||||
sender.sendMessage(ChatColor.RED + "Redo aborted");
|
||||
return;
|
||||
}
|
||||
@@ -817,10 +814,10 @@ public class CommandsHandler implements CommandExecutor {
|
||||
rs = state.executeQuery("SELECT count(*) FROM `" + table + "-blocks` " + join + params.getWhere());
|
||||
rs.next();
|
||||
if ((deleted = rs.getInt(1)) > 0) {
|
||||
if (!params.silent && askClearLogs && sender instanceof Player && questioner != null) {
|
||||
if (!params.silent && askClearLogs && sender instanceof Player) {
|
||||
sender.sendMessage(ChatColor.DARK_AQUA + "Searching " + params.getTitle() + ":");
|
||||
sender.sendMessage(ChatColor.GREEN.toString() + deleted + " blocks found.");
|
||||
if (!questioner.ask((Player) sender, "Are you sure you want to continue?", "yes", "no").equals("yes")) {
|
||||
if (!logblock.getQuestioner().ask((Player) sender, "Are you sure you want to continue?", "yes", "no").equals("yes")) {
|
||||
sender.sendMessage(ChatColor.RED + "ClearLog aborted");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.diddiz.LogBlock;
|
||||
|
||||
import de.diddiz.LogBlock.config.Config;
|
||||
import de.diddiz.LogBlock.listeners.*;
|
||||
import de.diddiz.LogBlock.questioner.Questioner;
|
||||
import de.diddiz.util.BukkitUtils;
|
||||
import de.diddiz.util.MySQLConnectionPool;
|
||||
import de.diddiz.worldedit.WorldEditLoggingHook;
|
||||
@@ -38,6 +39,7 @@ public class LogBlock extends JavaPlugin {
|
||||
private Timer timer = null;
|
||||
private boolean errorAtLoading = false, noDb = false, connected = true;
|
||||
private PlayerInfoLogging playerInfoLogging;
|
||||
private Questioner questioner;
|
||||
|
||||
public static LogBlock getInstance() {
|
||||
return logblock;
|
||||
@@ -137,6 +139,7 @@ public class LogBlock extends JavaPlugin {
|
||||
pm.addPermission(perm);
|
||||
}
|
||||
}
|
||||
questioner = new Questioner(this);
|
||||
try {
|
||||
Metrics metrics = new Metrics(this);
|
||||
metrics.start();
|
||||
@@ -343,4 +346,8 @@ public class LogBlock extends JavaPlugin {
|
||||
public File getFile() {
|
||||
return super.getFile();
|
||||
}
|
||||
|
||||
public Questioner getQuestioner() {
|
||||
return questioner;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package de.diddiz.LogBlock.questioner;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Question {
|
||||
private String answer;
|
||||
private final String[] answers;
|
||||
private final String questionMessage;
|
||||
private final Player respondent;
|
||||
private final long start;
|
||||
|
||||
public Question(Player respondent, String questionMessage, String[] answers) {
|
||||
this.start = System.currentTimeMillis();
|
||||
this.respondent = respondent;
|
||||
this.questionMessage = questionMessage;
|
||||
this.answers = answers;
|
||||
}
|
||||
|
||||
public synchronized String ask() {
|
||||
StringBuilder options = new StringBuilder();
|
||||
for (String ans : this.answers) {
|
||||
options.append("/" + ans + ", ");
|
||||
}
|
||||
options.delete(options.length() - 2, options.length());
|
||||
this.respondent.sendMessage(this.questionMessage);
|
||||
this.respondent.sendMessage("- " + options + "?");
|
||||
while (answer == null) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException ex) {
|
||||
if (answer == null) {
|
||||
answer = "interrupted";
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.answer;
|
||||
}
|
||||
|
||||
public synchronized boolean isExpired(boolean forceExpire) {
|
||||
if (forceExpire || System.currentTimeMillis() - this.start > 120000L || this.answer != null) {
|
||||
if (answer == null) {
|
||||
answer = "timed out";
|
||||
}
|
||||
notifyAll();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean returnAnswer(String answer) {
|
||||
return returnAnswer(answer, false);
|
||||
}
|
||||
|
||||
public synchronized boolean returnAnswer(String answer, boolean forceReturn) {
|
||||
if (forceReturn) {
|
||||
if (this.answer == null) {
|
||||
this.answer = answer;
|
||||
}
|
||||
notifyAll();
|
||||
return true;
|
||||
}
|
||||
for (String s : answers) {
|
||||
if (s.equalsIgnoreCase(answer)) {
|
||||
if (this.answer == null) {
|
||||
this.answer = s;
|
||||
}
|
||||
notifyAll();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package de.diddiz.LogBlock.questioner;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
|
||||
import de.diddiz.LogBlock.LogBlock;
|
||||
|
||||
public class Questioner {
|
||||
private final LogBlock logBlock;
|
||||
private final ConcurrentHashMap<UUID, Question> questions = new ConcurrentHashMap<UUID, Question>();
|
||||
|
||||
public Questioner(LogBlock logBlock) {
|
||||
this.logBlock = logBlock;
|
||||
logBlock.getServer().getPluginManager().registerEvents(new QuestionerListener(), logBlock);
|
||||
logBlock.getServer().getScheduler().scheduleSyncRepeatingTask(logBlock, new QuestionsReaper(), 600, 600);
|
||||
}
|
||||
|
||||
public String ask(Player respondent, String questionMessage, String... answers) {
|
||||
if (Bukkit.isPrimaryThread()) {
|
||||
throw new IllegalStateException("This method may not be called from the primary thread");
|
||||
}
|
||||
Question question = new Question(respondent, questionMessage, answers);
|
||||
Question oldQuestion = this.questions.put(respondent.getUniqueId(), question);
|
||||
if (oldQuestion != null) {
|
||||
oldQuestion.returnAnswer("no", true);
|
||||
// wait a little time to let the other thread continue
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
return question.ask();
|
||||
}
|
||||
|
||||
private class QuestionsReaper implements Runnable {
|
||||
public void run() {
|
||||
if (questions.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Iterator<Entry<UUID, Question>> it = questions.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Entry<UUID, Question> e = it.next();
|
||||
Question question = e.getValue();
|
||||
if (question.isExpired(logBlock.getServer().getPlayer(e.getKey()) == null)) {
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class QuestionerListener implements Listener {
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
|
||||
UUID player = event.getPlayer().getUniqueId();
|
||||
Question question;
|
||||
question = questions.get(player);
|
||||
if (question != null) {
|
||||
String answer = event.getMessage().substring(1).toLowerCase();
|
||||
if (question.returnAnswer(answer)) {
|
||||
questions.remove(player, question);
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user