Reduced code duplication in the WorldEdit.get*Direction methods.

This commit is contained in:
TomyLobo 2011-10-25 01:03:37 +02:00
parent 3962e6c9d8
commit 2d654b59c5

View File

@ -29,6 +29,7 @@ import javax.script.ScriptException;
import com.sk89q.minecraft.util.commands.*; import com.sk89q.minecraft.util.commands.*;
import com.sk89q.util.StringUtil; import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.CuboidClipboard.FlipDirection;
import com.sk89q.worldedit.bags.BlockBag; import com.sk89q.worldedit.bags.BlockBag;
import com.sk89q.worldedit.blocks.*; import com.sk89q.worldedit.blocks.*;
import com.sk89q.worldedit.commands.*; import com.sk89q.worldedit.commands.*;
@ -764,45 +765,72 @@ public class WorldEdit {
dirStr = dirStr.toLowerCase(); dirStr = dirStr.toLowerCase();
if (dirStr.equals("me")) { final PlayerDirection dir = getPlayerDirection(player, dirStr);
final PlayerDirection dir = player.getCardinalDirection();
switch (dir) {
case WEST:
case EAST:
case SOUTH:
case NORTH:
case UP:
case DOWN:
dirStr = dir.name().toLowerCase();
break;
default: switch (dir) {
throw new UnknownDirectionException(dir.name()); case WEST:
} case EAST:
case SOUTH:
case NORTH:
case UP:
case DOWN:
return dir.vector();
default:
throw new UnknownDirectionException(dir.name());
} }
}
private PlayerDirection getPlayerDirection(LocalPlayer player, String dirStr) throws UnknownDirectionException {
final PlayerDirection dir;
switch (dirStr.charAt(0)) { switch (dirStr.charAt(0)) {
case 'w': case 'w':
return new Vector(0, 0, 1); dir = PlayerDirection.WEST;
break;
case 'e': case 'e':
return new Vector(0, 0, -1); dir = PlayerDirection.EAST;
break;
case 's': case 's':
return new Vector(1, 0, 0); if (dirStr.indexOf('w') > 0) {
return PlayerDirection.SOUTH_WEST;
}
if (dirStr.indexOf('e') > 0) {
return PlayerDirection.SOUTH_EAST;
}
dir = PlayerDirection.SOUTH;
break;
case 'n': case 'n':
return new Vector(-1, 0, 0); if (dirStr.indexOf('w') > 0) {
return PlayerDirection.NORTH_WEST;
}
if (dirStr.indexOf('e') > 0) {
return PlayerDirection.NORTH_EAST;
}
dir = PlayerDirection.NORTH;
break;
case 'u': case 'u':
return new Vector(0, 1, 0); dir = PlayerDirection.UP;
break;
case 'd': case 'd':
return new Vector(0, -1, 0); dir = PlayerDirection.DOWN;
break;
case 'm':
dir = player.getCardinalDirection(0);
break;
default: default:
throw new UnknownDirectionException(dirStr); throw new UnknownDirectionException(dirStr);
} }
return dir;
} }
/** /**
@ -814,53 +842,10 @@ public class WorldEdit {
* @return * @return
* @throws UnknownDirectionException * @throws UnknownDirectionException
*/ */
public Vector getDiagonalDirection( LocalPlayer player, String dirStr ) public Vector getDiagonalDirection(LocalPlayer player, String dirStr)
throws UnknownDirectionException { throws UnknownDirectionException {
dirStr = dirStr.toLowerCase(); return getPlayerDirection(player, dirStr.toLowerCase()).vector();
if (dirStr.equals("me")) {
dirStr = player.getCardinalDirection().name().toLowerCase();
}
switch (dirStr.charAt(0)) {
case 'w':
return new Vector(0, 0, 1);
case 'e':
return new Vector(0, 0, -1);
case 's':
if (dirStr.indexOf('w') > 0) {
return new Vector(1, 0, 1);
}
if (dirStr.indexOf('e') > 0) {
return new Vector(1, 0, -1);
}
return new Vector(1, 0, 0);
case 'n':
if (dirStr.indexOf('w') > 0) {
return new Vector(-1, 0, 1);
}
if (dirStr.indexOf('e') > 0) {
return new Vector(-1, 0, -1);
}
return new Vector(-1, 0, 0);
case 'u':
return new Vector(0, 1, 0);
case 'd':
return new Vector(0, -1, 0);
default:
throw new UnknownDirectionException(dirStr);
}
} }
/** /**
@ -871,45 +856,25 @@ public class WorldEdit {
* @return * @return
* @throws UnknownDirectionException * @throws UnknownDirectionException
*/ */
public CuboidClipboard.FlipDirection getFlipDirection( public FlipDirection getFlipDirection(LocalPlayer player, String dirStr)
LocalPlayer player, String dirStr)
throws UnknownDirectionException { throws UnknownDirectionException {
if (dirStr.equals("me")) { final PlayerDirection dir = getPlayerDirection(player, dirStr);
final PlayerDirection dir = player.getCardinalDirection(); switch (dir) {
switch (dir) { case WEST:
case WEST: case EAST:
case EAST: return FlipDirection.WEST_EAST;
return CuboidClipboard.FlipDirection.WEST_EAST;
case NORTH: case NORTH:
case SOUTH: case SOUTH:
return CuboidClipboard.FlipDirection.NORTH_SOUTH; return FlipDirection.NORTH_SOUTH;
case UP: case UP:
case DOWN: case DOWN:
return CuboidClipboard.FlipDirection.UP_DOWN; return FlipDirection.UP_DOWN;
default:
throw new UnknownDirectionException(dir.name());
}
}
switch (dirStr.charAt(0)) {
case 'w':
case 'e':
return CuboidClipboard.FlipDirection.WEST_EAST;
case 'n':
case 's':
return CuboidClipboard.FlipDirection.NORTH_SOUTH;
case 'u':
case 'd':
return CuboidClipboard.FlipDirection.UP_DOWN;
default: default:
throw new UnknownDirectionException(dirStr); throw new UnknownDirectionException(dir.name());
} }
} }