From b4a6ecc58cbc66e6186b36d3f6c0c9d23eff7c3c Mon Sep 17 00:00:00 2001 From: TfT_02 Date: Sun, 12 Jan 2014 12:08:47 +0100 Subject: [PATCH] Abstract HolidayManager a little more MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now we don’t have to manually edit this each year. --- .../gmail/nossr50/commands/McmmoCommand.java | 3 +- src/main/java/com/gmail/nossr50/mcMMO.java | 8 +++ .../gmail/nossr50/util/HolidayManager.java | 67 ++++++++++++++----- 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/commands/McmmoCommand.java b/src/main/java/com/gmail/nossr50/commands/McmmoCommand.java index 373639e0b..438ceadd4 100644 --- a/src/main/java/com/gmail/nossr50/commands/McmmoCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/McmmoCommand.java @@ -9,7 +9,6 @@ import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.commands.party.PartySubcommandType; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.locale.LocaleLoader; -import com.gmail.nossr50.util.HolidayManager; import com.gmail.nossr50.util.Permissions; public class McmmoCommand implements CommandExecutor { @@ -32,7 +31,7 @@ public class McmmoCommand implements CommandExecutor { } sender.sendMessage(LocaleLoader.getString("MOTD.Version", mcMMO.p.getDescription().getVersion())); - HolidayManager.anniversaryCheck(sender); + mcMMO.getHolidayManager().anniversaryCheck(sender); return true; case 1: diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 3a16e6f31..bfd5666db 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -43,6 +43,7 @@ import com.gmail.nossr50.skills.repair.repairables.Repairable; import com.gmail.nossr50.skills.repair.repairables.RepairableManager; import com.gmail.nossr50.skills.repair.repairables.SimpleRepairableManager; import com.gmail.nossr50.util.ChimaeraWing; +import com.gmail.nossr50.util.HolidayManager; import com.gmail.nossr50.util.LogFilter; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; @@ -64,6 +65,7 @@ public class mcMMO extends JavaPlugin { private static RepairableManager repairableManager; private static DatabaseManager databaseManager; private static FormulaManager formulaManager; + private static HolidayManager holidayManager; /* File Paths */ private static String mainDirectory; @@ -154,6 +156,7 @@ public class mcMMO extends JavaPlugin { PartyManager.loadParties(); formulaManager = new FormulaManager(); + holidayManager = new HolidayManager(); for (Player player : getServer().getOnlinePlayers()) { UserManager.addUser(player); // In case of reload add all users back into UserManager @@ -199,6 +202,7 @@ public class mcMMO extends JavaPlugin { PartyManager.saveParties(); // Save our parties ScoreboardManager.teardownAll(); formulaManager.saveFormula(); + holidayManager.saveAnniversaryFiles(); placeStore.saveAll(); // Save our metadata placeStore.cleanUp(); // Cleanup empty metadata stores } @@ -269,6 +273,10 @@ public class mcMMO extends JavaPlugin { return formulaManager; } + public static HolidayManager getHolidayManager() { + return holidayManager; + } + public static ChunkManager getPlaceStore() { return placeStore; } diff --git a/src/main/java/com/gmail/nossr50/util/HolidayManager.java b/src/main/java/com/gmail/nossr50/util/HolidayManager.java index e8ddad18b..a3255e700 100644 --- a/src/main/java/com/gmail/nossr50/util/HolidayManager.java +++ b/src/main/java/com/gmail/nossr50/util/HolidayManager.java @@ -12,6 +12,7 @@ import java.util.Collections; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; +import java.util.regex.Pattern; import org.bukkit.ChatColor; import org.bukkit.Color; @@ -29,8 +30,9 @@ import com.gmail.nossr50.mcMMO; import com.google.common.collect.ImmutableList; public final class HolidayManager { - static String anniversaryFilePath = mcMMO.getFlatFileDirectory() + "anniversary"; private static ArrayList hasCelebrated; + private static int CURRENT_YEAR; + private static int START_YEAR = 2011; private static final List ALL_COLORS; private static final List ALL_CHAT_COLORS; @@ -63,11 +65,11 @@ public final class HolidayManager { CHAT_FORMATS = ImmutableList.copyOf(chatFormats); } - private HolidayManager() {} - // This gets called onEnable - public static void createAnniversaryFile() { - File anniversaryFile = new File(anniversaryFilePath); + public HolidayManager() { + CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR); + + File anniversaryFile = new File(mcMMO.getFlatFileDirectory(), "anniversary." + CURRENT_YEAR + ".yml"); if (!anniversaryFile.exists()) { try { @@ -82,7 +84,7 @@ public final class HolidayManager { try { hasCelebrated.clear(); - BufferedReader reader = new BufferedReader(new FileReader(anniversaryFilePath)); + BufferedReader reader = new BufferedReader(new FileReader(anniversaryFile.getPath())); String line = reader.readLine(); while (line != null) { @@ -95,10 +97,45 @@ public final class HolidayManager { catch (Exception ex) { mcMMO.p.getLogger().severe(ex.toString()); } + + cleanupFiles(); + } + + private void cleanupFiles() { + File FlatFileDir = new File(mcMMO.getFlatFileDirectory()); + File legacy = new File(FlatFileDir, "anniversary.yml"); + List toDelete = new ArrayList(); + + if (legacy.exists()) { + toDelete.add(legacy); + } + + Pattern pattern = Pattern.compile("anniversary\\.(?:.+)\\.yml"); + + for (String fileName : FlatFileDir.list()) { + if (!pattern.matcher(fileName).matches() || fileName.equals("anniversary." + CURRENT_YEAR + ".yml")) { + continue; + } + + File file = new File(FlatFileDir, fileName); + + if (file.isDirectory()) { + continue; + } + + toDelete.add(file); + } + + for (File file : toDelete) { + mcMMO.p.debug("Deleted: " + file.getName()); + file.delete(); + } } // This gets called onDisable - public static void saveAnniversaryFiles() { + public void saveAnniversaryFiles() { + String anniversaryFilePath = mcMMO.getFlatFileDirectory() + "anniversary." + CURRENT_YEAR + ".yml"; + try { BufferedWriter writer = new BufferedWriter(new FileWriter(anniversaryFilePath)); for (String player : hasCelebrated) { @@ -113,15 +150,11 @@ public final class HolidayManager { } // This gets called from /mcmmo command - public static void anniversaryCheck(final CommandSender sender) { - GregorianCalendar anniversaryStart = new GregorianCalendar(2014, Calendar.FEBRUARY, 3); - GregorianCalendar anniversaryEnd = new GregorianCalendar(2014, Calendar.FEBRUARY, 6); + public void anniversaryCheck(final CommandSender sender) { + GregorianCalendar anniversaryStart = new GregorianCalendar(CURRENT_YEAR, Calendar.FEBRUARY, 3); + GregorianCalendar anniversaryEnd = new GregorianCalendar(CURRENT_YEAR, Calendar.FEBRUARY, 6); GregorianCalendar day = new GregorianCalendar(); - if (hasCelebrated == null) { - createAnniversaryFile(); - } - if (hasCelebrated.contains(sender.getName())) { return; } @@ -130,7 +163,7 @@ public final class HolidayManager { return; } - sender.sendMessage(ChatColor.BLUE + "Happy 3 Year Anniversary! In honor of all of"); + sender.sendMessage(ChatColor.BLUE + "Happy " + (CURRENT_YEAR - START_YEAR) + " Year Anniversary! In honor of all of"); sender.sendMessage(ChatColor.BLUE + "nossr50's work and all the devs, here's a firework show!"); if (sender instanceof Player) { final int firework_amount = 10; @@ -186,11 +219,11 @@ public final class HolidayManager { hasCelebrated.add(sender.getName()); } - private static boolean getDateRange(Date date, Date start, Date end) { + private boolean getDateRange(Date date, Date start, Date end) { return !(date.before(start) || date.after(end)); } - private static void spawnFireworks(Player player) { + private void spawnFireworks(Player player) { int power = (int) (Misc.getRandom().nextDouble() * 3) + 1; Type fireworkType = Type.values()[Misc.getRandom().nextInt(Type.values().length)]; double varX = Misc.getRandom().nextGaussian() * 3;