Clean up WorldEdit main class.

This commit is contained in:
sk89q 2014-04-04 21:02:56 -07:00
parent 8fb73347e4
commit 52b828ae17
3 changed files with 126 additions and 79 deletions

View File

@ -210,6 +210,7 @@ public class WorldEdit {
/** /**
* @deprecated Use {@link #getBlockRegistry()} and {@link BlockRegistry#parseFromInput(String, ParserContext)} * @deprecated Use {@link #getBlockRegistry()} and {@link BlockRegistry#parseFromInput(String, ParserContext)}
*/ */
@SuppressWarnings("deprecation")
@Deprecated @Deprecated
public BaseBlock getBlock(LocalPlayer player, String arg, boolean allAllowed) throws WorldEditException { public BaseBlock getBlock(LocalPlayer player, String arg, boolean allAllowed) throws WorldEditException {
return getBlock(player, arg, allAllowed, false); return getBlock(player, arg, allAllowed, false);
@ -218,6 +219,7 @@ public class WorldEdit {
/** /**
* @deprecated Use {@link #getBlockRegistry()} and {@link BlockRegistry#parseFromInput(String, ParserContext)} * @deprecated Use {@link #getBlockRegistry()} and {@link BlockRegistry#parseFromInput(String, ParserContext)}
*/ */
@SuppressWarnings("deprecation")
@Deprecated @Deprecated
public BaseBlock getBlock(LocalPlayer player, String arg, boolean allAllowed, boolean allowNoData) throws WorldEditException { public BaseBlock getBlock(LocalPlayer player, String arg, boolean allAllowed, boolean allowNoData) throws WorldEditException {
ParserContext context = new ParserContext(); ParserContext context = new ParserContext();
@ -232,6 +234,7 @@ public class WorldEdit {
/** /**
* @deprecated Use {@link #getBlockRegistry()} and {@link BlockRegistry#parseFromInput(String, ParserContext)} * @deprecated Use {@link #getBlockRegistry()} and {@link BlockRegistry#parseFromInput(String, ParserContext)}
*/ */
@SuppressWarnings("deprecation")
@Deprecated @Deprecated
public BaseBlock getBlock(LocalPlayer player, String id) throws WorldEditException { public BaseBlock getBlock(LocalPlayer player, String id) throws WorldEditException {
return getBlock(player, id, false); return getBlock(player, id, false);
@ -313,18 +316,17 @@ public class WorldEdit {
* Gets the path to a file. This method will check to see if the filename * Gets the path to a file. This method will check to see if the filename
* has valid characters and has an extension. It also prevents directory * has valid characters and has an extension. It also prevents directory
* traversal exploits by checking the root directory and the file directory. * traversal exploits by checking the root directory and the file directory.
* On success, a <code>java.io.File</code> object will be returned. * On success, a {@code java.io.File} object will be returned.
* *
* @param player * @param player the player
* @param dir sub-directory to look in * @param dir sub-directory to look in
* @param filename filename (user-submitted) * @param filename filename (user-submitted)
* @param defaultExt append an extension if missing one, null to not use * @param defaultExt append an extension if missing one, null to not use
* @param extensions list of extensions, null for any * @param extensions list of extensions, null for any
* @return * @return a file
* @throws FilenameException * @throws FilenameException thrown if the filename is invalid
*/ */
public File getSafeSaveFile(LocalPlayer player, File dir, String filename, public File getSafeSaveFile(LocalPlayer player, File dir, String filename, String defaultExt, String... extensions)
String defaultExt, String... extensions)
throws FilenameException { throws FilenameException {
return getSafeFile(player, dir, filename, defaultExt, extensions, true); return getSafeFile(player, dir, filename, defaultExt, extensions, true);
} }
@ -333,18 +335,17 @@ public class WorldEdit {
* Gets the path to a file. This method will check to see if the filename * Gets the path to a file. This method will check to see if the filename
* has valid characters and has an extension. It also prevents directory * has valid characters and has an extension. It also prevents directory
* traversal exploits by checking the root directory and the file directory. * traversal exploits by checking the root directory and the file directory.
* On success, a <code>java.io.File</code> object will be returned. * On success, a {@code java.io.File} object will be returned.
* *
* @param player * @param player the player
* @param dir sub-directory to look in * @param dir sub-directory to look in
* @param filename filename (user-submitted) * @param filename filename (user-submitted)
* @param defaultExt append an extension if missing one, null to not use * @param defaultExt append an extension if missing one, null to not use
* @param extensions list of extensions, null for any * @param extensions list of extensions, null for any
* @return * @return a file
* @throws FilenameException * @throws FilenameException thrown if the filename is invalid
*/ */
public File getSafeOpenFile(LocalPlayer player, File dir, String filename, public File getSafeOpenFile(LocalPlayer player, File dir, String filename, String defaultExt, String... extensions)
String defaultExt, String... extensions)
throws FilenameException { throws FilenameException {
return getSafeFile(player, dir, filename, defaultExt, extensions, false); return getSafeFile(player, dir, filename, defaultExt, extensions, false);
} }
@ -352,14 +353,14 @@ public class WorldEdit {
/** /**
* Get a safe path to a file. * Get a safe path to a file.
* *
* @param player * @param player the player
* @param dir * @param dir sub-directory to look in
* @param filename * @param filename filename (user-submitted)
* @param defaultExt * @param defaultExt append an extension if missing one, null to not use
* @param extensions * @param extensions list of extensions, null for any
* @param isSave * @param isSave true if the purpose is for saving
* @return * @return a file
* @throws FilenameException * @throws FilenameException thrown if the filename is invalid
*/ */
private File getSafeFile(LocalPlayer player, File dir, String filename, private File getSafeFile(LocalPlayer player, File dir, String filename,
String defaultExt, String[] extensions, boolean isSave) String defaultExt, String[] extensions, boolean isSave)
@ -433,7 +434,7 @@ public class WorldEdit {
/** /**
* Checks to see if the specified radius is within bounds. * Checks to see if the specified radius is within bounds.
* *
* @param radius * @param radius the radius
* @throws MaxRadiusException * @throws MaxRadiusException
*/ */
public void checkMaxRadius(double radius) throws MaxRadiusException { public void checkMaxRadius(double radius) throws MaxRadiusException {
@ -445,7 +446,7 @@ public class WorldEdit {
/** /**
* Checks to see if the specified brush radius is within bounds. * Checks to see if the specified brush radius is within bounds.
* *
* @param radius * @param radius the radius
* @throws MaxBrushRadiusException * @throws MaxBrushRadiusException
*/ */
public void checkMaxBrushRadius(double radius) throws MaxBrushRadiusException { public void checkMaxBrushRadius(double radius) throws MaxBrushRadiusException {
@ -458,8 +459,8 @@ public class WorldEdit {
* Get a file relative to the defined working directory. If the specified * Get a file relative to the defined working directory. If the specified
* path is absolute, then the working directory is not used. * path is absolute, then the working directory is not used.
* *
* @param path * @param path the subpath under the working directory
* @return * @return a working directory
*/ */
public File getWorkingDirectoryFile(String path) { public File getWorkingDirectoryFile(String path) {
File f = new File(path); File f = new File(path);
@ -470,29 +471,16 @@ public class WorldEdit {
return new File(getConfiguration().getWorkingDirectory(), path); return new File(getConfiguration().getWorkingDirectory(), path);
} }
/**
* Modulus, divisor-style.
*
* @param a
* @param n
* @return
*/
public static int divisorMod(int a, int n) {
return (int) (a - n * Math.floor(Math.floor(a) / n));
}
/** /**
* Get the direction vector for a player's direction. May return * Get the direction vector for a player's direction. May return
* null if a direction could not be found. * null if a direction could not be found.
* *
* @param player * @param player the player
* @param dirStr * @param dirStr the direction string
* @return * @return a direction vector
* @throws UnknownDirectionException * @throws UnknownDirectionException thrown if the direction is not known
*/ */
public Vector getDirection(LocalPlayer player, String dirStr) public Vector getDirection(LocalPlayer player, String dirStr) throws UnknownDirectionException {
throws UnknownDirectionException {
dirStr = dirStr.toLowerCase(); dirStr = dirStr.toLowerCase();
final PlayerDirection dir = getPlayerDirection(player, dirStr); final PlayerDirection dir = getPlayerDirection(player, dirStr);
@ -511,6 +499,15 @@ public class WorldEdit {
} }
} }
/**
* Get the direction vector for a player's direction. May return
* null if a direction could not be found.
*
* @param player the player
* @param dirStr the direction string
* @return a direction enum value
* @throws UnknownDirectionException thrown if the direction is not known
*/
private PlayerDirection getPlayerDirection(LocalPlayer player, String dirStr) throws UnknownDirectionException { private PlayerDirection getPlayerDirection(LocalPlayer player, String dirStr) throws UnknownDirectionException {
final PlayerDirection dir; final PlayerDirection dir;
@ -580,10 +577,10 @@ public class WorldEdit {
* Get diagonal direction vector for a player's direction. May return * Get diagonal direction vector for a player's direction. May return
* null if a direction could not be found. * null if a direction could not be found.
* *
* @param player * @param player the player
* @param dirStr * @param dirStr the direction string
* @return * @return a direction vector
* @throws UnknownDirectionException * @throws UnknownDirectionException thrown if the direction is not known
*/ */
public Vector getDiagonalDirection(LocalPlayer player, String dirStr) public Vector getDiagonalDirection(LocalPlayer player, String dirStr)
throws UnknownDirectionException { throws UnknownDirectionException {
@ -594,13 +591,12 @@ public class WorldEdit {
/** /**
* Get the flip direction for a player's direction. * Get the flip direction for a player's direction.
* *
* @param player * @param player the player
* @param dirStr * @param dirStr the direction string
* @return * @return a direction vector
* @throws UnknownDirectionException * @throws UnknownDirectionException thrown if the direction is not known
*/ */
public FlipDirection getFlipDirection(LocalPlayer player, String dirStr) public FlipDirection getFlipDirection(LocalPlayer player, String dirStr) throws UnknownDirectionException {
throws UnknownDirectionException {
final PlayerDirection dir = getPlayerDirection(player, dirStr); final PlayerDirection dir = getPlayerDirection(player, dirStr);
switch (dir) { switch (dir) {
@ -624,12 +620,10 @@ public class WorldEdit {
/** /**
* Flush a block bag's changes to a player. * Flush a block bag's changes to a player.
* *
* @param player * @param player the player
* @param editSession * @param editSession the edit session
*/ */
public void flushBlockBag(LocalPlayer player, public void flushBlockBag(LocalPlayer player, EditSession editSession) {
EditSession editSession) {
BlockBag blockBag = editSession.getBlockBag(); BlockBag blockBag = editSession.getBlockBag();
if (blockBag != null) { if (blockBag != null) {
@ -651,7 +645,7 @@ public class WorldEdit {
? type.getName() + " (" + id + ")" ? type.getName() + " (" + id + ")"
: id.toString()); : id.toString());
str.append(" [Amt: " + missingBlocks.get(id) + "]"); str.append(" [Amt: ").append(missingBlocks.get(id)).append("]");
++i; ++i;
@ -665,6 +659,8 @@ public class WorldEdit {
} }
/** /**
* Get the map of commands (internal usage only).
*
* @return the commands * @return the commands
*/ */
public Map<String, String> getCommands() { public Map<String, String> getCommands() {
@ -672,6 +668,8 @@ public class WorldEdit {
} }
/** /**
* Get the commands manager (internal usage only).
*
* @return the commands * @return the commands
*/ */
public CommandsManager<LocalPlayer> getCommandsManager() { public CommandsManager<LocalPlayer> getCommandsManager() {
@ -716,8 +714,8 @@ public class WorldEdit {
/** /**
* Called on arm swing. * Called on arm swing.
* *
* @param player * @param player the player
* @return * @return true if the swing was handled
*/ */
public boolean handleArmSwing(LocalPlayer player) { public boolean handleArmSwing(LocalPlayer player) {
if (player.getItemInHand() == getConfiguration().navigationWand) { if (player.getItemInHand() == getConfiguration().navigationWand) {
@ -754,8 +752,8 @@ public class WorldEdit {
/** /**
* Called on right click (not on a block). * Called on right click (not on a block).
* *
* @param player * @param player the player
* @return * @return true if the right click was handled
*/ */
public boolean handleRightClick(LocalPlayer player) { public boolean handleRightClick(LocalPlayer player) {
if (player.getItemInHand() == getConfiguration().navigationWand) { if (player.getItemInHand() == getConfiguration().navigationWand) {
@ -790,8 +788,8 @@ public class WorldEdit {
/** /**
* Called on right click. * Called on right click.
* *
* @param player * @param player the player
* @param clicked * @param clicked the clicked block
* @return false if you want the action to go through * @return false if you want the action to go through
*/ */
public boolean handleBlockRightClick(LocalPlayer player, WorldVector clicked) { public boolean handleBlockRightClick(LocalPlayer player, WorldVector clicked) {
@ -828,8 +826,8 @@ public class WorldEdit {
/** /**
* Called on left click. * Called on left click.
* *
* @param player * @param player the player
* @param clicked * @param clicked the clicked block
* @return false if you want the action to go through * @return false if you want the action to go through
*/ */
public boolean handleBlockLeftClick(LocalPlayer player, WorldVector clicked) { public boolean handleBlockLeftClick(LocalPlayer player, WorldVector clicked) {
@ -889,9 +887,9 @@ public class WorldEdit {
/** /**
* Executes a WorldEdit script. * Executes a WorldEdit script.
* *
* @param player * @param player the player
* @param f * @param f the script file to execute
* @param args * @param args arguments for the script
* @throws WorldEditException * @throws WorldEditException
*/ */
public void runScript(LocalPlayer player, File f, String[] args) throws WorldEditException { public void runScript(LocalPlayer player, File f, String[] args) throws WorldEditException {
@ -912,8 +910,7 @@ public class WorldEdit {
InputStream file; InputStream file;
if (!f.exists()) { if (!f.exists()) {
file = WorldEdit.class.getResourceAsStream( file = WorldEdit.class.getResourceAsStream("craftscripts/" + filename);
"craftscripts/" + filename);
if (file == null) { if (file == null) {
player.printError("Script does not exist: " + filename); player.printError("Script does not exist: " + filename);
@ -1018,7 +1015,7 @@ public class WorldEdit {
/** /**
* Get the version. * Get the version.
* *
* @return * @return the version of WorldEdit
*/ */
public static String getVersion() { public static String getVersion() {
if (version != null) { if (version != null) {
@ -1044,7 +1041,13 @@ public class WorldEdit {
return version; return version;
} }
/**
* Set the version of WorldEdit.
*
* @param version the version
*/
public static void setVersion(String version) { public static void setVersion(String version) {
WorldEdit.version = version; WorldEdit.version = version;
} }
} }

View File

@ -19,19 +19,22 @@
package com.sk89q.worldedit.command; package com.sk89q.worldedit.command;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Set;
import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.minecraft.util.commands.Logging; import com.sk89q.minecraft.util.commands.Logging;
import static com.sk89q.minecraft.util.commands.Logging.LogMode.*;
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
import com.sk89q.worldedit.math.MathUtils;
import com.sk89q.worldedit.world.storage.LegacyChunkStore; import com.sk89q.worldedit.world.storage.LegacyChunkStore;
import com.sk89q.worldedit.world.storage.McRegionChunkStore; import com.sk89q.worldedit.world.storage.McRegionChunkStore;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Set;
import static com.sk89q.minecraft.util.commands.Logging.LogMode.REGION;
/** /**
* Chunk tools. * Chunk tools.
* *
@ -59,8 +62,8 @@ public class ChunkCommands {
int chunkX = (int) Math.floor(pos.getBlockX() / 16.0); int chunkX = (int) Math.floor(pos.getBlockX() / 16.0);
int chunkZ = (int) Math.floor(pos.getBlockZ() / 16.0); int chunkZ = (int) Math.floor(pos.getBlockZ() / 16.0);
String folder1 = Integer.toString(WorldEdit.divisorMod(chunkX, 64), 36); String folder1 = Integer.toString(MathUtils.divisorMod(chunkX, 64), 36);
String folder2 = Integer.toString(WorldEdit.divisorMod(chunkZ, 64), 36); String folder2 = Integer.toString(MathUtils.divisorMod(chunkZ, 64), 36);
String filename = "c." + Integer.toString(chunkX, 36) String filename = "c." + Integer.toString(chunkX, 36)
+ "." + Integer.toString(chunkZ, 36) + ".dat"; + "." + Integer.toString(chunkZ, 36) + ".dat";

View File

@ -0,0 +1,41 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.math;
/**
* Various math utility methods.
*/
public final class MathUtils {
private MathUtils() {
}
/**
* Modulus, divisor-style.
*
* @param a a
* @param n n
* @return the modulus
*/
public static int divisorMod(int a, int n) {
return (int) (a - n * Math.floor(Math.floor(a) / n));
}
}