Zone wall guard works. Yahoo

This commit is contained in:
taoneill 2011-01-12 02:16:34 -05:00
parent 894bbaca43
commit d598c921d5
6 changed files with 245 additions and 65 deletions

View File

@ -47,7 +47,7 @@ public class WarBlockListener extends BlockListener {
event.setCancelled(true);
}
}
if(zone.isImportantBlock(block)){
if(zone != null && zone.isImportantBlock(block)){
player.sendMessage(war.str("Can't build here."));
event.setCancelled(true);
}

View File

@ -51,14 +51,7 @@ public class Team {
this.volume.setCornerTwo(warzone.getWorld().getBlockAt(x+2, y+5, z+2));
}
public void setTeamSpawn(Location teamSpawn) {
this.teamSpawn = teamSpawn;
// this resets the block to old state
this.setVolume();
volume.saveBlocks();
private void initializeTeamSpawn(Location teamSpawn) {
// Set the spawn
int x = teamSpawn.getBlockX();
int y = teamSpawn.getBlockY();
@ -129,7 +122,17 @@ public class Team {
warzone.getWorld().getBlockAt(x-2, y+3, z-2).setType(material);
resetSign();
}
public void setTeamSpawn(Location teamSpawn) {
this.teamSpawn = teamSpawn;
// this resets the block to old state
this.setVolume();
volume.saveBlocks();
initializeTeamSpawn(teamSpawn);
}
public Location getTeamSpawn() {

View File

@ -39,7 +39,7 @@ public class Warzone {
private World world;
private Material originalSouthEastBlock;
private Material originalNorthWestBlock;
private final int minSafeDistanceFromWall = 5;
private final int minSafeDistanceFromWall = 4;
private List<ZoneWallGuard> zoneWallGuards = new ArrayList<ZoneWallGuard>();
private War war;
@ -91,10 +91,10 @@ public class Warzone {
}
public void setNorthwest(Location northwest) {
if(this.northwest != null) {
if(this.northwest != null && originalNorthWestBlock != null) {
// reset old corner
int highest = this.world.getHighestBlockYAt(this.northwest.getBlockX(), this.southeast.getBlockZ()) - 1;
Block oldTopNWBlock = this.world.getBlockAt(this.northwest.getBlockX(), highest, this.southeast.getBlockZ());
int highest = this.world.getHighestBlockYAt(this.northwest.getBlockX(), this.northwest.getBlockZ()) - 1;
Block oldTopNWBlock = this.world.getBlockAt(this.northwest.getBlockX(), highest, this.northwest.getBlockZ());
oldTopNWBlock.setType(originalNorthWestBlock);
}
this.northwest = northwest;
@ -111,7 +111,7 @@ public class Warzone {
}
public void setSoutheast(Location southeast) {
if(this.southeast != null) {
if(this.southeast != null && originalSouthEastBlock != null) {
// reset old corner
int highest = this.world.getHighestBlockYAt(this.southeast.getBlockX(), this.southeast.getBlockZ()) - 1;
Block oldTopSEBlock = this.world.getBlockAt(this.southeast.getBlockX(), highest, this.southeast.getBlockZ());
@ -140,7 +140,22 @@ public class Warzone {
public int saveState() {
if(ready()){
return volume.saveBlocks();
// removed everything to keep save clean
volume.resetWallBlocks(BlockFace.East);
volume.resetWallBlocks(BlockFace.West);
volume.resetWallBlocks(BlockFace.North);
volume.resetWallBlocks(BlockFace.South);
for(Team team : teams) {
team.getVolume().resetBlocks();
}
for(Monument monument : monuments) {
monument.remove();
}
int saved = volume.saveBlocks();
initializeZone(); // bring back stuff
return saved;
}
return 0;
}
@ -192,7 +207,7 @@ public class Warzone {
respawnPlayer(team, player);
}
team.setRemainingTickets(lifePool);
team.getVolume().resetBlocks();
team.setTeamSpawn(team.getTeamSpawn());
team.resetSign();
}
@ -449,22 +464,24 @@ public class Warzone {
}
public boolean isNearWall(Location latestPlayerLocation) {
if(Math.abs(southeast.getBlockZ() - latestPlayerLocation.getBlockZ()) < minSafeDistanceFromWall
&& latestPlayerLocation.getBlockX() <= southeast.getBlockX()
&& latestPlayerLocation.getBlockX() >= northwest.getBlockX()) {
return true; // near east wall
} else if (Math.abs(southeast.getBlockX() - latestPlayerLocation.getBlockX()) < minSafeDistanceFromWall
&& latestPlayerLocation.getBlockZ() <= northwest.getBlockZ()
&& latestPlayerLocation.getBlockZ() >= southeast.getBlockZ()) {
return true; // near south wall
} else if (Math.abs(northwest.getBlockX() - latestPlayerLocation.getBlockX()) < minSafeDistanceFromWall
&& latestPlayerLocation.getBlockZ() <= northwest.getBlockZ()
&& latestPlayerLocation.getBlockZ() >= southeast.getBlockZ()) {
return true; // near north wall
} else if (Math.abs(northwest.getBlockZ() - latestPlayerLocation.getBlockZ()) < minSafeDistanceFromWall
&& latestPlayerLocation.getBlockX() <= southeast.getBlockX()
&& latestPlayerLocation.getBlockX() >= northwest.getBlockX()) {
return true; // near west wall
if(volume.hasTwoCorners()) {
if(Math.abs(southeast.getBlockZ() - latestPlayerLocation.getBlockZ()) < minSafeDistanceFromWall
&& latestPlayerLocation.getBlockX() < southeast.getBlockX()
&& latestPlayerLocation.getBlockX() > northwest.getBlockX()) {
return true; // near east wall
} else if (Math.abs(southeast.getBlockX() - latestPlayerLocation.getBlockX()) < minSafeDistanceFromWall
&& latestPlayerLocation.getBlockZ() < northwest.getBlockZ()
&& latestPlayerLocation.getBlockZ() > southeast.getBlockZ()) {
return true; // near south wall
} else if (Math.abs(northwest.getBlockX() - latestPlayerLocation.getBlockX()) < minSafeDistanceFromWall
&& latestPlayerLocation.getBlockZ() < northwest.getBlockZ()
&& latestPlayerLocation.getBlockZ() > southeast.getBlockZ()) {
return true; // near north wall
} else if (Math.abs(northwest.getBlockZ() - latestPlayerLocation.getBlockZ()) < minSafeDistanceFromWall
&& latestPlayerLocation.getBlockX() < southeast.getBlockX()
&& latestPlayerLocation.getBlockX() > northwest.getBlockX()) {
return true; // near west wall
}
}
return false;
}
@ -499,9 +516,34 @@ public class Warzone {
// note: y + 1 to line up 3 sided square with player eyes
}
public ZoneWallGuard getPlayerZoneWallGuard(String name) {
public BlockFace getNearestWall(Location latestPlayerLocation) {
if(Math.abs(southeast.getBlockZ() - latestPlayerLocation.getBlockZ()) < minSafeDistanceFromWall
&& latestPlayerLocation.getBlockX() <= southeast.getBlockX()
&& latestPlayerLocation.getBlockX() >= northwest.getBlockX()) {
// near east wall
return BlockFace.East;
} else if (Math.abs(southeast.getBlockX() - latestPlayerLocation.getBlockX()) < minSafeDistanceFromWall
&& latestPlayerLocation.getBlockZ() <= northwest.getBlockZ()
&& latestPlayerLocation.getBlockZ() >= southeast.getBlockZ()) {
// near south wall
return BlockFace.South;
} else if (Math.abs(northwest.getBlockX() - latestPlayerLocation.getBlockX()) < minSafeDistanceFromWall
&& latestPlayerLocation.getBlockZ() <= northwest.getBlockZ()
&& latestPlayerLocation.getBlockZ() >= southeast.getBlockZ()) {
// near north wall
return BlockFace.North;
} else if (Math.abs(northwest.getBlockZ() - latestPlayerLocation.getBlockZ()) < minSafeDistanceFromWall
&& latestPlayerLocation.getBlockX() <= southeast.getBlockX()
&& latestPlayerLocation.getBlockX() >= northwest.getBlockX()) {
// near west wall
return BlockFace.West;
}
return null;
}
public ZoneWallGuard getPlayerZoneWallGuard(String name, BlockFace wall) {
for(ZoneWallGuard guard : zoneWallGuards) {
if(guard.getPlayer().getName().equals(name)) {
if(guard.getPlayer().getName().equals(name) && wall == guard.getWall()) {
return guard;
}
}
@ -510,20 +552,32 @@ public class Warzone {
}
public void protectZoneWallAgainstPlayer(Player player) {
ZoneWallGuard guard = getPlayerZoneWallGuard(player.getName());
BlockFace nearestWall = getNearestWall(player.getLocation());
ZoneWallGuard guard = getPlayerZoneWallGuard(player.getName(), nearestWall);
if(guard != null) {
// already protected, need to move the guard
guard.updatePlayerPosition(player.getLocation());
} else {
// new guard
guard = new ZoneWallGuard(player, war, this);
zoneWallGuards.add(guard);
}
}
public void dropZoneWallGuardIfAny(Player player) {
ZoneWallGuard guard = getPlayerZoneWallGuard(player.getName());
guard.updatePlayerPosition(player.getLocation()); // should restore old blocks
zoneWallGuards.remove(guard);
List<ZoneWallGuard> playerGuards = new ArrayList<ZoneWallGuard>();
for(ZoneWallGuard guard : zoneWallGuards) {
if(guard.getPlayer().getName().equals(player.getName())){
playerGuards.add(guard);
int reset = volume.resetWallBlocks(guard.getWall()); // this should restore old blocks
war.getLogger().info("Reset " + reset + " blocks in " + guard.getWall() + "wall of warzone " + name);
}
}
// now remove those zone guards
for(ZoneWallGuard playerGuard : playerGuards) {
zoneWallGuards.remove(playerGuard);
}
playerGuards.clear();
}

View File

@ -15,8 +15,8 @@ public class ZoneWallGuard {
private Player player;
private Warzone warzone;
private Location playerLocation;
private CenteredVolume volume;
private final War war;
private BlockFace wall;
private final int radius = 3;
@ -32,12 +32,15 @@ public class ZoneWallGuard {
private void activate() {
// save current blocks
Block nearestWallBlock = warzone.getNearestWallBlock(playerLocation);
if(volume == null) {
volume = new CenteredVolume("zoneGuard-" + warzone.getName() + "-" + player.getName(), nearestWallBlock, radius, war, warzone);
} else {
volume.changeCenter(nearestWallBlock, radius);
volume.saveBlocks();
}
// if(volume == null) {
// volume = new CenteredVolume("zoneGuard-" + warzone.getName() + "-" + player.getName(), nearestWallBlock, radius, war, warzone);
// int saved = volume.saveBlocks();
// war.getLogger().info("Warzone wall guard created: " + saved + " blocks saved for " + player.getName());
// } else {
// volume.changeCenter(nearestWallBlock, radius);
// int saved = volume.saveBlocks();
// war.getLogger().info("Warzone wall guard updated: " + saved + " blocks saved for " + player.getName());
// }
// add wall guard blocks
nearestWallBlock.setType(Material.Glass);
nearestWallBlock.getFace(BlockFace.Up).setType(Material.Glass);
@ -45,6 +48,7 @@ public class ZoneWallGuard {
if(warzone.getVolume().isNorthWallBlock(nearestWallBlock.getFace(BlockFace.East)) &&
warzone.getVolume().isNorthWallBlock(nearestWallBlock.getFace(BlockFace.West))) {
// north wall guard
this.wall = BlockFace.North;
toGlass(nearestWallBlock.getFace(BlockFace.East), BlockFace.North);
toGlass(nearestWallBlock.getFace(BlockFace.East).getFace(BlockFace.Up), BlockFace.North);
toGlass(nearestWallBlock.getFace(BlockFace.East).getFace(BlockFace.Down), BlockFace.North);
@ -54,6 +58,7 @@ public class ZoneWallGuard {
} else if (warzone.getVolume().isSouthWallBlock(nearestWallBlock.getFace(BlockFace.East)) &&
warzone.getVolume().isSouthWallBlock(nearestWallBlock.getFace(BlockFace.West))) {
// south wall guard
this.wall = BlockFace.South;
toGlass(nearestWallBlock.getFace(BlockFace.East), BlockFace.South);
toGlass(nearestWallBlock.getFace(BlockFace.East).getFace(BlockFace.Up), BlockFace.South);
toGlass(nearestWallBlock.getFace(BlockFace.East).getFace(BlockFace.Down), BlockFace.South);
@ -64,6 +69,7 @@ public class ZoneWallGuard {
} else if (warzone.getVolume().isEastWallBlock(nearestWallBlock.getFace(BlockFace.North)) &&
warzone.getVolume().isEastWallBlock(nearestWallBlock.getFace(BlockFace.South))) {
//east wall guard
this.wall = BlockFace.East;
toGlass(nearestWallBlock.getFace(BlockFace.North), BlockFace.East);
toGlass(nearestWallBlock.getFace(BlockFace.North).getFace(BlockFace.Up), BlockFace.East);
toGlass(nearestWallBlock.getFace(BlockFace.North).getFace(BlockFace.Down), BlockFace.East);
@ -73,6 +79,7 @@ public class ZoneWallGuard {
} else if (warzone.getVolume().isWestWallBlock(nearestWallBlock.getFace(BlockFace.North)) &&
warzone.getVolume().isWestWallBlock(nearestWallBlock.getFace(BlockFace.South))) {
//west wall guard
this.wall = BlockFace.West;
toGlass(nearestWallBlock.getFace(BlockFace.North), BlockFace.West);
toGlass(nearestWallBlock.getFace(BlockFace.North).getFace(BlockFace.Up), BlockFace.West);
toGlass(nearestWallBlock.getFace(BlockFace.North).getFace(BlockFace.Down), BlockFace.West);
@ -102,23 +109,23 @@ public class ZoneWallGuard {
}
}
}
private void deactivate() {
// restore old blocks
volume.resetBlocks();
}
public void updatePlayerPosition(Location location) {
if(warzone.isNearWall(location)) {
deactivate();
this.playerLocation = location;
activate();
} else {
deactivate();
}
}
public Player getPlayer() {
return player;
}
public void setWall(BlockFace wall) {
this.wall = wall;
}
public BlockFace getWall() {
return wall;
}
}

View File

@ -1,6 +1,10 @@
package com.tommytony.war.volumes;
import org.bukkit.Block;
import org.bukkit.BlockFace;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import bukkit.tommytony.war.War;
@ -66,4 +70,104 @@ public class VerticalVolume extends Volume{
}
return false;
}
public int resetWallBlocks(BlockFace wall) {
int noOfResetBlocks = 0;
try {
if(hasTwoCorners() && getBlockInfos() != null) {
if(wall == BlockFace.East) {
int z = getMinZ();
int k = 0;
int y = getMinY();
for(int j = 0; j < getSizeY(); j++) {
int x = getMinX();
for(int i = 0; i < getSizeX(); i++) {
BlockInfo oldBlockInfo = getBlockInfos()[i][j][k];
Block currentBlock = getWorld().getBlockAt(x, y, z);
if(resetBlock(oldBlockInfo, currentBlock)) {
noOfResetBlocks++;
}
x++;
}
y++;
}
} else if(wall == BlockFace.West) {
int z = getMaxZ();
int k = getSizeZ()-1;
int y = getMinY();
for(int j = 0; j < getSizeY(); j++) {
int x = getMinX();
for(int i = 0; i < getSizeX(); i++) {
BlockInfo oldBlockInfo = getBlockInfos()[i][j][k];
Block currentBlock = getWorld().getBlockAt(x, y, z);
if(resetBlock(oldBlockInfo, currentBlock)) {
noOfResetBlocks++;
}
x++;
}
y++;
}
} else if(wall == BlockFace.North) {
int x = getMinX();
int i = 0;
int y = getMinY();
for(int j = 0; j < getSizeY(); j++) {
int z = getMinZ();
for(int k = 0; k < getSizeZ(); k++) {
BlockInfo oldBlockInfo = getBlockInfos()[i][j][k];
Block currentBlock = getWorld().getBlockAt(x, y, z);
if(resetBlock(oldBlockInfo, currentBlock)) {
noOfResetBlocks++;
}
z++;
}
y++;
}
} else if(wall == BlockFace.South) {
int x = getMaxX();
int i = getSizeX()-1;
int y = getMinY();
for(int j = 0; j < getSizeY(); j++) {
int z = getMinZ();
for(int k = 0; k < getSizeZ(); k++) {
BlockInfo oldBlockInfo = getBlockInfos()[i][j][k];
Block currentBlock = getWorld().getBlockAt(x, y, z);
if(resetBlock(oldBlockInfo, currentBlock)) {
noOfResetBlocks++;
}
z++;
}
y++;
}
}
}
} catch (Exception e) {
this.getWar().getLogger().warning(getWar().str("Failed to reset wall " + wall + " in volume " + getName() + ". " + e.getMessage()));
}
return noOfResetBlocks;
}
private boolean resetBlock(BlockInfo oldBlockInfo, Block currentBlock) {
if(currentBlock.getTypeID() != oldBlockInfo.getTypeID() ||
(currentBlock.getTypeID() == oldBlockInfo.getTypeID() && currentBlock.getData() != oldBlockInfo.getData()) ||
(currentBlock.getTypeID() == oldBlockInfo.getTypeID() && currentBlock.getData() == oldBlockInfo.getData() &&
(oldBlockInfo.is(Material.Sign) || oldBlockInfo.is(Material.SignPost))
)
) {
currentBlock.setType(oldBlockInfo.getType());
currentBlock.setData(oldBlockInfo.getData());
if(oldBlockInfo.is(Material.Sign) || oldBlockInfo.is(Material.SignPost)) {
BlockState state = currentBlock.getState();
Sign currentSign = (Sign) state;
currentSign.setLine(0, oldBlockInfo.getSignLines()[0]);
currentSign.setLine(1, oldBlockInfo.getSignLines()[0]);
currentSign.setLine(2, oldBlockInfo.getSignLines()[0]);
currentSign.setLine(3, oldBlockInfo.getSignLines()[0]);
state.update();
}
return true;
}
return false;
}
}

View File

@ -50,14 +50,14 @@ public class Volume {
int noOfSavedBlocks = 0;
try {
if(hasTwoCorners()) {
this.blockInfos = new BlockInfo[getSizeX()][getSizeY()][getSizeZ()];
this.setBlockInfos(new BlockInfo[getSizeX()][getSizeY()][getSizeZ()]);
int x = getMinX();
for(int i = 0; i < getSizeX(); i++){
int y = getMinY();
for(int j = 0; j < getSizeY(); j++){
int z = getMinZ();
for(int k = 0;k < getSizeZ(); k++) {
this.blockInfos[i][j][k] = new BlockInfo(world.getBlockAt(x, y, z));
this.getBlockInfos()[i][j][k] = new BlockInfo(getWorld().getBlockAt(x, y, z));
z++;
noOfSavedBlocks++;
}
@ -67,7 +67,7 @@ public class Volume {
}
}
} catch (Exception e) {
this.war.getLogger().warning(war.str("Failed to save volume " + name + " blocks. " + e.getMessage()));
this.getWar().getLogger().warning(getWar().str("Failed to save volume " + getName() + " blocks. " + e.getMessage()));
}
return noOfSavedBlocks;
}
@ -75,15 +75,15 @@ public class Volume {
public int resetBlocks() {
int noOfResetBlocks = 0;
try {
if(hasTwoCorners() && blockInfos != null) {
if(hasTwoCorners() && getBlockInfos() != null) {
int x = getMinX();
for(int i = 0; i < getSizeX(); i++){
int y = getMinY();
for(int j = 0; j < getSizeY(); j++){
int z = getMinZ();
for(int k = 0;k < getSizeZ(); k++) {
BlockInfo oldBlockInfo = blockInfos[i][j][k];
Block currentBlock = world.getBlockAt(x, y, z);
BlockInfo oldBlockInfo = getBlockInfos()[i][j][k];
Block currentBlock = getWorld().getBlockAt(x, y, z);
if(currentBlock.getTypeID() != oldBlockInfo.getTypeID() ||
(currentBlock.getTypeID() == oldBlockInfo.getTypeID() && currentBlock.getData() != oldBlockInfo.getData()) ||
(currentBlock.getTypeID() == oldBlockInfo.getTypeID() && currentBlock.getData() == oldBlockInfo.getData() &&
@ -111,7 +111,7 @@ public class Volume {
}
}
} catch (Exception e) {
this.war.getLogger().warning(war.str("Failed to reset volume " + name + " blocks. " + e.getMessage()));
this.getWar().getLogger().warning(getWar().str("Failed to reset volume " + getName() + " blocks. " + e.getMessage()));
}
return noOfResetBlocks;
}
@ -187,7 +187,7 @@ public class Volume {
}
public boolean isSaved() {
return blockInfos != null;
return getBlockInfos() != null;
}
public BlockInfo[][][] getBlockInfos() {
@ -226,16 +226,16 @@ public class Volume {
scanner.next(",");
int z1 = scanner.nextInt();
scanner.next(";");
cornerOne = world.getBlockAt(x1, y1, z1);
cornerOne = getWorld().getBlockAt(x1, y1, z1);
int x2 = scanner.nextInt();
scanner.next(",");
int y2 = scanner.nextInt();
scanner.next(",");
int z2 = scanner.nextInt();
scanner.next(";");
cornerOne = world.getBlockAt(x2, y2, z2);
cornerOne = getWorld().getBlockAt(x2, y2, z2);
blockInfos = new BlockInfo[getSizeX()][getSizeY()][getSizeZ()];
setBlockInfos(new BlockInfo[getSizeX()][getSizeY()][getSizeZ()]);
for(int i = 0; i < getSizeX(); i++){
for(int j = 0; j < getSizeY(); j++) {
for(int k = 0; k < getSizeZ(); k++) {
@ -281,4 +281,16 @@ public class Volume {
z <= getMaxZ() && z >= getMinZ();
}
public void setBlockInfos(BlockInfo[][][] blockInfos) {
this.blockInfos = blockInfos;
}
public War getWar() {
return war;
}
public String getName() {
return name;
}
}