Skip to content

Commit

Permalink
Added /uh finish, to broadcast the winner(s) and launch some fireworks!
Browse files Browse the repository at this point in the history
Closes #33.
  • Loading branch information
AmauryCarrade committed Aug 28, 2014
1 parent 6cd371f commit f581b8d
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/main/java/me/azenet/UHPlugin/UHPluginCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;

import me.azenet.UHPlugin.i18n.I18n;
import me.azenet.UHPlugin.task.FireworksOnWinnersTask;

import org.apache.commons.lang.WordUtils;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -66,6 +67,7 @@ public UHPluginCommand(UHPlugin p) {
commands.add("resurrect");
commands.add("tpback");
commands.add("spec");
commands.add("finish");

teamCommands.add("add");
teamCommands.add("remove");
Expand Down Expand Up @@ -224,6 +226,7 @@ private void helpPage(CommandSender sender, int page) {
case 3:
sender.sendMessage(i.t("cmd.titleMiscCmd"));
sender.sendMessage(i.t("cmd.helpFreeze"));
sender.sendMessage(i.t("cmd.helpFinish"));
sender.sendMessage(i.t("cmd.helpAbout"));
break;
}
Expand Down Expand Up @@ -994,6 +997,64 @@ else if(subcommand.equalsIgnoreCase("check")) {
}


/**
* This commands broadcast the winner(s) of the game and sends some fireworks at these players.
* It fails if there is more than one team alive.
*
* Usage: /uh finish
*
* @param sender
* @param command
* @param label
* @param args
*/
@SuppressWarnings("unused")
private void doFinish(CommandSender sender, Command command, String label, String[] args) {

if(!p.getGameManager().isGameRunning()) {
sender.sendMessage(i.t("finish.notStarted"));
return;
}

if(p.getGameManager().getAliveTeamsCount() != 1) {
sender.sendMessage(i.t("finish.notFinished"));
return;
}

// There's only one team.
UHTeam winnerTeam = p.getGameManager().getAliveTeams().get(0);
ArrayList<Player> listWinners = winnerTeam.getPlayers();

if(p.getConfig().getBoolean("finish.message")) {
if(p.getGameManager().isGameWithTeams()) {
String winners = "";

for(Player winner : listWinners) {
if(winner == listWinners.get(0)) {
// Nothing
}
else if(winner == listWinners.get(listWinners.size() - 1)) {
winners += " " + i.t("finish.and") + " ";
}
else {
winners += ", ";
}
winners += winner.getName();
}

p.getServer().broadcastMessage(i.t("finish.broadcast.withTeams", winners, winnerTeam.getDisplayName()));
}
else {
p.getServer().broadcastMessage(i.t("finish.broadcast.withoutTeams", winnerTeam.getName()));
}
}

if(p.getConfig().getBoolean("finish.fireworks.enabled")) {
new FireworksOnWinnersTask(p, listWinners).runTaskTimer(p, 0l, 10l);
}
}


/**
* This command, /t <message>, is used to send a team-message.
*
Expand Down
99 changes: 99 additions & 0 deletions src/main/java/me/azenet/UHPlugin/UHUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@

package me.azenet.UHPlugin;

import java.util.Random;

import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.FireworkEffect.Builder;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.FireworkMeta;

public class UHUtils {

Expand Down Expand Up @@ -154,4 +162,95 @@ private static boolean isSafeSpot(Location location) {
return false;
}
}


/**
* Spawns a random firework at the given location.
*
* Please note: because the power of a firework is an integer, the min/max heights
* are with a precision of ±5 blocks.
*
* @param location The location where the firework will be spawned.
* @param heightMin The minimal height of the explosion.
* @param heightMax The maximal height of the explosion.
*
* @return The random firework generated.
*/
public static Firework generateRandomFirework(Location location, int heightMin, int heightMax) {
Random rand = new Random();

Firework firework = (Firework) location.getWorld().spawnEntity(location, EntityType.FIREWORK);
FireworkMeta meta = firework.getFireworkMeta();

int effectsCount = rand.nextInt(3) + 1;

for(int i = 0; i < effectsCount; i++) {
meta.addEffect(generateRandomFireworkEffect());
}

// One level of power is half a second of flight time.
// In half a second, a firework fly ~5 blocks.
// So, one level of power = ~5 blocks.
meta.setPower((int) Math.min(Math.floor((heightMin / 5) + rand.nextInt(heightMax / 5)), 128D));

firework.setFireworkMeta(meta);

return firework;
}

/**
* Generates a random firework effect.
*
* @return The firework effect.
*/
private static FireworkEffect generateRandomFireworkEffect() {
Random rand = new Random();
Builder fireworkBuilder = FireworkEffect.builder();

int colorCount = rand.nextInt(3) + 1;
int trailCount = rand.nextInt(3) + 1;

fireworkBuilder.flicker(rand.nextInt(3) == 1);
fireworkBuilder.trail(rand.nextInt(3) == 1);

for(int i = 0; i < colorCount; i++) {
fireworkBuilder.withColor(generateRandomColor());
}

for(int i = 0; i < trailCount; i++) {
fireworkBuilder.withFade(generateRandomColor());
}

// Random shape
int shape = rand.nextInt(5);
switch(shape) {
case 0:
fireworkBuilder.with(FireworkEffect.Type.BALL);
break;
case 1:
fireworkBuilder.with(FireworkEffect.Type.BALL_LARGE);
break;
case 2:
fireworkBuilder.with(FireworkEffect.Type.BURST);
break;
case 3:
fireworkBuilder.with(FireworkEffect.Type.CREEPER);
break;
case 4:
fireworkBuilder.with(FireworkEffect.Type.STAR);
break;
}

return fireworkBuilder.build();
}

/**
* Generates a random color.
*
* @return The color.
*/
private static Color generateRandomColor() {
Random rand = new Random();
return Color.fromBGR(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
}
}
55 changes: 55 additions & 0 deletions src/main/java/me/azenet/UHPlugin/task/FireworksOnWinnersTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Plugin UltraHardcore (UHPlugin)
* Copyright (C) 2013 azenet
* Copyright (C) 2014 Amaury Carrade
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [http://www.gnu.org/licenses/].
*/

package me.azenet.UHPlugin.task;

import java.util.List;

import me.azenet.UHPlugin.UHPlugin;
import me.azenet.UHPlugin.UHUtils;

import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

public class FireworksOnWinnersTask extends BukkitRunnable {

private UHPlugin p = null;
private List<Player> winners = null;

private long startTime = 0L;

public FireworksOnWinnersTask(UHPlugin p, List<Player> winners) {
this.p = p;
this.winners = winners;

this.startTime = System.currentTimeMillis();
}

@Override
public void run() {
for(Player winner : winners) {
UHUtils.generateRandomFirework(winner.getLocation().add(0.5, 0.5, 0.5), 0, 5);
}

if((System.currentTimeMillis() - startTime) / 1000 > p.getConfig().getInt("finish.fireworks.duration", 10)) {
this.cancel();
}
}

}
8 changes: 8 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ teams-options:
slow-start:
delayBetweenTP: 3 # in seconds

# Controls the behavior of the /uh finish command
finish:
message: true # If true, the name of the winner(s) will be broadcasted.
fireworks:
enabled: true # If true, some fireworks will be launched at the location of the winners.
duration: 10 # In seconds.


dynmap:
showSpawnLocations: true
showDeathLocations: true
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/i18n/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ keys:

titleMiscCmd: "{aqua}------ Miscellaneous commands ------"
helpFreeze: "{cc}/uh freeze {ci}: (un)freezes the entire game, or a player. See /uh freeze for details."
helpFinish: "{cc}/uh finish {ci}: displays the name of the winner(s) and launches some fireworks."
helpAbout: "{cc}/uh about {ci}: informations about the plugin and the translation."

teamHelpTitle: "{aqua}------ Team commands ------"
Expand Down Expand Up @@ -102,6 +103,16 @@ keys:

go: "{green}--- GO ---"

finish:
notStarted: "{ce}The game is not started!"
notFinished: "{ce}There's not one team alive!"

and: "and"

broadcast:
withTeams: "{darkgreen}{obfuscated}--{green} Congratulations to {0} (team {1}{green}) for their victory! {darkgreen}{obfuscated}--"
withoutTeams: "{darkgreen}{obfuscated}--{green} Congratulations to {0} for his victory! {darkgreen}{obfuscated}--"

episodes:
end: "{aqua}-------- End of episode {0} --------"
endForced: "{aqua}-------- End of episode {0} [forced by {1}] --------"
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/i18n/fr_FR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ keys:

titleMiscCmd: "{aqua}------ Commandes diverses ------"
helpFreeze: "{cc}/uh freeze {ci}: (dés)immobilise l'ensemble du jeu, ou un joueur. Consultez /uh freeze pour plus de détails."
helpFinish: "{cc}/uh finish {ci}: affiche le nom du/des vainqueurs et lance des feux d'artifices."
helpAbout: "{cc}/uh about {ci}: informations à propos du plugin et des traductions."

teamHelpTitle: "{aqua}------ Commandes liées aux équipes ------"
Expand Down Expand Up @@ -102,6 +103,16 @@ keys:

go: "{green}--- GO ---"

finish:
notStarted: "{ce}Le jeu n'est pas encore démarré !"
notFinished: "{ce}Il n'y a pas qu'une équipe encore en vie !"

and: "et"

broadcast:
withTeams: "{darkgreen}{obfuscated}--{green} Félicitations à {0} (équipe {1}{green}) pour leur victoire ! {darkgreen}{obfuscated}--"
withoutTeams: "{darkgreen}{obfuscated}--{green} Félicitation à {0} pour sa victoire ! {darkgreen}{obfuscated}--"

episodes:
end: "{aqua}-------- Fin de l'épisode {0} --------"
endForced: "{aqua}-------- Fin de l'épisode {0} [forcé par {1}] --------"
Expand Down

0 comments on commit f581b8d

Please sign in to comment.