Added warzones and state reset.

This commit is contained in:
taoneill 2010-12-19 07:07:59 -05:00
parent 0d877390da
commit 86c93fe4e2
5 changed files with 338 additions and 34 deletions

View File

@ -0,0 +1,4 @@
public class NotReadyException extends Exception {
}

View File

@ -13,7 +13,9 @@ public class War extends Plugin {
String name = "War";
String version = "0.1";
private final List<Team> teams = new ArrayList<Team>();
private final List<Warzone> warzones = new ArrayList<Warzone>();
//private final WarMessenger messenger = new WarMessenger();
public void initialize() {
this.log = Logger.getLogger("Minecraft");
@ -47,23 +49,54 @@ public class War extends Plugin {
}
public List<Team> getTeams() {
return teams;
}
public Team getPlayerTeam(String playerName) {
for(Team team : teams) {
for(Player player : team.getPlayers()) {
if(player.getName().equals(playerName)) {
return team;
}
}
for(Warzone warzone : warzones) {
return warzone.getPlayerTeam(playerName);
}
return null;
}
public Logger getLogger() {
return log;
}
public Warzone warzone(Location location) {
for(Warzone warzone : warzones) {
if(warzone.contains(location)) return warzone;
}
return null;
}
public boolean inAnyWarzone(Location location) {
if(warzone(location) == null) {
return false;
}
return true;
}
public boolean inWarzone(String warzoneName, Location location) {
Warzone currentZone = warzone(location);
if(currentZone == null) {
return false;
} else if (warzoneName.equals(currentZone.getName())){
return true;
}
return false;
}
public void addWarzone(Warzone zone) {
warzones.add(zone);
}
public Server getServer() {
// TODO Auto-generated method stub
return etc.getServer();
}
public List<Warzone> getWarzones() {
return warzones;
}
}

View File

@ -9,43 +9,73 @@ public class WarListener extends PluginListener {
}
public void onLogin(Player player) {
player.sendMessage("<war> War is on! You must pick sides (try /teams and /join).");
player.sendMessage(Colors.Gray + "[war] War is on! You must pick sides (try /teams and /join).");
}
public boolean onCommand(Player player, java.lang.String[] split) {
String command = split[0];
// /teams
if(command.equals("/teams")){
String teamsMessage = "<war> Teams: ";
for(Team team : war.getTeams()) {
teamsMessage += team.getName() + " (";
for(Player member : team.getPlayers()) {
teamsMessage += member.getName() + " ";
// Player commands: /warzones, /warzone, /teams, /join
// warzones
if(command.equals("/warzones")){
String warzonesMessage = Colors.Gray + "[war] Warzones: ";
for(Warzone warzone : war.getWarzones()) {
warzonesMessage += warzone.getName() + " ("
+ warzone.getTeams().size() + " teams, ";
int playerTotal = 0;
for(Team team : warzone.getTeams()) {
playerTotal += team.getPlayers().size();
}
teamsMessage += ") ";
warzonesMessage += playerTotal + " players) ";
}
player.sendMessage(teamsMessage);
player.sendMessage(warzonesMessage + " Use /warzone <zone-name> to teleport to warzone, then use /teams and /join <team-name>.");
return true;
}
// /newteam <teamname>
else if(command.equals("/newteam")) {
// warzone
else if(command.equals("/warzone")) {
if(split.length < 2) {
player.sendMessage("<war> Usage: /newteam <teamname>");
player.sendMessage(Colors.Gray + "[war] Usage: /warzone <warzone-name>.");
} else {
String name = split[1];
war.getTeams().add(new Team(name, player.getLocation()));
for(Warzone warzone : war.getWarzones()) {
if(warzone.getName().equals(split[2])){
player.teleportTo(warzone.getTeleport());
return true;
}
}
player.sendMessage(Colors.Gray + "[war] So such warzone.");
}
return true;
}
// /teams
if(command.equals("/teams")){
if(split.length < 2 || !war.inAnyWarzone(player.getLocation())) {
player.sendMessage(Colors.Gray + "[war] Usage: /teams. Must be in a warzone (try /warzones and /warzone).");
} else {
String teamsMessage = Colors.Gray + "[war] Teams: ";
for(Team team : war.warzone(player.getLocation()).getTeams()) {
teamsMessage += team.getName() + " (";
for(Player member : team.getPlayers()) {
teamsMessage += member.getName() + " ";
}
teamsMessage += ") ";
}
player.sendMessage(teamsMessage);
}
return true;
}
// /join <teamname>
else if(command.equals("/join")) {
if(split.length < 2) {
player.sendMessage("<war> Usage: /join <teamname>");
if(split.length < 2 || !war.inAnyWarzone(player.getLocation())) {
player.sendMessage(Colors.Gray + "[war] Usage: /join <team-name>. Teams are warzone specific. You must be inside a warzone to join a team.");
} else {
String name = split[1];
List<Team> teams = war.getTeams();
List<Team> teams = war.warzone(player.getLocation()).getTeams();
boolean foundTeam = false;
for(Team team : teams) {
if(team.getName().equals(name)) {
@ -54,22 +84,23 @@ public class WarListener extends PluginListener {
}
}
if(foundTeam) {
player.sendMessage("<war> Joined " + name);
etc.getServer().messageAll("<war> " + player.getName() + " joined " + name);
player.sendMessage(Colors.Gray + Colors.Gray + "[war] Joined " + name);
etc.getServer().messageAll(Colors.Gray + "[war] " + player.getName() + " joined " + name);
} else {
player.sendMessage("<war> No such team. Try /teams.");
player.sendMessage(Colors.Gray + "[war] No such team. Try /teams.");
}
}
return true;
}
// /team <msg>
else if(command.equals("/team")) {
if(split.length < 2) {
player.sendMessage("<war> Usage: /team <team-only message>");
player.sendMessage(Colors.Gray + "[war] Usage: /team <message>. Sends a message only to your teammates.");
} else {
Team playerTeam = war.getPlayerTeam(player.getName());
String teamMessage = "<" + playerTeam.getName() + " - " + player.getName() + ":> ";
String teamMessage = Colors.LightBlue + "<"+ player.getName() + ":> ";
for(int j = 1 ; j<split.length; j++) {
String part = split[j];
teamMessage += part + " ";
@ -79,6 +110,97 @@ public class WarListener extends PluginListener {
return true;
}
// Mod commands : /restartbattle
// /restartbattle
else if(command.equals("/restartbattle")) {
if(!war.inAnyWarzone(player.getLocation())) {
player.sendMessage(Colors.Gray + "[war] Usage: /restartbattle. Must be in warzone.");
} else {
Warzone warzone = war.warzone(player.getLocation());
for(Team team: warzone.getTeams()) {
team.teamcast(Colors.Gray + "[war] Resetting warzone.");
}
warzone.resetState();
player.sendMessage(Colors.Gray + "[war] Warzone reset.");
}
return true;
}
// Warzone maker commands: /setwarzone, /setwarzonestart, /resetwarzone, /newteam, /setteamspawn, .. /setmonument
// /newteam <teamname>
else if(command.equals("/newteam")) {
if(split.length < 2 || !war.inAnyWarzone(player.getLocation())) {
player.sendMessage(Colors.Gray + "[war] Usage: /newteam <team-name>. Sets the team spawn to the current location. Must be in a warzone (try /warzones and /warzone). ");
} else {
String name = split[1];
war.warzone(player.getLocation()).getTeams().add(new Team(name, player.getLocation()));
player.sendMessage(Colors.Gray + "[war] Team created with spawn here.");
}
return true;
}
// /setteamspawn
else if(command.equals("/setteamspawn")) {
if(split.length < 2 || war.getPlayerTeam(player.getName()) == null) {
player.sendMessage(Colors.Gray + "[war] Usage: /setteamspawn <team-name>. Sets the team spawn. Must be in warzone and team must already exist.");
} else {
Team playerTeam = war.getPlayerTeam(player.getName());
playerTeam.setTeamSpawn(player.getLocation());
player.sendMessage(Colors.Gray + "[war] Team spawn relocated.");
}
return true;
}
// /setwarzone
else if(command.equals("/setwarzone")) {
if(split.length < 3 || (split.length == 3 && (!split[2].equals("southeast") && !split[2].equals("northwest")))) {
player.sendMessage(Colors.Gray + "[war] Usage: /setwarzone <warzone-name> <'southeast'/'northwest'>. " +
"Defines the battleground boundary. The warzone is reset at the start of every battle. " +
"This command overwrites any previously saved blocks (i.e. make sure you reset with /restartbattle " +
"or /resetwarzone before changing the boundary). ");
} else {
Warzone zone = new Warzone(war.getServer(), split[1]);
war.addWarzone(zone);
player.sendMessage(Colors.Gray + "[war] Warzone added.");
}
return true;
}
// /setwarzonestate
else if(command.equals("/setwarzonestart")) {
if(!war.inAnyWarzone(player.getLocation())) {
player.sendMessage(Colors.Gray + "[war] Usage: /setwarzonestart. Must be in warzone. Changes the warzone state at the beginning of every battle. " +
"Also sets the teleport point for this warzone (i.e. make sure to use /warzone or the warzone tp point will change). " +
"Just like /setwarzone, this command overwrites any previously saved blocks (i.e. make sure you reset with /restartbattle " +
"or /resetwarzone before changing start state). ");
} else {
Warzone warzone = war.warzone(player.getLocation());
warzone.resetState();
warzone.setTeleport(player.getLocation());
player.sendMessage(Colors.Gray + "[war] Warzone initial state and teleport location changed.");
}
return true;
}
// /resetwarzone
else if(command.equals("/resetwarzone")) {
if(!war.inAnyWarzone(player.getLocation())) {
player.sendMessage(Colors.Gray + "[war] Usage: /resetwarzone. Must be in warzone.");
} else {
Warzone warzone = war.warzone(player.getLocation());
for(Team team: warzone.getTeams()) {
team.teamcast(Colors.Gray + "[war] Resetting warzone.");
}
warzone.resetState();
player.sendMessage(Colors.Gray + "[war] Warzone reset.");
}
return true;
}
return false;
}

View File

@ -0,0 +1,12 @@
public class WarMessenger {
public void sendMessage(Player player, String msg) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,133 @@
import java.util.ArrayList;
import java.util.List;
public class Warzone {
private String name;
private Location northwest;
private Location southeast;
private final List<Team> teams = new ArrayList<Team>();
private final Server server;
private int[][][] initialState = null;
private Location teleport;
public Warzone(Server server, String name) {
this.server = server;
this.name = name;
}
public boolean ready() {
if(getNorthwest() != null && getSoutheast() != null
&& !tooSmall() && !tooBig()) return true;
return false;
}
public boolean tooSmall() {
if((getSoutheast().x - getNorthwest().x < 20)
|| (getNorthwest().z - getSoutheast().z < 20)) return true;
return false;
}
public boolean tooBig() {
if((getSoutheast().x - getNorthwest().x > 1000)
|| (getNorthwest().z - getSoutheast().z > 1000)) return true;
return false;
}
public boolean contains(Location point) {
return point.x <= getSoutheast().x && point.x >= getNorthwest().x
&& point.z <= getNorthwest().z && point.z >= getSoutheast().z;
}
public List<Team> getTeams() {
return teams;
}
public Team getPlayerTeam(String playerName) {
for(Team team : teams) {
for(Player player : team.getPlayers()) {
if(player.getName().equals(playerName)) {
return team;
}
}
}
return null;
}
public String getName() {
return name;
}
public void setNorthwest(Location northwest) {
this.northwest = northwest;
if(ready()) {
saveState();
}
}
public void saveState() {
if(ready()){
int northSouth = (int)(southeast.x - northwest.x);
int eastWest = (int)(northwest.z - southeast.z);
initialState = new int[northSouth][128][eastWest];
for(int x = 0; x < northSouth; x++){
for(int y = 0; y < 128; y++) {
for(int z = 0; z < eastWest; z++) {
initialState[x][y][z] = server.getBlockAt(x, y, z).getType();
}
}
}
}
}
public void resetState() {
if(ready() && initialState != null){
// reset blocks
int northSouth = (int)(southeast.x - northwest.x);
int eastWest = (int)(northwest.z - southeast.z);
initialState = new int[northSouth][128][eastWest];
for(int x = 0; x < northSouth; x++){
for(int y = 0; y < 128; y++) {
for(int z = 0; z < eastWest; z++) {
server.setBlockAt(initialState[x][y][z],x, y, z);
}
}
}
// everyone back to team spawn with full health
for(Team team : teams) {
for(Player player : team.getPlayers()) {
player.setHealth(20);
player.teleportTo(team.getTeamSpawn());
}
}
}
}
public Location getNorthwest() {
return northwest;
}
public void setSoutheast(Location southeast) {
this.southeast = southeast;
if(ready()) {
saveState();
}
}
public Location getSoutheast() {
return southeast;
}
public void setTeleport(Location location) {
this.teleport = location;
}
public Location getTeleport() {
return this.teleport;
}
}