users now set duration in ticks, and parties now stop on player quit

This commit is contained in:
Charlie Wang 2013-07-12 11:56:22 -04:00
parent 869210e0d2
commit b0f55fbefb
4 changed files with 31 additions and 17 deletions

View File

@ -8,6 +8,7 @@ import org.bukkit.entity.Sheep;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerShearEntityEvent; import org.bukkit.event.player.PlayerShearEntityEvent;
/** /**
@ -22,6 +23,7 @@ public class BaaBaaBlockSheepEvents implements Listener {
this.parent = parent; this.parent = parent;
} }
// prevent sheep shearing
@EventHandler @EventHandler
public void onPlayerShear(PlayerShearEntityEvent e) { public void onPlayerShear(PlayerShearEntityEvent e) {
if (e.getEntity() instanceof Sheep) { if (e.getEntity() instanceof Sheep) {
@ -47,4 +49,11 @@ public class BaaBaaBlockSheepEvents implements Listener {
} }
} }
} }
@EventHandler
public void onPlayerQuitEvent(PlayerQuitEvent e) {
String name = e.getPlayer().getName();
parent.stopParty(name);
}
} }

View File

@ -27,7 +27,6 @@ public class DiscoParty {
private Player player; private Player player;
private ArrayList<Sheep> sheepList = new ArrayList<Sheep>(); private ArrayList<Sheep> sheepList = new ArrayList<Sheep>();
private int duration, frequency = 20; private int duration, frequency = 20;
private int numSheep = 5;
static int defaultDuration = 300; // ticks for entire party static int defaultDuration = 300; // ticks for entire party
static int defaultPeriod = 10; // ticks per state change static int defaultPeriod = 10; // ticks per state change
static int defaultRadius = 5; static int defaultRadius = 5;
@ -68,7 +67,6 @@ public class DiscoParty {
void spawnSheep(World world, Location loc) { void spawnSheep(World world, Location loc) {
Sheep newSheep = (Sheep) world.spawnEntity(loc, EntityType.SHEEP); Sheep newSheep = (Sheep) world.spawnEntity(loc, EntityType.SHEEP);
newSheep.setColor(discoColours[(int) (Math.random() * (discoColours.length - 1))]); newSheep.setColor(discoColours[(int) (Math.random() * (discoColours.length - 1))]);
newSheep.setTarget(player);
newSheep.setBreed(false); newSheep.setBreed(false);
getSheep().add(newSheep); getSheep().add(newSheep);
} }
@ -81,12 +79,14 @@ public class DiscoParty {
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
double x, y, z; double x, y, z;
// random x and z coordinates within a 5 block radius // random x, z, and yaw
// safe y-coordinate // safe y-coordinate
x = -sheepSpawnRadius + (Math.random() * ((sheepSpawnRadius * 2) + 1)) + player.getLocation().getX(); x = -sheepSpawnRadius + (Math.random() * ((sheepSpawnRadius * 2) + 1)) + player.getLocation().getX();
z = -sheepSpawnRadius + (Math.random() * ((sheepSpawnRadius * 2) + 1)) + player.getLocation().getZ(); z = -sheepSpawnRadius + (Math.random() * ((sheepSpawnRadius * 2) + 1)) + player.getLocation().getZ();
y = world.getHighestBlockYAt((int) x, (int) z); y = world.getHighestBlockYAt((int) x, (int) z);
loc = new Location(world, x, y, z); loc = new Location(world, x, y, z);
//loc.setYaw(0);
//loc.setPitch((float) Math.random() * 360 - 180);
spawnSheep(world, loc); spawnSheep(world, loc);
} }
} }

View File

@ -23,26 +23,26 @@ public final class DiscoSheep extends JavaPlugin {
config.addDefault("max.sheep", DiscoParty.maxSheep); config.addDefault("max.sheep", DiscoParty.maxSheep);
config.addDefault("max.radius", DiscoParty.maxRadius); config.addDefault("max.radius", DiscoParty.maxRadius);
config.addDefault("max.duration", DiscoParty.maxDuration); config.addDefault("max.duration", toSeconds_i(DiscoParty.maxDuration));
config.addDefault("max.period", DiscoParty.maxPeriod); config.addDefault("max.period-ticks", DiscoParty.maxPeriod);
config.addDefault("min.period", DiscoParty.minPeriod); config.addDefault("min.period-ticks", DiscoParty.minPeriod);
config.addDefault("default.sheep", DiscoParty.defaultSheep); config.addDefault("default.sheep", DiscoParty.defaultSheep);
config.addDefault("default.radius", DiscoParty.defaultRadius); config.addDefault("default.radius", DiscoParty.defaultRadius);
config.addDefault("default.duration", DiscoParty.defaultDuration); config.addDefault("default.duration", toSeconds_i(DiscoParty.defaultDuration));
config.addDefault("default.period", DiscoParty.defaultPeriod); config.addDefault("default.period-ticks", DiscoParty.defaultPeriod);
config.options().copyDefaults(true); config.options().copyDefaults(true);
saveConfig(); saveConfig();
DiscoParty.maxSheep = getConfig().getInt("max.sheep"); DiscoParty.maxSheep = getConfig().getInt("max.sheep");
DiscoParty.maxRadius = getConfig().getInt("max.radius"); DiscoParty.maxRadius = getConfig().getInt("max.radius");
DiscoParty.maxDuration = getConfig().getInt("max.duration"); DiscoParty.maxDuration = toTicks(getConfig().getInt("max.duration"));
DiscoParty.maxPeriod = getConfig().getInt("max.period"); DiscoParty.maxPeriod = getConfig().getInt("max.period-ticks");
DiscoParty.minPeriod = getConfig().getInt("min.period"); DiscoParty.minPeriod = getConfig().getInt("min.period-ticks");
DiscoParty.defaultSheep = getConfig().getInt("default.sheep"); DiscoParty.defaultSheep = getConfig().getInt("default.sheep");
DiscoParty.defaultRadius = getConfig().getInt("default.radius"); DiscoParty.defaultRadius = getConfig().getInt("default.radius");
DiscoParty.defaultDuration = getConfig().getInt("default.duration"); DiscoParty.defaultDuration = toTicks(getConfig().getInt("default.duration"));
DiscoParty.defaultPeriod = getConfig().getInt("default.period"); DiscoParty.defaultPeriod = getConfig().getInt("default.period-ticks");
} }
@Override @Override
@ -58,6 +58,10 @@ public final class DiscoSheep extends JavaPlugin {
return (double) Math.round(ticks / 20.0); 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() { public synchronized Map<String, DiscoParty> getPartyMap() {
return this.parties; return this.parties;
} }

View File

@ -2,6 +2,7 @@ package gibstick.bukkit.discosheep;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -93,10 +94,10 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
return true; return true;
} }
} else if (args[i].equalsIgnoreCase("-t")) { } else if (args[i].equalsIgnoreCase("-t")) {
duration = parseNextIntArg(args, i); duration = parent.toTicks(parseNextIntArg(args, i));
if (duration < 1 || duration > parent.toSeconds(DiscoParty.maxDuration)) { if (duration < 1 || duration > DiscoParty.maxDuration) {
sender.sendMessage("The duration in ticks must be an integer within the range [1, " sender.sendMessage("The duration in seconds must be an integer within the range [1, "
+ parent.toSeconds(DiscoParty.maxDuration) + "]"); + parent.toSeconds(DiscoParty.maxDuration) + "]");
return true; return true;
} }