TFM-4.3-Reloaded/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java

339 lines
12 KiB
Java
Raw Normal View History

2011-09-20 02:52:08 +00:00
package me.StevenLawson.TotalFreedomMod;
import java.io.File;
2012-12-06 11:01:51 +00:00
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
2011-10-19 00:37:00 +00:00
import me.StevenLawson.TotalFreedomMod.Commands.TFM_Command;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader;
2013-08-27 01:48:04 +00:00
import me.StevenLawson.TotalFreedomMod.HTTPD.TFM_HTTPD_Manager;
import me.StevenLawson.TotalFreedomMod.Listener.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
2011-09-20 02:52:08 +00:00
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
2012-09-20 03:17:10 +00:00
import org.bukkit.World;
2011-10-19 00:37:00 +00:00
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
2011-10-13 23:07:52 +00:00
import org.bukkit.entity.Player;
2011-09-23 03:22:10 +00:00
import org.bukkit.plugin.PluginManager;
2011-09-20 02:52:08 +00:00
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.mcstats.Metrics;
2011-09-20 02:52:08 +00:00
public class TotalFreedomMod extends JavaPlugin
{
2011-10-10 12:09:19 +00:00
public static final long HEARTBEAT_RATE = 5L; //Seconds
public static final long SERVICE_CHECKER_RATE = 120L;
//
public static final String SUPERADMIN_FILE = "superadmin.yml";
2012-09-18 14:40:48 +00:00
public static final String PERMBAN_FILE = "permban.yml";
public static final String PROTECTED_AREA_FILE = "protectedareas.dat";
public static final String SAVED_FLAGS_FILE = "savedflags.dat";
//
public static final String COMMAND_PATH = "me.StevenLawson.TotalFreedomMod.Commands";
public static final String COMMAND_PREFIX = "Command_";
//
public static final String MSG_NO_PERMS = ChatColor.YELLOW + "You do not have permission to use this command.";
public static final String YOU_ARE_OP = ChatColor.YELLOW + "You are now op!";
public static final String YOU_ARE_NOT_OP = ChatColor.YELLOW + "You are no longer op!";
public static final String CAKE_LYRICS = "But there's no sense crying over every mistake. You just keep on trying till you run out of cake.";
2011-10-16 06:00:37 +00:00
public static final String NOT_FROM_CONSOLE = "This command may not be used from the console.";
//
public static final Server server = Bukkit.getServer();
public static TotalFreedomMod plugin = null;
//
public static String pluginName = "";
public static String pluginVersion = "";
public static String buildNumber = "";
public static String buildDate = "";
//
public static boolean allPlayersFrozen = false;
public static BukkitTask freezePurgeTask = null;
public static BukkitTask mutePurgeTask = null;
2013-08-12 18:47:36 +00:00
public static boolean lockdownEnabled = false;
2011-11-28 22:44:51 +00:00
public static Map<Player, Double> fuckoffEnabledFor = new HashMap<Player, Double>();
//
public static List<String> permbanned_players = new ArrayList<String>();
public static List<String> permbanned_ips = new ArrayList<String>();
@Override
2013-08-21 21:42:21 +00:00
public void onLoad()
{
TotalFreedomMod.plugin = this;
TotalFreedomMod.pluginName = plugin.getDescription().getName();
2013-11-30 17:04:43 +00:00
TotalFreedomMod.pluginVersion = plugin.getDescription().getVersion();
2013-08-21 00:04:06 +00:00
2013-11-30 17:04:43 +00:00
TFM_Log.setPluginLogger(plugin.getLogger());
TFM_Log.setServerLogger(server.getLogger());
2013-08-21 21:42:21 +00:00
setAppProperties();
2013-08-21 21:42:21 +00:00
}
2013-08-21 00:04:06 +00:00
2013-08-21 21:42:21 +00:00
@Override
public void onEnable()
{
TFM_Log.info("Version: " + TotalFreedomMod.pluginVersion + "." + TotalFreedomMod.buildNumber + " by Madgeek1450 and DarthSalamon");
loadSuperadminConfig();
2012-09-18 14:40:48 +00:00
loadPermbanConfig();
TFM_UserList.getInstance(plugin);
registerEventHandlers();
2013-08-22 00:07:14 +00:00
try
{
TFM_Flatlands.getInstance().getWorld();
}
catch (Exception ex)
{
}
2012-09-20 03:17:10 +00:00
2013-08-22 00:07:14 +00:00
try
{
TFM_AdminWorld.getInstance().getWorld();
}
catch (Exception ex)
{
}
2013-08-12 16:38:46 +00:00
2013-08-18 19:27:17 +00:00
if (TFM_ConfigEntry.DISABLE_WEATHER.getBoolean())
2012-09-20 03:17:10 +00:00
{
for (World world : server.getWorlds())
{
world.setThundering(false);
world.setStorm(false);
world.setThunderDuration(0);
world.setWeatherDuration(0);
2012-09-20 03:17:10 +00:00
}
}
// Initialize game rules
2013-08-18 19:27:17 +00:00
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.DO_DAYLIGHT_CYCLE, !TFM_ConfigEntry.DISABLE_NIGHT.getBoolean(), false);
2013-08-18 18:52:46 +00:00
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.DO_FIRE_TICK, TFM_ConfigEntry.ALLOW_FIRE_SPREAD.getBoolean(), false);
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.DO_MOB_LOOT, false, false);
2013-08-18 19:27:17 +00:00
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.DO_MOB_SPAWNING, !TFM_ConfigEntry.MOB_LIMITER_ENABLED.getBoolean(), false);
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.DO_TILE_DROPS, false, false);
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.MOB_GRIEFING, false, false);
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.NATURAL_REGENERATION, true, false);
TFM_GameRuleHandler.commitGameRules();
2013-08-18 19:27:17 +00:00
if (TFM_ConfigEntry.PROTECTED_AREAS_ENABLED.getBoolean())
{
TFM_ProtectedArea.loadProtectedAreas();
TFM_ProtectedArea.autoAddSpawnpoints();
}
TFM_Util.deleteFolder(new File("./_deleteme"));
File[] coreDumps = new File(".").listFiles(new java.io.FileFilter()
{
@Override
public boolean accept(File file)
{
return file.getName().startsWith("java.core");
}
});
for (File dump : coreDumps)
{
TFM_Log.info("Removing core dump file: " + dump.getName());
dump.delete();
}
// Heartbeat
new TFM_Heartbeat(plugin).runTaskTimer(plugin, HEARTBEAT_RATE * 20L, HEARTBEAT_RATE * 20L);
2012-12-06 11:01:51 +00:00
// metrics @ http://mcstats.org/plugin/TotalFreedomMod
try
{
2012-12-06 11:01:51 +00:00
Metrics metrics = new Metrics(plugin);
metrics.start();
}
catch (IOException ex)
{
TFM_Log.warning("Failed to submit metrics data: " + ex.getMessage());
2012-12-06 11:01:51 +00:00
}
TFM_ServiceChecker.getInstance().getUpdateRunnable().runTaskTimerAsynchronously(plugin, 40L, SERVICE_CHECKER_RATE * 20L);
2013-09-05 14:48:57 +00:00
TFM_HTTPD_Manager.getInstance().start();
TFM_FrontDoor.getInstance().start();
2013-11-30 17:04:43 +00:00
TFM_Log.info("Version " + pluginVersion + " enabled");
2013-09-05 14:48:57 +00:00
// Delayed Start :
new BukkitRunnable()
{
@Override
public void run()
{
TFM_CommandLoader.getInstance().scan();
TFM_CommandBlocker.getInstance().parseBlockingRules();
}
}.runTaskLater(plugin, 20L);
}
@Override
public void onDisable()
{
server.getScheduler().cancelTasks(plugin);
2013-08-21 00:04:06 +00:00
2013-08-27 01:48:04 +00:00
TFM_HTTPD_Manager.getInstance().stop();
2013-08-27 00:39:30 +00:00
2013-11-30 17:04:43 +00:00
TFM_Log.info("Plugin disabled");
2011-10-12 22:45:43 +00:00
}
2011-10-19 00:37:00 +00:00
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
{
try
{
Player sender_p = null;
boolean senderIsConsole = false;
if (sender instanceof Player)
{
sender_p = (Player) sender;
TFM_Log.info(String.format("[PLAYER_COMMAND] %s(%s): /%s %s",
2011-10-19 00:37:00 +00:00
sender_p.getName(),
ChatColor.stripColor(sender_p.getDisplayName()),
commandLabel,
StringUtils.join(args, " ")), true);
2011-10-19 00:37:00 +00:00
}
else
{
senderIsConsole = true;
TFM_Log.info(String.format("[CONSOLE_COMMAND] %s: /%s %s",
2011-10-19 00:37:00 +00:00
sender.getName(),
commandLabel,
StringUtils.join(args, " ")), true);
2011-10-19 00:37:00 +00:00
}
TFM_Command dispatcher;
try
{
ClassLoader classLoader = TotalFreedomMod.class.getClassLoader();
dispatcher = (TFM_Command) classLoader.loadClass(String.format("%s.%s%s", COMMAND_PATH, COMMAND_PREFIX, cmd.getName().toLowerCase())).newInstance();
dispatcher.setup(plugin, sender, dispatcher.getClass());
2011-10-19 00:37:00 +00:00
}
catch (Throwable ex)
2011-10-19 00:37:00 +00:00
{
TFM_Log.severe("Command not loaded: " + cmd.getName() + "\n" + ExceptionUtils.getStackTrace(ex));
2011-10-19 00:37:00 +00:00
sender.sendMessage(ChatColor.RED + "Command Error: Command not loaded: " + cmd.getName());
return true;
}
try
{
if (dispatcher.senderHasPermission())
{
return dispatcher.run(sender, sender_p, cmd, commandLabel, args, senderIsConsole);
}
else
{
sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS);
}
2011-10-19 00:37:00 +00:00
}
catch (Throwable ex)
2011-10-19 00:37:00 +00:00
{
TFM_Log.severe("Command Error: " + commandLabel + "\n" + ExceptionUtils.getStackTrace(ex));
2011-10-19 00:37:00 +00:00
}
2011-10-19 00:37:00 +00:00
}
catch (Throwable ex)
2011-10-19 00:37:00 +00:00
{
TFM_Log.severe("Command Error: " + commandLabel + "\n" + ExceptionUtils.getStackTrace(ex));
2011-10-19 00:37:00 +00:00
sender.sendMessage(ChatColor.RED + "Unknown Command Error.");
}
return true;
}
public static void loadSuperadminConfig()
{
2012-09-20 20:35:00 +00:00
try
2011-10-16 06:00:37 +00:00
{
2012-11-18 03:57:24 +00:00
TFM_SuperadminList.backupSavedList();
2012-11-13 01:42:30 +00:00
TFM_SuperadminList.loadSuperadminList();
2011-10-16 06:00:37 +00:00
}
catch (Exception ex)
2012-09-20 20:35:00 +00:00
{
TFM_Log.severe("Error loading superadmin list: " + ex.getMessage());
2012-09-20 20:35:00 +00:00
}
2012-09-18 14:40:48 +00:00
}
public static void loadPermbanConfig()
2012-09-18 14:40:48 +00:00
{
2012-09-20 20:35:00 +00:00
try
2012-09-18 14:40:48 +00:00
{
2013-09-24 12:05:48 +00:00
TFM_Util.createDefaultConfiguration(PERMBAN_FILE);
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), PERMBAN_FILE));
2012-09-18 14:40:48 +00:00
2012-09-20 20:35:00 +00:00
permbanned_players = new ArrayList<String>();
permbanned_ips = new ArrayList<String>();
for (String user : config.getKeys(false))
2012-09-18 14:40:48 +00:00
{
2012-09-20 20:35:00 +00:00
permbanned_players.add(user.toLowerCase().trim());
List<String> user_ips = config.getStringList(user);
2012-09-20 20:35:00 +00:00
for (String ip : user_ips)
2012-09-18 14:40:48 +00:00
{
2012-09-20 20:35:00 +00:00
ip = ip.toLowerCase().trim();
if (!permbanned_ips.contains(ip))
{
permbanned_ips.add(ip);
}
2012-09-18 14:40:48 +00:00
}
}
}
catch (Exception ex)
2012-09-20 20:35:00 +00:00
{
TFM_Log.severe("Error loading permban list!");
TFM_Log.severe(ex);
2012-09-20 20:35:00 +00:00
}
2011-10-12 18:13:10 +00:00
}
private static void registerEventHandlers()
{
PluginManager pm = server.getPluginManager();
2012-09-16 12:11:33 +00:00
pm.registerEvents(new TFM_EntityListener(), plugin);
pm.registerEvents(new TFM_BlockListener(), plugin);
pm.registerEvents(new TFM_PlayerListener(), plugin);
pm.registerEvents(new TFM_WeatherListener(), plugin);
pm.registerEvents(new TFM_ServerListener(), plugin);
pm.registerEvents(new TFM_CustomListener(), plugin);
}
private static void setAppProperties()
{
try
{
InputStream in = plugin.getResource("appinfo.properties");
Properties props = new Properties();
// in = plugin.getClass().getResourceAsStream("/appinfo.properties");
props.load(in);
in.close();
2013-11-30 17:04:43 +00:00
TotalFreedomMod.buildNumber = props.getProperty("program.buildnumber");
TotalFreedomMod.buildDate = props.getProperty("program.builddate");
}
catch (Exception ex)
{
TFM_Log.severe("Could not load App properties!");
TFM_Log.severe(ex);
2013-11-30 17:04:43 +00:00
TotalFreedomMod.buildNumber = "1";
TotalFreedomMod.buildDate = TFM_Util.dateToString(new Date());
}
}
2011-09-20 02:52:08 +00:00
}