2015-01-24 07:09:43 +01:00
|
|
|
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;
|
|
|
|
|
2015-01-24 09:46:25 +01:00
|
|
|
import org.bukkit.ChatColor;
|
2015-01-24 07:09:43 +01:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
|
|
|
|
|
|
public class AutoMessagesManager extends BukkitRunnable {
|
|
|
|
private PandacraftUtils plugin = PandacraftUtils.getInstance();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public AutoMessagesManager()
|
|
|
|
{
|
2015-01-25 05:11:06 +01:00
|
|
|
plugin.getServer().getScheduler().runTaskTimer(plugin, this, 0L, 20L);
|
2015-01-24 07:09:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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;
|
2015-01-25 05:11:06 +01:00
|
|
|
|
|
|
|
// on cherche le prochain message à afficher
|
2015-01-24 07:09:43 +01:00
|
|
|
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)
|
2015-01-24 09:46:25 +01:00
|
|
|
ret.add(new AutoMessage(ChatColor.translateAlternateColorCodes('&', message.get("message").replace("%n", "\n")), message.get("permissions")));
|
2015-01-24 07:09:43 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|