Ajout de la fonctionnalité de messages automatiques (remplace ZavAutoMessager)
This commit is contained in:
parent
af3a81ee43
commit
6b01e8e28d
@ -84,6 +84,15 @@ public class ConfigManager {
|
||||
|
||||
UsersCleaner_users_cleaner_group = configFile.getString("cleaner.users_cleaner_group");
|
||||
|
||||
// TODO ajouter les valeurs par défaut dans config.yml
|
||||
Automessage_interval_players = configFile.getInt("autoMessage.interval_player", 10*60);
|
||||
Automessage_interval_default = configFile.getInt("autoMessage.interval_default", 2*60+30);
|
||||
|
||||
|
||||
// TODO ajouter les valeurs par défaut dans config.yml
|
||||
// TODO gérer les codes de coloration;
|
||||
ServerMessages_prefix = ChatColor.YELLOW+"["+ChatColor.WHITE+"Pandacraft"+ChatColor.YELLOW+"] "; //configFile.getString("server_messages_prefix");
|
||||
|
||||
|
||||
initChatAnalysisBadWord();
|
||||
initCommandAlias();
|
||||
@ -123,6 +132,14 @@ public class ConfigManager {
|
||||
* Automessages
|
||||
*/
|
||||
public List<Map<String, String>> Automessages_messages;
|
||||
/**
|
||||
* En seconde : pour les joueurs != visiteur
|
||||
*/
|
||||
public int Automessage_interval_players;
|
||||
/**
|
||||
* En seconde : pour les visiteurs
|
||||
*/
|
||||
public int Automessage_interval_default;
|
||||
|
||||
private void initAutomessages() {
|
||||
Automessages_messages = new ArrayList<Map<String,String>>();
|
||||
@ -130,6 +147,12 @@ public class ConfigManager {
|
||||
String default_color = ChatColor.YELLOW.toString();
|
||||
|
||||
Map<String, String> message;
|
||||
|
||||
message = new HashMap<String, String>();
|
||||
message.put("message", ChatColor.BLUE+"Bienvenue sur Pandacraft ! :) Pour pouvoir jouer, n'oubliez pas de vous inscrire en cliquant sur le lien en dessous%n &ahttp://mc-pandacraft.net/?p=inscription");
|
||||
message.put("permissions", "pandacraft.grade.default");
|
||||
Automessages_messages.add(message);
|
||||
|
||||
message = new HashMap<String, String>();
|
||||
message.put("message", "WorldEdit disponible en &bCréatif"+default_color+" avec le grade &f[&eUltimate&f]"+default_color+". Profitez en vite :D%n&f &a http://mc-pandacraft.net/?p=ultimate");
|
||||
message.put("permissions", "*");
|
||||
@ -139,12 +162,6 @@ public class ConfigManager {
|
||||
message.put("message", "Launcher disponible pour Pandacraft : Optifine intégré et mise à jour automatique avec le serveur.%n &ahttp://mc-pandacraft.net/?p=launcher");
|
||||
message.put("permissions", "*");
|
||||
Automessages_messages.add(message);
|
||||
|
||||
message = new HashMap<String, String>();
|
||||
message.put("message", ChatColor.BLUE+"Bienvenue sur Pandacraft ! :) Pour pouvoir jouer, n'oubliez pas de vous inscrire en cliquant sur le lien en dessous%n &ahttp://mc-pandacraft.net/?p=inscription");
|
||||
message.put("permissions", "pandacraft.grade.default");
|
||||
Automessages_messages.add(message);
|
||||
|
||||
/*
|
||||
* Fonctionnement :
|
||||
* Chaque joueur a une variable indiquand la date/heure du dernier message affiché
|
||||
@ -321,7 +338,7 @@ public class ConfigManager {
|
||||
|
||||
public List<String> CuboCommand_worlds = new ArrayList<String>();
|
||||
|
||||
|
||||
public String ServerMessages_prefix;
|
||||
|
||||
|
||||
|
||||
|
@ -2,6 +2,7 @@ package net.mc_pandacraft.java.plugin.pandacraftutils;
|
||||
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.commands.CommandsManager;
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.modules.AfkManager;
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.modules.AutoMessagesManager;
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.modules.CalculatorManager;
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.modules.ChatAnalysisManager;
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.modules.CommandAliasManager;
|
||||
@ -43,6 +44,7 @@ public class PandacraftUtils extends JavaPlugin {
|
||||
public SurvivalCuboManager survivalCuboManager;
|
||||
public StaffQueueManager staffQueueManager;
|
||||
public TPSAnalysisManager tpsAnalysisManager;
|
||||
public AutoMessagesManager autoMessagesManager;
|
||||
|
||||
|
||||
|
||||
@ -82,7 +84,7 @@ public class PandacraftUtils extends JavaPlugin {
|
||||
serverPingListener = new PacketOutServerInfoListener(this);
|
||||
staffQueueManager = new StaffQueueManager();
|
||||
tpsAnalysisManager = new TPSAnalysisManager();
|
||||
|
||||
autoMessagesManager = new AutoMessagesManager();
|
||||
|
||||
|
||||
|
||||
@ -111,6 +113,7 @@ public class PandacraftUtils extends JavaPlugin {
|
||||
serverPingListener = null;
|
||||
staffQueueManager = null;
|
||||
tpsAnalysisManager = null;
|
||||
autoMessagesManager = null;
|
||||
|
||||
|
||||
ConfigManager.reloadConfig();
|
||||
@ -153,7 +156,7 @@ public class PandacraftUtils extends JavaPlugin {
|
||||
throw new IllegalArgumentException("Un message doit être défini");
|
||||
|
||||
if (prefix)
|
||||
message = ChatColor.YELLOW+"["+ChatColor.WHITE+"Pandacraft"+ChatColor.YELLOW+"] " + message;
|
||||
message = ConfigManager.getInstance().ServerMessages_prefix + message;
|
||||
|
||||
|
||||
for (Player p : getServer().getOnlinePlayers())
|
||||
|
@ -0,0 +1,125 @@
|
||||
package net.mc_pandacraft.java.plugin.pandacraftutils.modules;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.ConfigManager;
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils;
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer;
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayerManager;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class AutoMessagesManager extends BukkitRunnable {
|
||||
private PandacraftUtils plugin = PandacraftUtils.getInstance();
|
||||
|
||||
|
||||
|
||||
public AutoMessagesManager()
|
||||
{
|
||||
plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, this, 0L, 20L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
List<AutoMessage> messages = getMessagesFromConfig();
|
||||
int interval_default = ConfigManager.getInstance().Automessage_interval_default,
|
||||
interval_players = ConfigManager.getInstance().Automessage_interval_players;
|
||||
// message
|
||||
// permissions
|
||||
|
||||
Collection<OnlinePlayer> oPlayers = OnlinePlayerManager.getInstance().getAll();
|
||||
|
||||
|
||||
for (OnlinePlayer op : oPlayers) {
|
||||
|
||||
int interval_sec = op.getPlayer().hasPermission("pandacraft.grade.default") ? interval_default : interval_players;
|
||||
|
||||
op.firstAutomessageCkeck(interval_sec);
|
||||
|
||||
// on vérifie si il est temps, ou non, d'afficher un message aux joueurs
|
||||
if (op.getAutomessage_last_message_time() + (interval_sec*1000) > System.currentTimeMillis())
|
||||
continue;
|
||||
|
||||
// ici, il se peut que id == -1
|
||||
int id = op.getAutomessage_last_message_id();
|
||||
boolean atLeastOnePermitted = true;
|
||||
int c = 0;
|
||||
do {
|
||||
id++;
|
||||
c++;
|
||||
if (id >= messages.size()) id = 0;
|
||||
if (c > messages.size()) { // il parcouru au moins tout le tableau, sans trouver de message qu'il peut afficher
|
||||
atLeastOnePermitted = false;
|
||||
break;
|
||||
}
|
||||
} while(!messages.get(id).hasPlayerPermission(op.getPlayer()));
|
||||
|
||||
if (atLeastOnePermitted) {
|
||||
op.setAutomessage_last_message_id(id);
|
||||
op.updateAutomessage_last_message_time();
|
||||
|
||||
op.sendMessageFromServer(messages.get(id).getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public List<AutoMessage> getMessagesFromConfig() {
|
||||
ArrayList<AutoMessage> ret = new ArrayList<AutoMessage>();
|
||||
|
||||
for (Map<String, String> message : ConfigManager.getInstance().Automessages_messages)
|
||||
ret.add(new AutoMessage(message.get("message"), message.get("permissions")));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public class AutoMessage {
|
||||
private String message;
|
||||
private List<String> permissions;
|
||||
|
||||
public AutoMessage(String m, String perm) {
|
||||
setMessage(m);
|
||||
if (perm == null || perm.equals("*"))
|
||||
permissions = null;
|
||||
else
|
||||
permissions = Arrays.asList(perm.split(";"));
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
if (message == null) throw new IllegalArgumentException("message ne doit pas être null");
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public boolean hasPlayerPermission(Player p) {
|
||||
if (p == null) throw new IllegalArgumentException("le joueur ne doit pas être null");
|
||||
if (permissions == null)
|
||||
return true;
|
||||
|
||||
for (String pe : permissions) {
|
||||
if (p.hasPermission(pe)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -2,6 +2,7 @@ package net.mc_pandacraft.java.plugin.pandacraftutils.players;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.ConfigManager;
|
||||
@ -160,4 +161,69 @@ public class OnlinePlayer {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* AutomessagesData
|
||||
*/
|
||||
private boolean automessage_first_since_online = false;
|
||||
private long automessage_last_message_time = System.currentTimeMillis();
|
||||
private int automessage_last_message_id = -1;
|
||||
|
||||
public boolean firstAutomessageCkeck(int aleaMaxDuration) {
|
||||
if (automessage_first_since_online) return true;
|
||||
automessage_first_since_online = true;
|
||||
automessage_last_message_time = System.currentTimeMillis() - (new Random()).nextInt(aleaMaxDuration)*1000;
|
||||
return false;
|
||||
}
|
||||
public void updateAutomessage_last_message_time() { automessage_last_message_time = System.currentTimeMillis(); }
|
||||
public long getAutomessage_last_message_time() { return automessage_last_message_time; }
|
||||
|
||||
public int getAutomessage_last_message_id() { return automessage_last_message_id; }
|
||||
|
||||
public void setAutomessage_last_message_id(int id) { automessage_last_message_id = id; }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Utils
|
||||
*/
|
||||
|
||||
/**
|
||||
* Envoi un message au joueur avec un prefixe.
|
||||
* Fait appel à <code>player.sendMessage()</code>
|
||||
* @param message le message à envoyer au joueur
|
||||
*/
|
||||
public void sendMessageFromServer(String message) {
|
||||
player.sendMessage(ConfigManager.getInstance().ServerMessages_prefix + message);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user