Broken colored strings are making me nuts.

This commit is contained in:
taoneill 2010-12-19 15:18:00 -05:00
parent 86c93fe4e2
commit e790fd9c18
3 changed files with 227 additions and 45 deletions

View File

@ -0,0 +1,5 @@
public class Battle {
}

View File

@ -2,6 +2,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -30,6 +31,15 @@ public class War extends Plugin {
listener,
this,
PluginListener.Priority.MEDIUM);
etc.getLoader().addListener( PluginLoader.Hook.HEALTH_CHANGE,
listener,
this,
PluginListener.Priority.MEDIUM);
etc.getLoader().addListener( PluginLoader.Hook.DAMAGE,
listener,
this,
PluginListener.Priority.HIGH);
// etc.getLoader().addListener(
// PluginLoader.Hook.BLOCK_CREATED,
// listener,
@ -98,5 +108,71 @@ public class War extends Plugin {
return warzones;
}
public String str(String str) {
String out = "[war] " + str;
out = str(Colors.LightGray, out);
if(out.length() > 120) {
out = out.substring(0, 119);
}
log.log(Level.INFO, "Out: " + out);
return out;
}
public String str(String color, String str) {
if(str.length() > 60) {
String out = "";
List<String> subStrs = toSubStrings(str);
List<String> coloredSubStrs = new ArrayList<String>();
for(String sub : subStrs) {
String colored = "";
if(sub.length() < 60) {
colored = color + sub;
}
else {
// colored = color + sub.charAt(0) + color + sub.charAt(1) + color + sub.charAt(2) + color + sub.charAt(3) + color + sub.charAt(4) +
// sub.substring(5);
//if(sub.length() > 10) {
colored = color + sub;
//.substring(5, sub.length() - 5);
//colored += color + sub.charAt(sub.length() - 5) + color + sub.charAt(sub.length() - 4) + color + sub.charAt(sub.length() - 3)
// + color + sub.charAt(sub.length() - 2) + color + sub.charAt(sub.length() - 1);
//}
}
coloredSubStrs.add(colored);
}
for(String sub : coloredSubStrs) {
out += sub;
}
return out;
} else {
return color + str;
}
}
private List<String> toSubStrings(String str) {
List<String> subStrings = new ArrayList<String>();
int start = 0;
int end = 60;
while(end < str.length()) {
subStrings.add(str.substring(start, end));
start += 60;
end += 60;
}
if(start < str.length()) {
subStrings.add(str.substring(start));
}
return subStrings;
}
public Warzone findWarzone(String warzoneName) {
for(Warzone warzone : warzones) {
if(warzone.getName().equals(warzoneName)) {
return warzone;
}
}
return null;
}
}

View File

@ -1,4 +1,7 @@
import java.util.List;
import java.util.logging.Level;
public class WarListener extends PluginListener {
@ -9,7 +12,7 @@ public class WarListener extends PluginListener {
}
public void onLogin(Player player) {
player.sendMessage(Colors.Gray + "[war] War is on! You must pick sides (try /teams and /join).");
player.sendMessage(war.str("War is on! Pick your battle (try /warzones)."));
}
public boolean onCommand(Player player, java.lang.String[] split) {
@ -20,7 +23,7 @@ public class WarListener extends PluginListener {
// warzones
if(command.equals("/warzones")){
String warzonesMessage = Colors.Gray + "[war] Warzones: ";
String warzonesMessage = "Warzones: ";
for(Warzone warzone : war.getWarzones()) {
warzonesMessage += warzone.getName() + " ("
@ -31,22 +34,26 @@ public class WarListener extends PluginListener {
}
warzonesMessage += playerTotal + " players) ";
}
player.sendMessage(warzonesMessage + " Use /warzone <zone-name> to teleport to warzone, then use /teams and /join <team-name>.");
player.sendMessage(war.str(warzonesMessage + " Use /warzone <zone-name> to " +
"teleport to warzone, " +
"then use /teams and /join <team-name>."));
return true;
}
// warzone
else if(command.equals("/warzone")) {
if(split.length < 2) {
player.sendMessage(Colors.Gray + "[war] Usage: /warzone <warzone-name>.");
player.sendMessage(war.str("Usage: /warzone <warzone-name>."));
} else {
for(Warzone warzone : war.getWarzones()) {
if(warzone.getName().equals(split[2])){
player.teleportTo(warzone.getTeleport());
player.sendMessage(war.str("You've landed in the " + warzone.getName() +
" warzone. Use the /join command. " + getAllTeamsMsg(player)));
return true;
}
}
player.sendMessage(Colors.Gray + "[war] So such warzone.");
player.sendMessage("So such warzone.");
}
return true;
}
@ -54,17 +61,10 @@ public class WarListener extends PluginListener {
// /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).");
player.sendMessage(war.str("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);
player.sendMessage(war.str("" + getAllTeamsMsg(player)));
}
return true;
}
@ -72,7 +72,9 @@ public class WarListener extends PluginListener {
// /join <teamname>
else if(command.equals("/join")) {
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.");
player.sendMessage(war.str("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.warzone(player.getLocation()).getTeams();
@ -84,10 +86,9 @@ public class WarListener extends PluginListener {
}
}
if(foundTeam) {
player.sendMessage(Colors.Gray + Colors.Gray + "[war] Joined " + name);
etc.getServer().messageAll(Colors.Gray + "[war] " + player.getName() + " joined " + name);
etc.getServer().messageAll(war.str("" + player.getName() + " joined " + name));
} else {
player.sendMessage(Colors.Gray + "[war] No such team. Try /teams.");
player.sendMessage(war.str("No such team. Try /teams."));
}
}
return true;
@ -97,15 +98,16 @@ public class WarListener extends PluginListener {
// /team <msg>
else if(command.equals("/team")) {
if(split.length < 2) {
player.sendMessage(Colors.Gray + "[war] Usage: /team <message>. Sends a message only to your teammates.");
player.sendMessage(war.str("Usage: /team <message>. " +
"Sends a message only to your teammates."));
} else {
Team playerTeam = war.getPlayerTeam(player.getName());
String teamMessage = Colors.LightBlue + "<"+ player.getName() + ":> ";
String teamMessage = "<"+ player.getName() + ":> ";
for(int j = 1 ; j<split.length; j++) {
String part = split[j];
teamMessage += part + " ";
}
playerTeam.teamcast(teamMessage);
playerTeam.teamcast(war.str(Colors.LightBlue, teamMessage));
}
return true;
}
@ -115,14 +117,14 @@ public class WarListener extends PluginListener {
// /restartbattle
else if(command.equals("/restartbattle")) {
if(!war.inAnyWarzone(player.getLocation())) {
player.sendMessage(Colors.Gray + "[war] Usage: /restartbattle. Must be in warzone.");
player.sendMessage(war.str("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.");
team.teamcast(war.str("Resetting warzone."));
}
warzone.resetState();
player.sendMessage(Colors.Gray + "[war] Warzone reset.");
player.sendMessage(war.str("Warzone reset."));
}
return true;
}
@ -133,11 +135,13 @@ public class WarListener extends PluginListener {
// /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). ");
player.sendMessage(war.str("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.");
player.sendMessage(war.str("Team created with spawn here."));
}
return true;
}
@ -145,11 +149,13 @@ public class WarListener extends PluginListener {
// /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.");
player.sendMessage(war.str("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.");
player.sendMessage(war.str("Team spawn relocated."));
}
return true;
}
@ -157,14 +163,54 @@ public class WarListener extends PluginListener {
// /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). ");
player.sendMessage(war.str("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.");
Warzone warzone = war.findWarzone(split[1]);
if(warzone == null) {
// create the warzone
Warzone newZone = new Warzone(war.getServer(), split[1]);
war.addWarzone(newZone);
if(split[2].equals("northwest")) {
newZone.setNorthwest(player.getLocation());
player.sendMessage(war.str("Warzone added. Northwesternmost point set."));
} else {
newZone.setSoutheast(player.getLocation());
player.sendMessage(war.str("Warzone added. Southeasternmost point set."));
}
} else {
String message = "";
if(split[2].equals("northwest")) {
warzone.setNorthwest(player.getLocation());
message += "Northwesternmost point set." ;
} else {
warzone.setSoutheast(player.getLocation());
message += "Southeasternmost point set.";
}
if(warzone.getNorthwest() == null) {
message += " Still missing northwesternmost point.";
}
if(warzone.getSoutheast() == null) {
message += " Still missing southeasternmost point.";
}
if(warzone.getNorthwest() != null && warzone.getSoutheast() != null) {
if(warzone.ready()) {
message += " Warzone ready. Use /newteam while inside the warzone to create new teams. Make sure to use /setwarzonestart to " +
"set the warzone teleport point and initial state.";
} else if (warzone.tooSmall()) {
message += " Warzone is too small. Min north-south size: 20. Min east-west size: 20.";
} else if (warzone.tooBig()) {
message += " Warzone is too Big. Max north-south size: 1000. Max east-west size: 1000.";
}
}
player.sendMessage(war.str(message));
}
}
return true;
}
@ -173,15 +219,18 @@ public class WarListener extends PluginListener {
// /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). ");
player.sendMessage(war.str("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.saveState();
warzone.setTeleport(player.getLocation());
player.sendMessage(Colors.Gray + "[war] Warzone initial state and teleport location changed.");
player.sendMessage(war.str("Warzone initial state and teleport location changed."));
}
return true;
}
@ -189,14 +238,14 @@ public class WarListener extends PluginListener {
// /resetwarzone
else if(command.equals("/resetwarzone")) {
if(!war.inAnyWarzone(player.getLocation())) {
player.sendMessage(Colors.Gray + "[war] Usage: /resetwarzone. Must be in warzone.");
player.sendMessage(war.str("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.");
team.teamcast(war.str("Resetting warzone..."));
}
warzone.resetState();
player.sendMessage(Colors.Gray + "[war] Warzone reset.");
player.sendMessage(war.str("Warzone reset."));
}
return true;
}
@ -204,4 +253,56 @@ public class WarListener extends PluginListener {
return false;
}
public boolean onDamage(PluginLoader.DamageType damageType, BaseEntity attacker, BaseEntity defender, int damageAmount) {
if(attacker != null && defender != null && attacker.isPlayer() && defender.isPlayer()) {
// only let adversaries (same warzone, different team) attack each other
Player a = (Player) attacker;
Player d = (Player) defender;
Warzone attackerWarzone = war.warzone(a.getLocation());
Team attackerTeam = war.getPlayerTeam(a.getName());
Warzone defenderWarzone = war.warzone(d.getLocation());
Team defenderTeam = war.getPlayerTeam(d.getName());
if(attackerTeam != null && defenderTeam != null
&& attackerTeam != defenderTeam
&& attackerWarzone == defenderWarzone) {
war.getLogger().log(Level.INFO, a.getName() + " hit " + d.getName() + " for " + damageAmount);
return false; // adversaries!
} else {
a.sendMessage(war.str("Your attack was blocked!" +
" You must join a team " +
", then you'll be able to damage people " +
"in the other teams in that warzone."));
return true; // no pvp outside of the war battles, no friendly fire either
}
}
// mobs are always dangerous
return false;
}
public boolean onHealthChange(Player player, int before, int after) {
if(after < 0) {
Team team = war.getPlayerTeam(player.getName());
if(team != null){
// teleport to team spawn upon death
player.setHealth(20);
player.teleportTo(team.getTeamSpawn());
war.getLogger().log(Level.INFO, player.getName() + " died and was tp'd back to team " + team.getName() + "'s spawn");
return true;
}
}
return false;
}
private String getAllTeamsMsg(Player player){
String teamsMessage = "Teams: ";
for(Team team : war.warzone(player.getLocation()).getTeams()) {
teamsMessage += team.getName() + " (";
for(Player member : team.getPlayers()) {
teamsMessage += member.getName() + " ";
}
teamsMessage += ") ";
}
return teamsMessage;
}
}