DeluxeGroups/src/main/java/xyz/etztech/deluxegroups/DeluxeGroups.java

238 lines
7.6 KiB
Java

package xyz.etztech.deluxegroups;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.dynmap.DynmapCommonAPI;
import xyz.etztech.core.api.IMinecraftManager;
import xyz.etztech.deluxegroups.command.CommandGroup;
import xyz.etztech.deluxegroups.command.CommandMain;
import xyz.etztech.deluxegroups.listeners.AsyncPlayerChatListener;
import xyz.etztech.deluxegroups.listeners.SessionListener;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.logging.Logger;
public class DeluxeGroups extends JavaPlugin {
private static DeluxeGroups instance;
public static FileConfiguration config;
public static FileConfiguration groupConfig;
private Logger log = Logger.getLogger( "Minecraft" );
protected static Map<String, String> inGroup = new HashMap();
protected static Map<String, DeluxeGroup> groups = new HashMap();
// Objects that can be reloaded
AsyncPlayerChatListener chatListener;
// Dynmap API
private static DynmapCommonAPI dynmap = null;
// MinecraftManager API
private static IMinecraftManager minecraftManager = null;
@Override
public void onEnable() {
instance = this;
saveDefaultConfig();
reloadConfig();
try {
File logs = new File(getDataFolder().getAbsolutePath()+"/logs/");
logs.mkdir();
} catch (Exception e) {}
if (isEnabled()) {
// Dynmap integration
if (Bukkit.getPluginManager().isPluginEnabled("dynmap")) {
dynmap = (DynmapCommonAPI) Bukkit.getPluginManager().getPlugin("dynmap");
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "IMPORTANT: Disabling Dynmap Game->Web Chat. All web chat will be handled by DeluxeGroups.");
dynmap.setDisableChatToWebProcessing(true);
getServer().getPluginManager().registerEvents(new SessionListener(), this);
}
// MCM integration
if (Bukkit.getPluginManager().isPluginEnabled("MinecraftManager")) {
minecraftManager = (IMinecraftManager) Bukkit.getPluginManager().getPlugin("MinecraftManager");
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "IMPORTANT: Disabling MinecraftManager global logging. All global logging will be handled by DeluxeGroups.");
minecraftManager.logOverride(true);
}
// Add Commands
CommandMain cmdMain = new CommandMain(this);
this.getCommand("deluxegroups").setExecutor(cmdMain);
CommandGroup cmdGroup = new CommandGroup(this);
this.getCommand("group").setExecutor(cmdGroup);
// Add Listeners
chatListener = new AsyncPlayerChatListener(this);
getServer().getPluginManager().registerEvents(chatListener, this);
}
}
@Override
public void onDisable() {
log("Saving groups.");
String root;
for (DeluxeGroup group : groups.values()) {
root = group.getName().toLowerCase();
groupConfig.createSection(root);
groupConfig.set(root + ".name", group.getName());
groupConfig.set(root + ".password", group.getPassword());
groupConfig.set(root + ".permanent", group.getPermanent());
groupConfig.set(root + ".players", group.getSize() > 0 ? group.getUUIDList() : null);
}
File groupf = new File(getDataFolder(), "groups.yml");
try {
PrintWriter pw = new PrintWriter(groupf);
pw.write("# This is a file for saving all your groups\n" +
"# Do not manually edit this file!\n\n");
pw.close();
} catch (Exception ex) {}
try {
groupf.createNewFile();
groupConfig.save(groupf);
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadConfig() {
config = Bukkit.getPluginManager().getPlugin("DeluxeGroups").getConfig();
}
@Override
public void reloadConfig() {
super.reloadConfig();
loadConfig();
loadGroupConfig();
if (chatListener != null) {
chatListener.reload();
}
}
public void loadGroupConfig() {
File groupf = new File(getDataFolder(), "groups.yml");
if (!groupf.exists()) {
groupf.getParentFile().mkdirs();
saveResource("groups.yml", false);
}
groupConfig = new YamlConfiguration();
try {
groupConfig.load(groupf);
int loaded = 0;
OfflinePlayer offlinePlayer;
DeluxeGroup group;
for (String key : groupConfig.getKeys(false)) {
group = new DeluxeGroup();
group.setName(groupConfig.getString(key + ".name"));
group.setPassword(groupConfig.getString(key + ".password"));
group.setPermanent(groupConfig.getBoolean(key + ".permanent"));
ArrayList<OfflinePlayer> playerList = new ArrayList<>();
for (String uuid : groupConfig.getStringList(key + ".players")) {
offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString(uuid));
playerList.add(offlinePlayer);
setInGroup(uuid, group.getName());
}
group.setGroupList(playerList);
addGroup(group);
loaded++;
}
log("Loaded " + loaded + " groups!");
} catch (Exception e) {
e.printStackTrace();
}
}
public void log(String message) {
log.info( "[DeluxeGroups]: " + message );
}
public static DeluxeGroups getInstance() {
return instance;
}
public static Collection<DeluxeGroup> getGroups() {
return groups.values();
}
public static void addGroup(DeluxeGroup group) {
groups.put(group.getName().toLowerCase(), group);
}
public static void removeGroup(DeluxeGroup group) {
groups.remove(group.getName().toLowerCase());
}
public static DeluxeGroup getGroup(String name) {
name = name.toLowerCase();
if (groups.containsKey(name)) {
return groups.get(name);
}
return null;
}
public static void setInGroup(String uuid, String group) {
inGroup.put(uuid, group.toLowerCase());
}
public static void removeFromInGroup(String uuid) {
inGroup.remove(uuid);
}
public static String getInGroup(String uuid) {
if (inGroup.containsKey(uuid)) {
return inGroup.get(uuid);
}
return null;
}
public static void addToGroup(Player uuid, String group) {
group = group.toLowerCase();
if (groups.containsKey(group)) {
groups.get(group).add(uuid);
}
}
public static void removeFromGroup(Player player, String group) {
group = group.toLowerCase();
if (groups.containsKey(group)) {
groups.get(group).remove(player);
}
}
public static void removeFromGroup(OfflinePlayer player, String group) {
group = group.toLowerCase();
if (groups.containsKey(group)) {
groups.get(group).remove(player.getUniqueId().toString());
}
}
public static DynmapCommonAPI getDynmap() {
return dynmap;
}
public static IMinecraftManager getMinecraftManager() {
return minecraftManager;
}
}