Merge pull request #112 in MIN/mineplex from feature/new-year to develop

* commit '6b3a5416643389a7bcac8df93f40f492452971e5':
  Accurate timings
  Small edits
  Flame
  Made it activate in all lobbies via command.
  Added a new years countdown to the hub.
This commit is contained in:
Shaun Bennett 2015-12-31 14:51:49 -06:00
commit 74eb04e5b4
5 changed files with 423 additions and 0 deletions

View File

@ -54,6 +54,7 @@ import mineplex.core.updater.FileUpdater;
import mineplex.core.updater.Updater;
import mineplex.core.velocity.VelocityFix;
import mineplex.core.visibility.VisibilityManager;
import mineplex.hub.modules.NewYearCountdown;
import mineplex.hub.modules.StackerManager;
import mineplex.hub.queue.QueueManager;
import mineplex.hub.server.ServerManager;
@ -183,6 +184,8 @@ public class Hub extends JavaPlugin implements IRelation
//Updates
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Updater(this), 1, 1);
new NewYearCountdown(this);
}
@Override

View File

@ -0,0 +1,67 @@
package mineplex.hub.commands;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.hub.modules.NewYearCountdown;
import mineplex.hub.modules.newyear.NewYearMessage;
/**
* Created by William (WilliamTiger).
* 29/12/15
*/
public class NewYearCommand extends CommandBase<NewYearCountdown>
{
public NewYearCommand(NewYearCountdown plugin)
{
super(plugin, Rank.JNR_DEV, "newyear", "nycountdown", "nyc");
}
@Override
public void Execute(Player caller, String[] args)
{
if (Plugin.isInProgress())
{
caller.sendMessage(F.main("NYC", C.cRed + "A new year countdown event is already in progress!"));
return;
}
if (args.length != 3)
{
caller.sendMessage(F.main("NYC", C.cRed + "/newyear <hours> <minutes> <seconds>"));
return;
}
if (!(validateInteger(args[0]) && validateInteger(args[1]) && validateInteger(args[2])))
{
caller.sendMessage(F.main("NYC", C.cRed + "You have entered invalid hours/minutes/seconds."));
return;
}
int hours = Integer.valueOf(args[0]);
int mins = Integer.valueOf(args[1]);
int secs = Integer.valueOf(args[2]);
int total = secs + (mins * 60) + (hours * 60 * 60);
caller.sendMessage(F.main("NYC", "Sent command to all lobbies to start with " + F.elem(total + "") + " seconds."));
new NewYearMessage(total).publish();
}
private boolean validateInteger(String str)
{
boolean passed = false;
try
{
int i = Integer.valueOf(str);
passed = true;
}
catch (Exception e)
{
}
return passed;
}
}

View File

@ -0,0 +1,296 @@
package mineplex.hub.modules;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import mineplex.core.MiniPlugin;
import mineplex.core.command.CommandCenter;
import mineplex.core.common.util.C;
import mineplex.core.common.util.UtilFirework;
import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.UtilParticle;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilShapes;
import mineplex.core.common.util.UtilTextBottom;
import mineplex.core.common.util.UtilTextMiddle;
import mineplex.core.common.util.UtilWorld;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.hub.commands.NewYearCommand;
import mineplex.hub.modules.newyear.NewYearHandler;
import mineplex.hub.modules.newyear.NewYearMessage;
import mineplex.serverdata.commands.ServerCommandManager;
/**
* Created by William (WilliamTiger).
* 29/12/15
*/
public class NewYearCountdown extends MiniPlugin
{
private boolean _inProgress;
private Location _center;
private Location _spawn;
private int _stage;
private long _targetTime;
private BufferedImage _currentFrame;
public NewYearCountdown(JavaPlugin plugin)
{
super("New Year Countdown", plugin);
ServerCommandManager.getInstance().registerCommandType("NewYearCommand",
NewYearMessage.class, new NewYearHandler(this));
_center = new Location(UtilWorld.getWorld("world"), 0.5, 85, 37.5);
_spawn = new Location(UtilWorld.getWorld("world"), 0.5, 74, 0.5);
_inProgress = false;
}
@Override
public void addCommands()
{
CommandCenter.Instance.AddCommand(new NewYearCommand(this));
}
public boolean isInProgress()
{
return _inProgress;
}
public void start(int seconds)
{
_inProgress = true;
_stage = seconds;
UtilWorld.getWorld("world").setTime(18000);
_currentFrame = stringToBufferedImage(new Font("Tahoma", Font.PLAIN, 36), formatTimer(_stage));
_targetTime = System.currentTimeMillis() + (seconds * 1000);
}
@EventHandler
public void cmd(PlayerCommandPreprocessEvent e)
{
if (!e.getPlayer().getName().equalsIgnoreCase("WilliamTiger"))
return;
if (e.getMessage().equalsIgnoreCase("/lmao"))
{
e.setCancelled(true);
start(30);
}
}
@EventHandler
public void updateStage(UpdateEvent e)
{
if (e.getType() != UpdateType.TICK)
return;
if (!_inProgress)
return;
int stageAttempt = (int) ((_targetTime - System.currentTimeMillis()) / 1000);
if (stageAttempt != _stage)
{
_stage = stageAttempt;
if (_stage > 0 && _stage <= 10)
for (Player p : UtilServer.getPlayers())
p.playSound(p.getLocation(), Sound.NOTE_BASS, 1f, 1f);
if (_stage <= 0)
return;
UtilTextBottom.display(C.cWhiteB + "New Year's Countdown - " + C.cGoldB + formatTimer(_stage), UtilServer.getPlayers());
_currentFrame = stringToBufferedImage(new Font("Tahoma", Font.PLAIN, 36), formatTimer(_stage));
}
}
@EventHandler
public void update(UpdateEvent e)
{
if (e.getType() != UpdateType.FASTER)
return;
if (!_inProgress)
return;
if (_stage == 0)
{
_currentFrame = stringToBufferedImage(new Font("Tahoma", Font.PLAIN, 20), "Happy New Year");
UtilTextMiddle.display(C.cGoldB + "2016", C.cYellowB + "Happy New Year!", UtilServer.getPlayers());
//fireworkCircle();
}
if (_stage <= -120)
{
//End of event.
_inProgress = false;
_currentFrame = null;
UtilWorld.getWorld("world").setTime(0);
return;
}
if (_stage <= 0)
{
randomFirework(offsetLoc(_center));
randomFirework(offsetLoc(_center));
randomFirework(offsetLoc(_center));
}
new BukkitRunnable(){
ArrayList<Location> line = UtilShapes.getLinesDistancedPoints(offsetLoc(_center), offsetLoc(_center), 0.2);
UtilParticle.ParticleType type = getRandomParticle();
@Override
public void run()
{
UtilParticle.PlayParticle(type, line.get(0), 0, 0, 0, 0, 1, UtilParticle.ViewDist.MAX);
line.remove(0);
if (line.isEmpty())
this.cancel();
}
}.runTaskTimer(getPlugin(), 0L, 1L);
if (_currentFrame == null)
return;
for (int y = 0; y < _currentFrame.getHeight(); y++)
for (int x = 0; x < _currentFrame.getWidth(); x++)
{
if (java.awt.Color.black.getRGB() != _currentFrame.getRGB(x, y))
continue;
Vector v = new Vector((float) _currentFrame.getWidth() / 2 - x, (float) _currentFrame.getHeight() / 2 - y, 0).multiply(0.5);
UtilParticle.PlayParticle(UtilParticle.ParticleType.FLAME, _center.add(v), 0, 0, 0, 0, 1, UtilParticle.ViewDist.MAX);
_center.subtract(v);
}
}
private BufferedImage stringToBufferedImage(Font font, String s)
{
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
Graphics g = img.getGraphics();
g.setFont(font);
FontRenderContext frc = g.getFontMetrics().getFontRenderContext();
Rectangle2D rect = font.getStringBounds(s, frc);
g.dispose();
img = new BufferedImage((int) Math.ceil(rect.getWidth()), (int) Math.ceil(rect.getHeight()), BufferedImage.TYPE_4BYTE_ABGR);
g = img.getGraphics();
g.setColor(java.awt.Color.black);
g.setFont(font);
FontMetrics fm = g.getFontMetrics();
int x = 0;
int y = fm.getAscent();
g.drawString(s, x, y);
g.dispose();
return img;
}
private String formatTimer(long seconds)
{
long minutes = seconds / 60;
long hours = minutes / 60;
if ((hours % 60) > 0)
return zeroNumber(hours % 60) + ":" + zeroNumber(minutes % 60) + ":" + zeroNumber(seconds % 60);
if ((minutes % 60) > 0)
return zeroNumber(minutes % 60) + ":" + zeroNumber(seconds % 60);
return zeroNumber(seconds % 60);
}
private String zeroNumber(long time)
{
return (time < 10 ? "0" : "") + time;
}
private Location offsetLoc(Location loc)
{
Random random = new Random();
return loc.clone().add((random.nextInt(100) - 50) + random.nextDouble() - 0.5, random.nextInt(20) + random.nextDouble() - 0.5, (random.nextInt(100) - 50) + random.nextDouble() - 0.5);
}
public static void randomFirework(Location l)
{
Color c = Color.RED;
int r = new Random().nextInt(4);
if (r == 0)
{
c = Color.AQUA;
}
else if (r == 1)
{
c = Color.YELLOW;
}
else if (r == 2)
{
c = Color.RED;
} else if (r == 3)
{
c = Color.LIME;
}
FireworkEffect.Type t = FireworkEffect.Type.BALL;
int rType = new Random().nextInt(4);
if (rType == 0)
{
t = FireworkEffect.Type.BALL_LARGE;
}
else if (rType == 1)
{
t = FireworkEffect.Type.BURST;
}
else if (rType == 2)
{
t = FireworkEffect.Type.STAR;
}
else if (rType == 3)
{
t = FireworkEffect.Type.CREEPER;
}
Firework f = l.getWorld().spawn(l, Firework.class);
FireworkMeta fm = f.getFireworkMeta();
fm.addEffect(FireworkEffect.builder().with(t).withColor(c).build());
fm.setPower(0 + new Random().nextInt(2));
f.setFireworkMeta(fm);
}
private void fireworkCircle()
{
for (Location cur : UtilShapes.getPointsInCircle(_spawn, 35, 30))
{
UtilFirework.launchFirework(cur, FireworkEffect.Type.BALL, Color.FUCHSIA, false, true, cur.toVector().setY(3).multiply(1), 2);
}
}
public UtilParticle.ParticleType getRandomParticle()
{
List<UtilParticle.ParticleType> types = Arrays.asList(UtilParticle.ParticleType.FLAME, UtilParticle.ParticleType.HAPPY_VILLAGER, UtilParticle.ParticleType.RED_DUST, UtilParticle.ParticleType.FIREWORKS_SPARK);
return types.get(new Random().nextInt(types.size()));
}
}

View File

@ -0,0 +1,28 @@
package mineplex.hub.modules.newyear;
import mineplex.hub.modules.NewYearCountdown;
import mineplex.serverdata.commands.CommandCallback;
import mineplex.serverdata.commands.ServerCommand;
/**
* Created by William (WilliamTiger).
* 31/12/15
*/
public class NewYearHandler implements CommandCallback
{
private NewYearCountdown _newYear;
public NewYearHandler(NewYearCountdown newYear)
{
_newYear = newYear;
}
@Override
public void run(ServerCommand command)
{
if (command instanceof NewYearMessage)
{
_newYear.start(((NewYearMessage)command).getSeconds());
}
}
}

View File

@ -0,0 +1,29 @@
package mineplex.hub.modules.newyear;
import mineplex.serverdata.commands.ServerCommand;
/**
* Created by William (WilliamTiger).
* 31/12/15
*/
public class NewYearMessage extends ServerCommand
{
private int seconds;
public NewYearMessage(int seconds)
{
this.seconds = seconds;
}
public int getSeconds()
{
return seconds;
}
@Override
public void run()
{
}
}