Abstract HolidayManager a little more

Now we don’t have to manually edit this each year.
This commit is contained in:
TfT_02 2014-01-12 12:08:47 +01:00
parent 103bf593f3
commit b4a6ecc58c
3 changed files with 59 additions and 19 deletions

View File

@ -9,7 +9,6 @@ import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.commands.party.PartySubcommandType; import com.gmail.nossr50.commands.party.PartySubcommandType;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.HolidayManager;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
public class McmmoCommand implements CommandExecutor { public class McmmoCommand implements CommandExecutor {
@ -32,7 +31,7 @@ public class McmmoCommand implements CommandExecutor {
} }
sender.sendMessage(LocaleLoader.getString("MOTD.Version", mcMMO.p.getDescription().getVersion())); sender.sendMessage(LocaleLoader.getString("MOTD.Version", mcMMO.p.getDescription().getVersion()));
HolidayManager.anniversaryCheck(sender); mcMMO.getHolidayManager().anniversaryCheck(sender);
return true; return true;
case 1: case 1:

View File

@ -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.RepairableManager;
import com.gmail.nossr50.skills.repair.repairables.SimpleRepairableManager; import com.gmail.nossr50.skills.repair.repairables.SimpleRepairableManager;
import com.gmail.nossr50.util.ChimaeraWing; import com.gmail.nossr50.util.ChimaeraWing;
import com.gmail.nossr50.util.HolidayManager;
import com.gmail.nossr50.util.LogFilter; import com.gmail.nossr50.util.LogFilter;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
@ -64,6 +65,7 @@ public class mcMMO extends JavaPlugin {
private static RepairableManager repairableManager; private static RepairableManager repairableManager;
private static DatabaseManager databaseManager; private static DatabaseManager databaseManager;
private static FormulaManager formulaManager; private static FormulaManager formulaManager;
private static HolidayManager holidayManager;
/* File Paths */ /* File Paths */
private static String mainDirectory; private static String mainDirectory;
@ -154,6 +156,7 @@ public class mcMMO extends JavaPlugin {
PartyManager.loadParties(); PartyManager.loadParties();
formulaManager = new FormulaManager(); formulaManager = new FormulaManager();
holidayManager = new HolidayManager();
for (Player player : getServer().getOnlinePlayers()) { for (Player player : getServer().getOnlinePlayers()) {
UserManager.addUser(player); // In case of reload add all users back into UserManager 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 PartyManager.saveParties(); // Save our parties
ScoreboardManager.teardownAll(); ScoreboardManager.teardownAll();
formulaManager.saveFormula(); formulaManager.saveFormula();
holidayManager.saveAnniversaryFiles();
placeStore.saveAll(); // Save our metadata placeStore.saveAll(); // Save our metadata
placeStore.cleanUp(); // Cleanup empty metadata stores placeStore.cleanUp(); // Cleanup empty metadata stores
} }
@ -269,6 +273,10 @@ public class mcMMO extends JavaPlugin {
return formulaManager; return formulaManager;
} }
public static HolidayManager getHolidayManager() {
return holidayManager;
}
public static ChunkManager getPlaceStore() { public static ChunkManager getPlaceStore() {
return placeStore; return placeStore;
} }

View File

@ -12,6 +12,7 @@ import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Color; import org.bukkit.Color;
@ -29,8 +30,9 @@ import com.gmail.nossr50.mcMMO;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
public final class HolidayManager { public final class HolidayManager {
static String anniversaryFilePath = mcMMO.getFlatFileDirectory() + "anniversary";
private static ArrayList<String> hasCelebrated; private static ArrayList<String> hasCelebrated;
private static int CURRENT_YEAR;
private static int START_YEAR = 2011;
private static final List<Color> ALL_COLORS; private static final List<Color> ALL_COLORS;
private static final List<ChatColor> ALL_CHAT_COLORS; private static final List<ChatColor> ALL_CHAT_COLORS;
@ -63,11 +65,11 @@ public final class HolidayManager {
CHAT_FORMATS = ImmutableList.copyOf(chatFormats); CHAT_FORMATS = ImmutableList.copyOf(chatFormats);
} }
private HolidayManager() {}
// This gets called onEnable // This gets called onEnable
public static void createAnniversaryFile() { public HolidayManager() {
File anniversaryFile = new File(anniversaryFilePath); CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR);
File anniversaryFile = new File(mcMMO.getFlatFileDirectory(), "anniversary." + CURRENT_YEAR + ".yml");
if (!anniversaryFile.exists()) { if (!anniversaryFile.exists()) {
try { try {
@ -82,7 +84,7 @@ public final class HolidayManager {
try { try {
hasCelebrated.clear(); hasCelebrated.clear();
BufferedReader reader = new BufferedReader(new FileReader(anniversaryFilePath)); BufferedReader reader = new BufferedReader(new FileReader(anniversaryFile.getPath()));
String line = reader.readLine(); String line = reader.readLine();
while (line != null) { while (line != null) {
@ -95,10 +97,45 @@ public final class HolidayManager {
catch (Exception ex) { catch (Exception ex) {
mcMMO.p.getLogger().severe(ex.toString()); mcMMO.p.getLogger().severe(ex.toString());
} }
cleanupFiles();
}
private void cleanupFiles() {
File FlatFileDir = new File(mcMMO.getFlatFileDirectory());
File legacy = new File(FlatFileDir, "anniversary.yml");
List<File> toDelete = new ArrayList<File>();
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 // This gets called onDisable
public static void saveAnniversaryFiles() { public void saveAnniversaryFiles() {
String anniversaryFilePath = mcMMO.getFlatFileDirectory() + "anniversary." + CURRENT_YEAR + ".yml";
try { try {
BufferedWriter writer = new BufferedWriter(new FileWriter(anniversaryFilePath)); BufferedWriter writer = new BufferedWriter(new FileWriter(anniversaryFilePath));
for (String player : hasCelebrated) { for (String player : hasCelebrated) {
@ -113,15 +150,11 @@ public final class HolidayManager {
} }
// This gets called from /mcmmo command // This gets called from /mcmmo command
public static void anniversaryCheck(final CommandSender sender) { public void anniversaryCheck(final CommandSender sender) {
GregorianCalendar anniversaryStart = new GregorianCalendar(2014, Calendar.FEBRUARY, 3); GregorianCalendar anniversaryStart = new GregorianCalendar(CURRENT_YEAR, Calendar.FEBRUARY, 3);
GregorianCalendar anniversaryEnd = new GregorianCalendar(2014, Calendar.FEBRUARY, 6); GregorianCalendar anniversaryEnd = new GregorianCalendar(CURRENT_YEAR, Calendar.FEBRUARY, 6);
GregorianCalendar day = new GregorianCalendar(); GregorianCalendar day = new GregorianCalendar();
if (hasCelebrated == null) {
createAnniversaryFile();
}
if (hasCelebrated.contains(sender.getName())) { if (hasCelebrated.contains(sender.getName())) {
return; return;
} }
@ -130,7 +163,7 @@ public final class HolidayManager {
return; 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!"); sender.sendMessage(ChatColor.BLUE + "nossr50's work and all the devs, here's a firework show!");
if (sender instanceof Player) { if (sender instanceof Player) {
final int firework_amount = 10; final int firework_amount = 10;
@ -186,11 +219,11 @@ public final class HolidayManager {
hasCelebrated.add(sender.getName()); 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)); 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; int power = (int) (Misc.getRandom().nextDouble() * 3) + 1;
Type fireworkType = Type.values()[Misc.getRandom().nextInt(Type.values().length)]; Type fireworkType = Type.values()[Misc.getRandom().nextInt(Type.values().length)];
double varX = Misc.getRandom().nextGaussian() * 3; double varX = Misc.getRandom().nextGaussian() * 3;