Skip to content

Commit

Permalink
Fix + optimize QuizManager (#100)
Browse files Browse the repository at this point in the history
- Rends le QuizManager reload proof
- N'update plus le quiz quand aucun joueur n'est connecté
  • Loading branch information
Margouta authored Jul 7, 2024
2 parents 97e0e83 + f991ebc commit 7cebaeb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/main/java/fr/communaywen/core/AywenCraftPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ public BukkitAudiences getAdventure() {

@Override
public void onDisable() {
System.out.println("DISABLE");
this.databaseManager.close();
this.quizManager.close();
}

public Set<UUID> getFrozenPlayers() {
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/fr/communaywen/core/QuizManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.jetbrains.annotations.Debug;

import java.text.MessageFormat;
import java.util.*;
Expand All @@ -11,8 +12,9 @@
import java.util.concurrent.TimeUnit;

public class QuizManager {
public static Quiz currentQuiz;
public ScheduledExecutorService executor;
public Quiz currentQuiz;
private ScheduledExecutorService timeoutExecutor;
private ScheduledExecutorService executor;
private List<Quiz> quizzes;
public FileConfiguration config;
private AywenCraftPlugin plugin;
Expand All @@ -29,7 +31,9 @@ public QuizManager(AywenCraftPlugin plugin, FileConfiguration config) {
};

Runnable runnable = () -> {
this.executor = Executors.newScheduledThreadPool(1);
if (Bukkit.getOnlinePlayers().size() < 1) return;

this.timeoutExecutor = Executors.newScheduledThreadPool(1);

int index = new Random().nextInt(this.quizzes.size());

Expand All @@ -46,11 +50,11 @@ public QuizManager(AywenCraftPlugin plugin, FileConfiguration config) {
currentQuiz = null;
};

this.executor.schedule(tellAnswer, 30, TimeUnit.SECONDS);
this.timeoutExecutor.schedule(tellAnswer, 30, TimeUnit.SECONDS);
};

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(runnable, 5, config.getInt("interval"), TimeUnit.MINUTES);
executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(runnable, 1, config.getInt("interval"), TimeUnit.MINUTES);
}

public void onPlayerChat(AsyncPlayerChatEvent event) {
Expand All @@ -63,11 +67,15 @@ public void onPlayerChat(AsyncPlayerChatEvent event) {
Bukkit.broadcastMessage(message);
this.plugin.economyManager.addBalance(event.getPlayer(), money);
currentQuiz = null;
this.executor.shutdownNow();
this.executor = Executors.newScheduledThreadPool(1);
this.timeoutExecutor.shutdownNow();
this.timeoutExecutor = Executors.newScheduledThreadPool(1);
}
}

public void close() {
executor.shutdownNow();
}

public class Quiz {
public String question;
public String answer;
Expand Down

0 comments on commit 7cebaeb

Please sign in to comment.