2013-01-11 22:31:21 +01:00
|
|
|
package com.gmail.nossr50.util;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
2013-01-14 00:20:41 +01:00
|
|
|
import java.io.BufferedWriter;
|
2013-01-11 22:31:21 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileReader;
|
2013-01-14 00:20:41 +01:00
|
|
|
import java.io.FileWriter;
|
2013-01-11 22:31:21 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.Color;
|
|
|
|
import org.bukkit.FireworkEffect;
|
|
|
|
import org.bukkit.FireworkEffect.Type;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.EntityType;
|
|
|
|
import org.bukkit.entity.Firework;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.inventory.meta.FireworkMeta;
|
2013-01-14 00:20:41 +01:00
|
|
|
|
2013-01-11 22:31:21 +01:00
|
|
|
import com.gmail.nossr50.mcMMO;
|
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
public final class HolidayManager {
|
2013-01-11 22:31:21 +01:00
|
|
|
public static ArrayList<String> hasCelebrated;
|
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
private HolidayManager() {}
|
2013-01-26 23:01:55 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
// This gets called onEnable
|
2013-01-26 23:01:55 +01:00
|
|
|
public static void createAnniversaryFile() {
|
2013-01-11 22:31:21 +01:00
|
|
|
File anniversaryFile = new File(mcMMO.p.getDataFolder().getAbsolutePath() + File.separator + "anniversary");
|
|
|
|
|
|
|
|
if (!anniversaryFile.exists()) {
|
|
|
|
try {
|
|
|
|
anniversaryFile.createNewFile();
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|
|
|
|
catch (IOException ex) {
|
2013-02-13 04:18:47 +01:00
|
|
|
mcMMO.p.getLogger().severe(ex.toString());
|
2013-01-11 22:31:21 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
|
2013-01-11 22:31:21 +01:00
|
|
|
hasCelebrated = new ArrayList<String>();
|
|
|
|
|
|
|
|
try {
|
|
|
|
hasCelebrated.clear();
|
2013-01-14 00:20:41 +01:00
|
|
|
BufferedReader reader = new BufferedReader(new FileReader(mcMMO.p.getDataFolder().getAbsolutePath() + File.separator + "anniversary"));
|
2013-01-11 22:31:21 +01:00
|
|
|
String line = reader.readLine();
|
|
|
|
|
|
|
|
while (line != null) {
|
|
|
|
hasCelebrated.add(line);
|
|
|
|
line = reader.readLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.close();
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
2013-02-13 04:18:47 +01:00
|
|
|
mcMMO.p.getLogger().severe(ex.toString());
|
2013-01-11 22:31:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
// This gets called onDisable
|
2013-01-26 23:01:55 +01:00
|
|
|
public static void saveAnniversaryFiles() {
|
2013-01-14 00:20:41 +01:00
|
|
|
try {
|
|
|
|
BufferedWriter writer = new BufferedWriter(new FileWriter(mcMMO.p.getDataFolder().getAbsolutePath() + File.separator + "anniversary"));
|
|
|
|
for (String player : hasCelebrated) {
|
|
|
|
writer.write(player);
|
|
|
|
writer.newLine();
|
2013-01-11 22:31:21 +01:00
|
|
|
}
|
2013-01-14 00:20:41 +01:00
|
|
|
writer.close();
|
2013-01-11 22:31:21 +01:00
|
|
|
}
|
2013-01-14 00:20:41 +01:00
|
|
|
catch (Exception ex) {
|
2013-02-13 04:18:47 +01:00
|
|
|
mcMMO.p.getLogger().severe(ex.toString());
|
2013-01-14 00:20:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
// This gets called from /mcmmo command
|
2013-01-26 23:01:55 +01:00
|
|
|
public static void anniversaryCheck(final CommandSender sender) {
|
2013-01-14 00:20:41 +01:00
|
|
|
if (sender instanceof Player) {
|
|
|
|
GregorianCalendar anniversaryStart = new GregorianCalendar(2013, Calendar.FEBRUARY, 3);
|
|
|
|
GregorianCalendar anniversaryEnd = new GregorianCalendar(2013, Calendar.FEBRUARY, 6);
|
|
|
|
GregorianCalendar day = new GregorianCalendar();
|
2013-01-11 22:31:21 +01:00
|
|
|
|
2013-02-08 21:23:14 +01:00
|
|
|
if (hasCelebrated == null) {
|
|
|
|
createAnniversaryFile();
|
|
|
|
}
|
|
|
|
|
2013-01-14 00:20:41 +01:00
|
|
|
if (hasCelebrated.contains(sender.getName())) {
|
2013-01-21 23:22:54 +01:00
|
|
|
return;
|
2013-01-14 06:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (getDateRange(day.getTime(), anniversaryStart.getTime(), anniversaryEnd.getTime())) {
|
|
|
|
sender.sendMessage(ChatColor.BLUE + "Happy 2 Year Anniversary! In honor of all of");
|
|
|
|
sender.sendMessage(ChatColor.BLUE + "nossr50's work and all the devs, here's a firework show!");
|
|
|
|
final int firework_amount = 10;
|
|
|
|
for (int i = 0; i < firework_amount; i++) {
|
|
|
|
int delay = (int) (Math.random() * 3) + 4;
|
|
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(mcMMO.p, new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
spawnFireworks((Player) sender);
|
|
|
|
}
|
|
|
|
}, 20 * delay);
|
2013-01-11 22:31:21 +01:00
|
|
|
}
|
2013-02-09 10:59:41 +01:00
|
|
|
hasCelebrated.add(sender.getName());
|
2013-01-11 22:31:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:01:55 +01:00
|
|
|
private static boolean getDateRange(Date date, Date start, Date end) {
|
2013-01-11 22:31:21 +01:00
|
|
|
return !(date.before(start) || date.after(end));
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:01:55 +01:00
|
|
|
private static void spawnFireworks(Player player) {
|
2013-01-11 22:31:21 +01:00
|
|
|
int power = (int) (Math.random() * 3) + 1;
|
|
|
|
int type = (int) (Math.random() * 5) + 1;
|
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
Type typen;
|
|
|
|
switch (type) {
|
|
|
|
case 2:
|
|
|
|
typen = Type.BALL_LARGE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
typen = Type.BURST;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
typen = Type.CREEPER;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
typen = Type.STAR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
typen = Type.BALL;
|
|
|
|
}
|
2013-01-11 22:31:21 +01:00
|
|
|
|
|
|
|
Firework fireworks = (Firework) player.getWorld().spawnEntity(player.getLocation(), EntityType.FIREWORK);
|
|
|
|
FireworkMeta fireworkmeta = fireworks.getFireworkMeta();
|
2013-01-26 23:01:55 +01:00
|
|
|
FireworkEffect effect = FireworkEffect.builder().flicker(Misc.getRandom().nextBoolean()).withColor(colorchoose()).withFade(colorchoose()).with(typen).trail(Misc.getRandom().nextBoolean()).build();
|
2013-01-11 22:31:21 +01:00
|
|
|
fireworkmeta.addEffect(effect);
|
|
|
|
fireworkmeta.setPower(power);
|
|
|
|
fireworks.setFireworkMeta(fireworkmeta);
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:01:55 +01:00
|
|
|
private static List<Color> colorchoose() {
|
2013-01-11 22:31:21 +01:00
|
|
|
// Thanks Zomis and Tejpbit for the help with this function!
|
|
|
|
|
2013-01-26 23:01:55 +01:00
|
|
|
int numberofcolors = Misc.getRandom().nextInt(17) + 1;
|
2013-01-11 22:31:21 +01:00
|
|
|
|
|
|
|
List<Color> allcolors = new ArrayList<Color>();
|
|
|
|
allcolors.add(Color.AQUA);
|
|
|
|
allcolors.add(Color.BLACK);
|
|
|
|
allcolors.add(Color.BLUE);
|
|
|
|
allcolors.add(Color.FUCHSIA);
|
|
|
|
allcolors.add(Color.GRAY);
|
|
|
|
allcolors.add(Color.GREEN);
|
|
|
|
allcolors.add(Color.LIME);
|
|
|
|
allcolors.add(Color.MAROON);
|
|
|
|
allcolors.add(Color.NAVY);
|
|
|
|
allcolors.add(Color.OLIVE);
|
|
|
|
allcolors.add(Color.ORANGE);
|
|
|
|
allcolors.add(Color.PURPLE);
|
|
|
|
allcolors.add(Color.RED);
|
|
|
|
allcolors.add(Color.SILVER);
|
|
|
|
allcolors.add(Color.TEAL);
|
|
|
|
allcolors.add(Color.WHITE);
|
|
|
|
allcolors.add(Color.YELLOW);
|
|
|
|
|
|
|
|
List<Color> choosencolors = new ArrayList<Color>();
|
|
|
|
|
|
|
|
for (int i = 0; i < numberofcolors; i++) {
|
2013-01-26 23:01:55 +01:00
|
|
|
choosencolors.add(allcolors.remove(Misc.getRandom().nextInt(allcolors.size())));
|
2013-01-11 22:31:21 +01:00
|
|
|
}
|
|
|
|
return choosencolors;
|
|
|
|
}
|
|
|
|
}
|