Renamed package to adhere to naming convention

This commit is contained in:
Gibstick
2013-07-19 20:19:54 -04:00
parent 4cfdf6fbe6
commit be78675c6d
5 changed files with 5 additions and 5 deletions

View File

@@ -0,0 +1,59 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ca.gibstick.discosheep;
import org.bukkit.entity.Sheep;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerShearEntityEvent;
/**
*
* @author Mauve
*/
public class BaaBaaBlockSheepEvents implements Listener {
DiscoSheep parent;
public BaaBaaBlockSheepEvents(DiscoSheep parent) {
this.parent = parent;
}
// prevent sheep shearing
@EventHandler
public void onPlayerShear(PlayerShearEntityEvent e) {
if (e.getEntity() instanceof Sheep) {
for (DiscoParty party : parent.getParties()) {
if (party.getSheep().contains((Sheep) e.getEntity())) {
e.setCancelled(true);
}
}
}
}
// actually make sheep invincible
@EventHandler
public void onEntityDamageEvent(EntityDamageEvent e) {
if (e.getEntity() instanceof Sheep) {
for (DiscoParty party : parent.getParties()) {
if (party.getSheep().contains((Sheep) e.getEntity())) {
{
party.jumpSheep((Sheep) e.getEntity());
e.setCancelled(true);
}
}
}
}
}
@EventHandler
public void onPlayerQuitEvent(PlayerQuitEvent e) {
String name = e.getPlayer().getName();
parent.stopParty(name);
}
}

View File

@@ -0,0 +1,359 @@
package ca.gibstick.discosheep;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.bukkit.Color;
import org.bukkit.DyeColor;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep;
import org.bukkit.FireworkEffect;
import org.bukkit.FireworkEffect.Builder;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.util.Vector;
/**
*
* @author Georgiy
*/
public class DiscoParty {
private DiscoSheep ds;
private Player player;
private ArrayList<Sheep> sheepList = new ArrayList<Sheep>();
private int duration, period, radius, sheep;
static int defaultDuration = 300; // ticks for entire party
static int defaultPeriod = 10; // ticks per state change
static int defaultRadius = 5;
static int defaultSheep = 10;
static float defaultSheepJump = 0.5f;
static int maxDuration = 2400; // 120 seconds
static int maxSheep = 100;
static int maxRadius = 100;
static int minPeriod = 5; // 0.25 seconds
static int maxPeriod = 40; // 2.0 seconds
private boolean doFireworks = false;
private boolean doJump = true;
private int state = 0;
private DiscoUpdater updater;
private static final DyeColor[] discoColours = {
DyeColor.RED,
DyeColor.ORANGE,
DyeColor.YELLOW,
DyeColor.GREEN,
DyeColor.BLUE,
DyeColor.LIGHT_BLUE,
DyeColor.PINK,
DyeColor.MAGENTA,
DyeColor.LIME,
DyeColor.CYAN,
DyeColor.PURPLE
};
public DiscoParty(DiscoSheep parent, Player player) {
this.ds = parent;
this.player = player;
this.duration = DiscoParty.defaultDuration;
this.period = DiscoParty.defaultPeriod;
this.radius = DiscoParty.defaultRadius;
this.sheep = DiscoParty.defaultSheep;
}
public DiscoParty(DiscoSheep parent) {
this.ds = parent;
this.duration = DiscoParty.defaultDuration;
this.period = DiscoParty.defaultPeriod;
this.radius = DiscoParty.defaultRadius;
this.sheep = DiscoParty.defaultSheep;
}
// copy but with new player
/**
*
* @param player The new player to be stored
* @return A copy of the class with the new player
*/
public DiscoParty DiscoParty(Player player) {
DiscoParty newParty = new DiscoParty(this.ds, player);
newParty.setDoFireworks(this.doFireworks);
newParty.setDuration(this.duration);
newParty.setPeriod(this.period);
newParty.setRadius(this.radius);
newParty.setSheep(this.sheep);
return newParty;
}
List<Sheep> getSheep() {
return sheepList;
}
public DiscoParty setPlayer(Player player) {
if (player != null) {
this.player = player;
return this;
} else {
throw new NullPointerException();
}
}
public DiscoParty setDuration(int duration) throws IllegalArgumentException {
if (duration <= DiscoParty.maxDuration && duration > 0) {
this.duration = duration;
return this;
} else {
throw new IllegalArgumentException();
}
}
public DiscoParty setPeriod(int period) throws IllegalArgumentException {
if (period >= DiscoParty.minPeriod && period <= DiscoParty.maxPeriod) {
this.period = period;
return this;
} else {
throw new IllegalArgumentException();
}
}
public DiscoParty setRadius(int radius) throws IllegalArgumentException {
if (radius <= DiscoParty.maxRadius && radius > 0) {
this.radius = radius;
return this;
} else {
throw new IllegalArgumentException();
}
}
public DiscoParty setSheep(int sheep) throws IllegalArgumentException {
if (sheep <= DiscoParty.maxSheep && sheep > 0) {
this.sheep = sheep;
return this;
} else {
throw new IllegalArgumentException();
}
}
public DiscoParty setDoFireworks(boolean doFireworks) {
this.doFireworks = doFireworks;
return this;
}
void spawnSheep(World world, Location loc) {
Sheep newSheep = (Sheep) world.spawnEntity(loc, EntityType.SHEEP);
newSheep.setColor(discoColours[(int) (Math.random() * (discoColours.length - 1))]);
newSheep.setBreed(false);
getSheep().add(newSheep);
}
// Spawn some number of sheep next to given player
void spawnSheep(int num, int sheepSpawnRadius) {
Location loc;
World world = player.getWorld();
for (int i = 0; i < num; i++) {
double x = player.getLocation().getX();
double z = player.getLocation().getZ();
double y;
// random point on circle with polar coordinates
double r = Math.sqrt(Math.random()) * sheepSpawnRadius; // sqrt for uniform distribution
double azimuth = Math.random() * 2 * Math.PI; // radians
x += r * Math.cos(azimuth);
z += r * Math.sin(azimuth);
y = world.getHighestBlockYAt((int) x, (int) z);
loc = new Location(world, x, y, z);
//loc.setYaw(0);
//loc.setPitch((float) Math.random() * 360 - 180);
spawnSheep(world, loc);
}
}
// Mark all sheep in the sheep array for removal, then clear the array
void removeAllSheep() {
for (Sheep sheeple : getSheep()) {
sheeple.setHealth(0);
sheeple.remove();
}
getSheep().clear();
}
// Set a random colour for all sheep in array
void randomizeSheepColour(Sheep sheep) {
sheep.setColor(discoColours[(int) Math.round(Math.random() * (discoColours.length - 1))]);
}
void jumpSheep(Sheep sheep) {
Vector orgVel = sheep.getVelocity();
Vector newVel = (new Vector()).copy(orgVel);
newVel.add(new Vector(0, defaultSheepJump, 0));
sheep.setVelocity(newVel);
}
private Color getColor(int i) {
Color c = null;
if (i == 1) {
c = Color.AQUA;
}
if (i == 2) {
c = Color.BLACK;
}
if (i == 3) {
c = Color.BLUE;
}
if (i == 4) {
c = Color.FUCHSIA;
}
if (i == 5) {
c = Color.GRAY;
}
if (i == 6) {
c = Color.GREEN;
}
if (i == 7) {
c = Color.LIME;
}
if (i == 8) {
c = Color.MAROON;
}
if (i == 9) {
c = Color.NAVY;
}
if (i == 10) {
c = Color.OLIVE;
}
if (i == 11) {
c = Color.ORANGE;
}
if (i == 12) {
c = Color.PURPLE;
}
if (i == 13) {
c = Color.RED;
}
if (i == 14) {
c = Color.SILVER;
}
if (i == 15) {
c = Color.TEAL;
}
if (i == 16) {
c = Color.WHITE;
}
if (i == 17) {
c = Color.YELLOW;
}
return c;
}
void updateAllSheep() {
int i = 0;
for (Sheep sheeple : getSheep()) {
randomizeSheepColour(sheeple);
if (doFireworks && state % 8 == 0) {
spawnRandomFireworkAtSheep(sheeple);
}
if (doJump) {
if (state % 2 == 0) {
if (Math.random() < 0.5) {
jumpSheep(sheeple);
}
}
}
i++;
}
}
void playSounds() {
player.playSound(player.getLocation(), Sound.NOTE_BASS_DRUM, 1.0f, 1.0f);
if (this.state % 2 == 0) {
player.playSound(player.getLocation(), Sound.NOTE_SNARE_DRUM, 1.0f, 1.0f);
}
if (this.state % 4 == 0) {
player.playSound(player.getLocation(), Sound.NOTE_STICKS, 1.0f, 1.0f);
}
player.playSound(player.getLocation(), Sound.BURP, 0.5f, (float) Math.random() + 1);
}
void randomizeFirework(Firework firework) {
Random r = new Random();
Builder effect = FireworkEffect.builder();
FireworkMeta meta = firework.getFireworkMeta();
// construct [1, 3] random colours
int numColours = r.nextInt(3) + 1;
Color[] colourArray = new Color[numColours];
for (int i = 0; i < numColours; i++) {
colourArray[i] = getColor(r.nextInt(17) + 1);
}
// randomize effects
effect.withColor(colourArray);
effect.flicker(r.nextDouble() < 0.5);
effect.trail(r.nextDouble() < 0.5);
effect.with(FireworkEffect.Type.values()[r.nextInt(FireworkEffect.Type.values().length)]);
// set random effect and randomize power
meta.addEffect(effect.build());
meta.setPower(r.nextInt(2));
// apply it to the given firework
firework.setFireworkMeta(meta);
}
void spawnRandomFireworkAtSheep(Sheep sheep) {
Firework firework = (Firework) sheep.getWorld().spawnEntity(sheep.getEyeLocation(), EntityType.FIREWORK);
randomizeFirework(firework);
}
void update() {
if (duration > 0) {
updateAllSheep();
playSounds();
duration -= period;
this.scheduleUpdate();
this.state++;
} else {
this.stopDisco();
}
}
void scheduleUpdate() {
updater = new DiscoUpdater(this);
updater.runTaskLater(ds, this.period);
}
void startDisco(int duration, int sheepAmount, int radius, int period, boolean fireworks) {
if (this.duration > 0) {
stopDisco();
}
this.spawnSheep(sheepAmount, radius);
this.doFireworks = fireworks;
this.period = period;
this.duration = duration;
this.scheduleUpdate();
ds.getPartyMap().put(this.player.getName(), this);
}
void startDisco() {
this.spawnSheep(sheep, radius);
this.scheduleUpdate();
ds.getPartyMap().put(this.player.getName(), this);
}
void stopDisco() {
removeAllSheep();
this.duration = 0;
if (updater != null) {
updater.cancel();
}
updater = null;
ds.getPartyMap().remove(this.player.getName());
}
}

View File

@@ -0,0 +1,122 @@
package ca.gibstick.discosheep;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public final class DiscoSheep extends JavaPlugin {
Map<String, DiscoParty> parties = new HashMap<String, DiscoParty>();
private BaaBaaBlockSheepEvents blockEvents = new BaaBaaBlockSheepEvents(this);
FileConfiguration config;
@Override
public void onEnable() {
getCommand("ds").setExecutor(new DiscoSheepCommandExecutor(this));
getServer().getPluginManager().registerEvents(blockEvents, this);
if (config == null) {
config = this.getConfig();
}
config.addDefault("max.sheep", DiscoParty.maxSheep);
config.addDefault("max.radius", DiscoParty.maxRadius);
config.addDefault("max.duration", toSeconds_i(DiscoParty.maxDuration));
config.addDefault("max.period-ticks", DiscoParty.maxPeriod);
config.addDefault("min.period-ticks", DiscoParty.minPeriod);
config.addDefault("default.sheep", DiscoParty.defaultSheep);
config.addDefault("default.radius", DiscoParty.defaultRadius);
config.addDefault("default.duration", toSeconds_i(DiscoParty.defaultDuration));
config.addDefault("default.period-ticks", DiscoParty.defaultPeriod);
loadConfigFromDisk();
}
void loadConfigFromDisk() {
getConfig().options().copyDefaults(true);
saveConfig();
DiscoParty.maxSheep = getConfig().getInt("max.sheep");
DiscoParty.maxRadius = getConfig().getInt("max.radius");
DiscoParty.maxDuration = toTicks(getConfig().getInt("max.duration"));
DiscoParty.maxPeriod = getConfig().getInt("max.period-ticks");
DiscoParty.minPeriod = getConfig().getInt("min.period-ticks");
DiscoParty.defaultSheep = getConfig().getInt("default.sheep");
DiscoParty.defaultRadius = getConfig().getInt("default.radius");
DiscoParty.defaultDuration = toTicks(getConfig().getInt("default.duration"));
DiscoParty.defaultPeriod = getConfig().getInt("default.period-ticks");
}
void reloadConfigFromDisk() {
reloadConfig();
loadConfigFromDisk();
}
@Override
public void onDisable() {
this.stopAllParties();
this.config = null;
}
int toTicks(double seconds) {
return (int) Math.round(seconds * 20.0);
}
double toSeconds(int ticks) {
return (double) Math.round(ticks / 20.0);
}
int toSeconds_i(int ticks) {
return (int) Math.round(ticks / 20.0);
}
public synchronized Map<String, DiscoParty> getPartyMap() {
return this.parties;
}
public synchronized List<DiscoParty> getParties() {
return new ArrayList(this.getPartyMap().values());
}
public void stopParty(String name) {
if (this.hasParty(name)) {
this.getParty(name).stopDisco();
}
}
public void stopAllParties() {
for (DiscoParty party : this.getParties()) {
party.stopDisco();
}
}
public boolean hasParty(String name) {
return this.getPartyMap().containsKey(name);
}
public DiscoParty getParty(String name) {
return this.getPartyMap().get(name);
}
public void removeParty(String name) {
if (this.hasParty(name)) {
this.getPartyMap().remove(name);
}
}
/*public void startParty(Player player, int duration, int sheepAmount, int radius, int period, boolean fireworksEnabled) {
* if (!hasParty(player.getName())) {
* DiscoParty ds = new DiscoParty(this, player);
* ds.setDuration(duration);
* ds.setSheep(sheepAmount);
* ds.setRadius(radius);
* ds.setPeriod(period);
* ds.setDoFireworks(fireworksEnabled);
* ds.startDisco();
* }
* }*/
}

View File

@@ -0,0 +1,253 @@
package ca.gibstick.discosheep;
import java.util.Arrays;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.CommandExecutor;
import org.bukkit.entity.Player;
public class DiscoSheepCommandExecutor implements CommandExecutor {
private DiscoSheep parent;
public DiscoSheepCommandExecutor(DiscoSheep parent) {
this.parent = parent;
}
private static final String PERMISSION_PARTY = "discosheep.party";
private static final String PERMISSION_ALL = "discosheep.partyall";
private static final String PERMISSION_FIREWORKS = "discosheep.fireworks";
private static final String PERMISSION_STOPALL = "discosheep.stopall";
private static final String PERMISSION_RELOAD = "discosheep.reload";
private static final String PERMISSION_OTHER = "discosheep.partyother";
private static final String PERMISSION_CHANGEPERIOD = "discosheep.changeperiod";
//private static final String DELIM = "[ ]+";
private boolean senderHasPerm(CommandSender sender, String permission) {
return sender.hasPermission(permission);
}
private boolean noPermsMessage(CommandSender sender, String permission) {
sender.sendMessage(ChatColor.RED + "You do not have the permission node " + ChatColor.GRAY + permission);
return false;
}
private boolean parseNextArg(String[] args, int i, String compare) {
if (i < args.length - 1) {
return args[i + 1].equalsIgnoreCase(compare);
}
return false;
}
private int parseNextIntArg(String[] args, int i) {
if (i < args.length - 1) {
try {
return Integer.parseInt(args[i + 1]);
} catch (NumberFormatException e) {
return -1;
}
}
return -1;
}
private Double parseNextDoubleArg(String[] args, int i) {
if (i < args.length - 1) {
return Double.parseDouble(args[i + 1]);
}
return -1.0d;
}
// extract a list of players from a list of arguments
private String[] parsePlayerList(String[] args, int i) {
int j = i;
while (j < args.length && !args[j].startsWith("-")) {
j++;
}
return Arrays.copyOfRange(args, i, j);
}
/*-- Actual commands begin here --*/
private boolean helpCommand(CommandSender sender) {
sender.sendMessage(ChatColor.YELLOW + "DiscoSheep Help\n"
+ ChatColor.GRAY + " Subcommands\n" + ChatColor.WHITE
+ "me: start a party for yourself\n"
+ "stop: stop your own party\n"
+ "all: start a party for all players on the server\n"
+ "stopall: stop all parties (takes no arguments)\n"
+ "other <players>: start a party for the space-delimited list of players\n"
+ ChatColor.GRAY + " Arguments\n" + ChatColor.WHITE
+ "-n <integer>: set the number of sheep per player that spawn\n"
+ "-t <integer>: set the party duration in seconds\n"
+ "-p <ticks>: set the number of ticks between each disco beat\n"
+ "-r <integer>: set radius of the area in which sheep can spawn\n"
+ "-fw: enables fireworks");
return true;
}
private boolean reloadCommand(CommandSender sender) {
if (senderHasPerm(sender, PERMISSION_RELOAD)) {
parent.reloadConfigFromDisk();
sender.sendMessage(ChatColor.GREEN + "DiscoSheep config reloaded from disk");
return true;
} else {
return noPermsMessage(sender, PERMISSION_RELOAD);
}
}
private boolean partyCommand(Player player, DiscoParty party) {
if (senderHasPerm(player, PERMISSION_PARTY)) {
if (!parent.hasParty(player.getName())) {
party.setPlayer(player);
party.startDisco();
} else {
player.sendMessage("You already have a party. Are you underground?");
}
return true;
} else {
return noPermsMessage(player, PERMISSION_PARTY);
}
}
private boolean partyAllCommand(CommandSender sender, DiscoParty party) {
if (senderHasPerm(sender, PERMISSION_ALL)) {
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
if (!parent.hasParty(p.getName())) {
DiscoParty individualParty = party.DiscoParty(p);
individualParty.startDisco();
p.sendMessage(ChatColor.RED + "LET'S DISCO!");
}
}
return true;
} else {
return noPermsMessage(sender, PERMISSION_ALL);
}
}
private boolean stopAllCommand(CommandSender sender) {
if (senderHasPerm(sender, PERMISSION_STOPALL)) {
parent.stopAllParties();
return true;
} else {
return noPermsMessage(sender, PERMISSION_STOPALL);
}
}
private boolean stopMeCommand(CommandSender sender) {
parent.stopParty(sender.getName());
return true;
}
private boolean partyOtherCommand(String[] players, CommandSender sender, DiscoParty party) {
if (senderHasPerm(sender, PERMISSION_OTHER)) {
Player p;
for (String playerName : players) {
p = Bukkit.getServer().getPlayer(playerName);
if (p != null) {
if (!parent.hasParty(p.getName())) {
DiscoParty individualParty = party.DiscoParty(p);
individualParty.startDisco();
}
} else {
sender.sendMessage("Invalid player: " + playerName);
}
}
return true;
} else {
return noPermsMessage(sender, PERMISSION_OTHER);
}
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Player player = null;
boolean isPlayer = false;
if (sender instanceof Player) {
player = (Player) sender;
isPlayer = true;
}
// check for commands that don't need a party
if (args.length == 1) {
if (args[0].equalsIgnoreCase("stopall")) {
return stopAllCommand(sender);
} else if (args[0].equalsIgnoreCase("stop") && isPlayer) {
return stopMeCommand(sender);
} else if (args[0].equalsIgnoreCase("help")) {
return helpCommand(sender);
} else if (args[0].equalsIgnoreCase("reload")) {
return reloadCommand(sender);
}
}
DiscoParty parentParty = new DiscoParty(parent);
for (int i = 1; i < args.length; i++) {
if (args[i].equalsIgnoreCase("-fw")) {
if (senderHasPerm(sender, PERMISSION_FIREWORKS)) {
parentParty.setDoFireworks(true);
} else {
return noPermsMessage(sender, PERMISSION_FIREWORKS);
}
} else if (args[i].equalsIgnoreCase("-r")) {
try {
parentParty.setRadius(parseNextIntArg(args, i));
//sender.sendMessage("RADIUS OK");
} catch (IllegalArgumentException e) {
sender.sendMessage("Radius must be an integer within the range [1, "
+ DiscoParty.maxRadius + "]");
return false;
}
} else if (args[i].equalsIgnoreCase("-n")) {
try {
parentParty.setSheep(parseNextIntArg(args, i));
//sender.sendMessage("SHEEP OK");
} catch (IllegalArgumentException e) {
sender.sendMessage("The number of sheep must be an integer within the range [1, "
+ DiscoParty.maxSheep + "]");
return false;
}
} else if (args[i].equalsIgnoreCase("-t")) {
try {
parentParty.setDuration(parent.toTicks(parseNextIntArg(args, i)));
//sender.sendMessage("DURATION OK");
} catch (IllegalArgumentException e) {
sender.sendMessage("The duration in seconds must be an integer within the range [1, "
+ parent.toSeconds(DiscoParty.maxDuration) + "]");
return false;
}
} else if (args[i].equalsIgnoreCase("-p")) {
if (!senderHasPerm(sender, PERMISSION_CHANGEPERIOD)) {
return noPermsMessage(sender, PERMISSION_CHANGEPERIOD);
}
try {
parentParty.setPeriod(parseNextIntArg(args, i));
//sender.sendMessage("PERIOD OK");
} catch (IllegalArgumentException e) {
sender.sendMessage(
"The period in ticks must be within the range ["
+ DiscoParty.minPeriod + ", "
+ DiscoParty.maxPeriod + "]");
return false;
}
}
}
if (args.length > 0) {
if (args[0].equalsIgnoreCase("all")) {
return partyAllCommand(sender, parentParty);
} else if (args[0].equalsIgnoreCase("me") && isPlayer) {
return partyCommand(player, parentParty);
} else if (args[0].equalsIgnoreCase("other")) {
return partyOtherCommand(parsePlayerList(args, 1), sender, parentParty);
} else {
sender.sendMessage(ChatColor.RED + "Invalid argument (certain commands do not work from console).");
return false;
}
}
return false;
}
}

View File

@@ -0,0 +1,17 @@
package ca.gibstick.discosheep;
import org.bukkit.scheduler.BukkitRunnable;
public class DiscoUpdater extends BukkitRunnable {
private DiscoParty parent;
public DiscoUpdater(DiscoParty parent) {
this.parent = parent;
}
@Override
public void run() {
parent.update();
}
}