|
| 1 | +package wtf.myles.hcfcore; |
| 2 | + |
| 3 | +import com.mongodb.DB; |
| 4 | +import com.mongodb.MongoClient; |
| 5 | +import com.sk89q.worldguard.bukkit.WGBukkit; |
| 6 | +import com.sk89q.worldguard.protection.ApplicableRegionSet; |
| 7 | +import org.bukkit.Bukkit; |
| 8 | +import org.bukkit.ChatColor; |
| 9 | +import org.bukkit.Location; |
| 10 | +import org.bukkit.World; |
| 11 | +import org.bukkit.configuration.file.YamlConfiguration; |
| 12 | +import org.bukkit.entity.Player; |
| 13 | +import org.bukkit.metadata.FixedMetadataValue; |
| 14 | +import org.bukkit.metadata.MetadataValue; |
| 15 | +import org.bukkit.plugin.java.JavaPlugin; |
| 16 | +import org.bukkit.scheduler.BukkitRunnable; |
| 17 | +import redis.clients.jedis.JedisPool; |
| 18 | +import wtf.myles.hcfcore.commands.*; |
| 19 | +import wtf.myles.hcfcore.deathmessages.DeathMessageHandler; |
| 20 | +import wtf.myles.hcfcore.listeners.*; |
| 21 | +import wtf.myles.hcfcore.handlers.MapHandler; |
| 22 | +import wtf.myles.hcfcore.managers.*; |
| 23 | +import wtf.myles.hcfcore.nms.EntityRegistrar; |
| 24 | +import wtf.myles.hcfcore.objects.Profile; |
| 25 | +import wtf.myles.hcfcore.staff.StaffMode; |
| 26 | +import wtf.myles.hcfcore.utils.SignGUI; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | +import java.util.*; |
| 30 | + |
| 31 | +/** |
| 32 | + * Created by Myles on 18/06/2015. |
| 33 | + */ |
| 34 | +public class Main extends JavaPlugin { |
| 35 | + |
| 36 | + private static Main instance; |
| 37 | + private MapHandler mapHandler; |
| 38 | + private ConfigManager configManager; |
| 39 | + private File file; |
| 40 | + private YamlConfiguration yamlConfiguration; |
| 41 | + private MongoClient mongoClient; |
| 42 | + private DB db; |
| 43 | + private JedisPool jedisPool; |
| 44 | + private HashMap<UUID, Integer> pvpTimerMap = new HashMap(); |
| 45 | + private SignGUI signGUI; |
| 46 | + private File messageFile; |
| 47 | + private ProfileManager profileManager; |
| 48 | + |
| 49 | + public File getMessageFile() { |
| 50 | + return messageFile; |
| 51 | + } |
| 52 | + |
| 53 | + public SignGUI getSignGUI() { |
| 54 | + return signGUI; |
| 55 | + } |
| 56 | + |
| 57 | + public JedisPool getJedisPool() { |
| 58 | + return jedisPool; |
| 59 | + } |
| 60 | + |
| 61 | + public MongoClient getMongoClient() { |
| 62 | + return mongoClient; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void onEnable() { |
| 67 | + instance = this; |
| 68 | + setupHandlers(); |
| 69 | + setupListeners(); |
| 70 | + setupEntityRegistar(); |
| 71 | + setupCommands(); |
| 72 | + |
| 73 | + if(!new File(getDataFolder(), "config.yml").exists()) { |
| 74 | + List<String> blockedPotions = Arrays.asList(new String[] { "373:8233" }); |
| 75 | + getConfig().set("blockedpotions", blockedPotions); |
| 76 | + getConfig().set("server-full-msg", "&cServer is currently full."); |
| 77 | + saveConfig(); |
| 78 | + } |
| 79 | + |
| 80 | + /*if(cfg.getBoolean("mongodb-enabled")) { |
| 81 | + try { |
| 82 | + this.db = new MongoDB(new DBAddress("localhost", 27017, "server")).getDatabase(); |
| 83 | + } catch(UnknownHostException ex) { |
| 84 | + ex.printStackTrace(); |
| 85 | + Bukkit.getLogger().log(Level.SEVERE, "Error connecting to the MongoDB."); |
| 86 | + } |
| 87 | + }*/ |
| 88 | + try { |
| 89 | + File player = new File(getDataFolder(), "users"); |
| 90 | + if(!player.exists()) { |
| 91 | + player.mkdir(); |
| 92 | + } |
| 93 | + } catch (Exception e) { |
| 94 | + e.printStackTrace(); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + loadConfig(); |
| 100 | + configManager = new ConfigManager(yamlConfiguration, file); |
| 101 | + saveYamlConfiguration(); |
| 102 | + |
| 103 | + for (final Player player : getServer().getOnlinePlayers()) { |
| 104 | + player.removeMetadata("logged", this); |
| 105 | + if(player.hasPermission("command.mod")) { |
| 106 | + StaffMode.toggleModMode(player); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + this.signGUI = new SignGUI(this); |
| 112 | + this.messageFile = new File(getDataFolder(), "messages.json"); |
| 113 | + if(messageFile.exists()) { |
| 114 | + AutoMessageManager.getMessageManager().load(this.messageFile); |
| 115 | + AutoMessageManager.getMessageManager().runTaskTimerAsynchronously(Main.getInstance(), 20L, 2400L); |
| 116 | + } else { |
| 117 | + AutoMessageManager.getMessageManager().addMessage("Default Message"); |
| 118 | + AutoMessageManager.getMessageManager().runTaskTimerAsynchronously(Main.getInstance(), 20L, 2400L); |
| 119 | + } |
| 120 | + |
| 121 | + for(World world : Bukkit.getWorlds()) { |
| 122 | + for(Player p : world.getPlayers()) { |
| 123 | + profileManager.createProfile(p.getUniqueId()); |
| 124 | + } |
| 125 | + } |
| 126 | + new BukkitRunnable() { |
| 127 | + public void run() { |
| 128 | + for (Player p : Bukkit.getServer().getOnlinePlayers()) { |
| 129 | + if (p.hasPermission("command.vip")) { |
| 130 | + Profile profile = profileManager.getProfile(p.getUniqueId()); |
| 131 | + if (!profile.isDonator()) { |
| 132 | + profile.setDonator(true); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + }.runTaskTimer(this, 200L, 200L); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void onDisable() { |
| 142 | + for(Player player : getServer().getOnlinePlayers()) { |
| 143 | + player.setMetadata("logged", (MetadataValue)new FixedMetadataValue(this, true)); |
| 144 | + } |
| 145 | + this.signGUI.destroy(); |
| 146 | + } |
| 147 | + |
| 148 | + private void setupHandlers() { |
| 149 | + this.mapHandler = new MapHandler(); |
| 150 | + new ServerManager(); |
| 151 | + new ConversationManager(); |
| 152 | + this.profileManager = new ProfileManager(); |
| 153 | + } |
| 154 | + |
| 155 | + private void setupCommands() { |
| 156 | + Teleport teleport = new Teleport(); |
| 157 | + getCommand(teleport.getCommand()).setExecutor(teleport); |
| 158 | + |
| 159 | + TeleportHere teleportHere = new TeleportHere(); |
| 160 | + getCommand(teleportHere.getCommand()).setExecutor(teleportHere); |
| 161 | + |
| 162 | + TeleportPosition teleportPosition = new TeleportPosition(); |
| 163 | + getCommand(teleportPosition.getCommand()).setExecutor(teleportPosition); |
| 164 | + |
| 165 | + Broadcast broadcast = new Broadcast(); |
| 166 | + getCommand(broadcast.getCommand()).setExecutor(broadcast); |
| 167 | + |
| 168 | + ClearChat clearChat = new ClearChat(); |
| 169 | + getCommand(clearChat.getCommand()).setExecutor(clearChat); |
| 170 | + |
| 171 | + Staff staff = new Staff(); |
| 172 | + getCommand(staff.getCommand()).setExecutor(staff); |
| 173 | + |
| 174 | + AutoMessage autoMessage = new AutoMessage(); |
| 175 | + getCommand(autoMessage.getCommand()).setExecutor(autoMessage); |
| 176 | + |
| 177 | + MuteChat muteChat = new MuteChat(); |
| 178 | + getCommand(muteChat.getCommand()).setExecutor(muteChat); |
| 179 | + |
| 180 | + SlowChat slowChat = new SlowChat(); |
| 181 | + getCommand(slowChat.getCommand()).setExecutor(slowChat); |
| 182 | + |
| 183 | + StaffChat staffChat = new StaffChat(); |
| 184 | + getCommand(staffChat.getCommand()).setExecutor(staffChat); |
| 185 | + |
| 186 | + SpawnMob spawnMob = new SpawnMob(); |
| 187 | + getCommand(spawnMob.getCommand()).setExecutor(spawnMob); |
| 188 | + |
| 189 | + ThrowBlock throwBlock = new ThrowBlock(); |
| 190 | + getCommand(throwBlock.getCommand()).setExecutor(throwBlock); |
| 191 | + |
| 192 | + Clearinventory clearinventory = new Clearinventory(); |
| 193 | + getCommand(clearinventory.getCommand()).setExecutor(clearinventory); |
| 194 | + |
| 195 | + Weather weather = new Weather(); |
| 196 | + getCommand(weather.getCommand()).setExecutor(weather); |
| 197 | + |
| 198 | + NameItem nameItem = new NameItem(); |
| 199 | + getCommand(nameItem.getCommand()).setExecutor(nameItem); |
| 200 | + |
| 201 | + Message message = new Message(); |
| 202 | + getCommand(message.getCommand()).setExecutor(message); |
| 203 | + |
| 204 | + Reply reply = new Reply(); |
| 205 | + getCommand(reply.getCommand()).setExecutor(reply); |
| 206 | + |
| 207 | + LOL lol = new LOL(); |
| 208 | + getCommand(lol.getCommand()).setExecutor(lol); |
| 209 | + |
| 210 | + CommandSpy commandSpy = new CommandSpy(); |
| 211 | + getCommand(commandSpy.getCommand()).setExecutor(commandSpy); |
| 212 | + |
| 213 | + SetSpawn setSpawn = new SetSpawn(); |
| 214 | + getCommand(setSpawn.getCommand()).setExecutor(setSpawn); |
| 215 | + |
| 216 | + SetStaff setStaff = new SetStaff(); |
| 217 | + getCommand(setStaff.getCommand()).setExecutor(setStaff); |
| 218 | + |
| 219 | + Alerts alerts = new Alerts(); |
| 220 | + getCommand(alerts.getCommand()).setExecutor(alerts); |
| 221 | + |
| 222 | + RemoveStaff removeStaff = new RemoveStaff(); |
| 223 | + getCommand(removeStaff.getCommand()).setExecutor(removeStaff); |
| 224 | + } |
| 225 | + |
| 226 | + private void setupListeners() { |
| 227 | + new EnchantmentLimiterListener(); |
| 228 | + new BorderListener(); |
| 229 | + new CombatLoggerListener(); |
| 230 | + new EnderpearlListener(); |
| 231 | + new MinerListener(); |
| 232 | + new PlayerListener(); |
| 233 | + new StaffMode(); |
| 234 | + new SpawnTagListener(); |
| 235 | + new LOL(); |
| 236 | + new OreListener(); |
| 237 | + DeathMessageHandler.init(); |
| 238 | + } |
| 239 | + |
| 240 | + public static ApplicableRegionSet getRegionAt(Location loc) { |
| 241 | + return WGBukkit.getRegionManager(loc.getWorld()).getApplicableRegions(loc); |
| 242 | + } |
| 243 | + |
| 244 | + private void loadConfig() { |
| 245 | + try { |
| 246 | + file = new File(getDataFolder() + File.separator + "mapSettings.yml"); |
| 247 | + yamlConfiguration = YamlConfiguration.loadConfiguration(file); |
| 248 | + if(!file.exists()) { |
| 249 | + file.createNewFile(); |
| 250 | + List<String> messageList = yamlConfiguration.getStringList("motd.text"); |
| 251 | + messageList.add("&cWelcome to the server!"); |
| 252 | + messageList.add("&6We hope you have a great time."); |
| 253 | + |
| 254 | + yamlConfiguration.set("motd.text", messageList); |
| 255 | + yamlConfiguration.set("motd.system", "&cDefault system message of the day."); |
| 256 | + yamlConfiguration.set("spawn.world", "world"); |
| 257 | + yamlConfiguration.set("spawn.x", 0.0D); |
| 258 | + yamlConfiguration.set("spawn.y", 70.0D); |
| 259 | + yamlConfiguration.set("spawn.z", 0.0D); |
| 260 | + yamlConfiguration.set("end.spawn.world", "world_the_end"); |
| 261 | + yamlConfiguration.set("end.spawn.x", 0.0D); |
| 262 | + yamlConfiguration.set("end.spawn.y", 70.0D); |
| 263 | + yamlConfiguration.set("end.spawn.z", 0.0D); |
| 264 | + |
| 265 | + yamlConfiguration.set("end.leavelocation.world", "world"); |
| 266 | + yamlConfiguration.set("end.leavelocation.x", 100.0D); |
| 267 | + yamlConfiguration.set("end.leavelocation.y", 70.0D); |
| 268 | + yamlConfiguration.set("end.leavelocation.z", 0.0D); |
| 269 | + |
| 270 | + yamlConfiguration.set("scoreboardEnabled", true); |
| 271 | + yamlConfiguration.set("scoreboardTitle", "&6&lHCF &c[Map 1]"); |
| 272 | + yamlConfiguration.set("teamspeakip", "&6TeamSpeak IP Here"); |
| 273 | + |
| 274 | + yamlConfiguration.set("pvptimer.enbaled", true); |
| 275 | + |
| 276 | + yamlConfiguration.set("spawntag.enabled", true); |
| 277 | + yamlConfiguration.set("spawntag.secondstotag", 60); |
| 278 | + |
| 279 | + yamlConfiguration.save(file); |
| 280 | + } |
| 281 | + } catch(Exception e) { |
| 282 | + e.printStackTrace(); |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | + public MapHandler getMapHandler() { |
| 287 | + return mapHandler; |
| 288 | + } |
| 289 | + |
| 290 | + public ConfigManager getConfigManager() { |
| 291 | + return configManager; |
| 292 | + } |
| 293 | + |
| 294 | + public static Main getInstance() { |
| 295 | + return instance; |
| 296 | + } |
| 297 | + |
| 298 | + private void saveYamlConfiguration() { |
| 299 | + try { |
| 300 | + this.yamlConfiguration.save(this.file); |
| 301 | + } catch(Exception e) { |
| 302 | + e.printStackTrace(); |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + public ProfileManager getProfileManager() { |
| 307 | + return profileManager; |
| 308 | + } |
| 309 | + |
| 310 | + private void setupEntityRegistar() { |
| 311 | + try { |
| 312 | + EntityRegistrar.registerCustomEntities(); |
| 313 | + } catch(Exception e) { |
| 314 | + e.printStackTrace(); |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + public void sendOps(String message) { |
| 319 | + for(Player player : Main.getInstance().getServer().getOnlinePlayers()) { |
| 320 | + if(player.isOp()) { |
| 321 | + player.sendMessage(ChatColor.translateAlternateColorCodes('&', message)); |
| 322 | + } |
| 323 | + } |
| 324 | + } |
| 325 | + |
| 326 | + public String colourMessage(String string) { |
| 327 | + return ChatColor.translateAlternateColorCodes('&', string); |
| 328 | + } |
| 329 | +} |
0 commit comments