Merge pull request #34 from craftinc/refactor/cleanup

Code formatting
This commit is contained in:
Tobias Ottenweller 2016-12-31 12:05:14 +01:00 committed by GitHub
commit 927b70fd2d
37 changed files with 1749 additions and 2094 deletions

View File

@ -27,8 +27,7 @@ import org.bukkit.configuration.serialization.ConfigurationSerializable;
import java.util.*;
public class Gate implements ConfigurationSerializable
{
public class Gate implements ConfigurationSerializable {
protected Location location; /* saving both location and gateBlockLocations is redundant but makes it easy to allow players to reshape gates */
protected Set<Location> gateBlockLocations = new HashSet<Location>(); /* Locations of the blocks inside the gate */
protected Set<Block> gateFrameBlocks = new HashSet<Block>();
@ -44,46 +43,40 @@ public class Gate implements ConfigurationSerializable
/**
* You should never create two gates with the same 'id'. Also see 'setId(String id)'.
*
* @param id This parameter must not be 'null'. An exception will be thrown otherwise!
*/
public Gate(final String id)
{
public Gate(final String id) {
setId(id);
}
public String toString()
{
public String toString() {
return super.toString() + " " + this.getId();
}
/**
*
* @return This method might return a 'null' data.
*/
public Location getLocation()
{
public Location getLocation() {
return location;
}
/**
*
* @param location Supplying 'null' is permitted.
* @throws Exception Will throw an exception if the gate is open and an invalid (no gate frame) location is
* supplied. Note that the supplied 'location' will be set even if an exception is thrown. Note that this
* gate will be closed if an exception is thrown.
*/
public void setLocation(final Location location) throws Exception
{
public void setLocation(final Location location) throws Exception {
this.location = location;
if (isOpen) {
findPortalBlocks();
validate();
}
else {
} else {
this.gateBlockLocations = new HashSet<Location>();
this.gateFrameBlocks = new HashSet<Block>();
}
@ -91,35 +84,29 @@ public class Gate implements ConfigurationSerializable
/**
*
* @return This method might return a 'null' value.
*/
public Location getExit()
{
public Location getExit() {
return exit;
}
/**
*
* @param exit Supplying 'null' is permitted.
* @throws Exception An exception will be thrown if 'null' data is supplied and this gate is open. Note that the
* supplied 'exit' will be set even if an exception is thrown. Note that this gate will be closed if an
* exception is thrown.
*/
public void setExit(final Location exit) throws Exception
{
public void setExit(final Location exit) throws Exception {
this.exit = exit;
validate();
}
/**
*
* @return This method will never return 'null'.
*/
public String getId()
{
public String getId() {
return id;
}
@ -127,10 +114,10 @@ public class Gate implements ConfigurationSerializable
/**
* Every gate should have an unique 'id'. You should therefore check if another gate with the same 'id' exists.
* Note that this method will not check if another gate with the same 'id' exists!
*
* @param id This parameter must not be 'null'. An exception will be thrown otherwise!
*/
public void setId(final String id)
{
public void setId(final String id) {
if (id == null) {
throw new IllegalArgumentException("gate 'id' cannot be 'null'");
}
@ -139,27 +126,23 @@ public class Gate implements ConfigurationSerializable
}
public boolean isHidden()
{
public boolean isHidden() {
return isHidden;
}
public void setHidden(boolean isHidden) throws Exception
{
public void setHidden(boolean isHidden) throws Exception {
this.isHidden = isHidden;
this.validate();
}
public boolean isOpen()
{
public boolean isOpen() {
return isOpen;
}
public void setOpen(boolean isOpen) throws Exception
{
public void setOpen(boolean isOpen) throws Exception {
if (isOpen && !this.isOpen) {
findPortalBlocks();
}
@ -169,41 +152,33 @@ public class Gate implements ConfigurationSerializable
}
public void setAllowsVehicles(boolean allowsVehicles)
{
public void setAllowsVehicles(boolean allowsVehicles) {
this.allowsVehicles = allowsVehicles;
}
public boolean getAllowsVehicles()
{
public boolean getAllowsVehicles() {
return this.allowsVehicles;
}
/**
*
* @return Will never return 'null' but might return an empty Set.
*/
public Set<Location> getGateBlockLocations()
{
public Set<Location> getGateBlockLocations() {
return gateBlockLocations;
}
/**
*
* @return Will never return 'null' but might return an empty Set.
*/
public Set<Block> getGateFrameBlocks()
{
public Set<Block> getGateFrameBlocks() {
return gateFrameBlocks;
}
protected void findPortalBlocks()
{
protected void findPortalBlocks() {
gateBlockLocations = new HashSet<Location>();
Set<Block> gateBlocks = FloodUtil.getGatePortalBlocks(location.getBlock());
@ -220,8 +195,7 @@ public class Gate implements ConfigurationSerializable
/**
* Checks if values attributes do add up; will close gate on wrong values.
*/
public void validate() throws Exception
{
public void validate() throws Exception {
if (!isOpen) {
return;
}
@ -283,8 +257,7 @@ public class Gate implements ConfigurationSerializable
@SuppressWarnings("unchecked")
public Gate(Map<String, Object> map)
{
public Gate(Map<String, Object> map) {
try {
id = map.get(idKey).toString().toLowerCase();
@ -316,8 +289,7 @@ public class Gate implements ConfigurationSerializable
}
gateFrameBlocks = FloodUtil.getFrameWithLocations(gateBlockLocations);
}
catch (Exception e) {
} catch (Exception e) {
Plugin.log("ERROR: Failed to load gate '" + id + "'! (" + e.getMessage() + ")");
Plugin.log("NOTE: This gate will be removed from 'gates.yml' and added to 'invalid_gates.yml'!");
@ -326,15 +298,13 @@ public class Gate implements ConfigurationSerializable
try {
validate(); // make sure to not write invalid stuff to disk
}
catch (Exception e) {
} catch (Exception e) {
Plugin.log("The loaded gate " + this.getId() + " seems to be not valid: " + e.getMessage());
}
}
public Map<String, Object> serialize()
{
public Map<String, Object> serialize() {
Map<String, Object> retVal = new HashMap<String, Object>();
retVal.put(idKey, id);

View File

@ -19,8 +19,7 @@ package de.craftinc.gates;
import java.util.Map;
public interface GateChangeListener
{
public interface GateChangeListener {
public static final String newGate = "GateChangeListener-newGate"; // value will be null
public static final String removedGate = "GateChangeListener-removedGate"; // value will be null
public static final String changedID = "GateChangeListener-changedID"; // value will be the old ID

View File

@ -35,8 +35,7 @@ import de.craftinc.gates.util.SimpleChunk;
import de.craftinc.gates.util.SimpleLocation;
public class GatesManager
{
public class GatesManager {
protected File gatesConfigFile;
protected FileConfiguration gatesConfig;
protected static final String gatesPath = "gates"; // path to gates inside the yaml file
@ -57,26 +56,22 @@ public class GatesManager
protected Set<GateChangeListener> changeListeners = new HashSet<GateChangeListener>();
public void addGateChangeListener(GateChangeListener listener)
{
public void addGateChangeListener(GateChangeListener listener) {
this.changeListeners.add(listener);
}
public void removeGateChangeListener(GateChangeListener listener)
{
public void removeGateChangeListener(GateChangeListener listener) {
this.changeListeners.remove(listener);
}
public Gate getGateWithId(final String id)
{
public Gate getGateWithId(final String id) {
return gatesById.get(id.toLowerCase());
}
public Set<Gate> getNearbyGates(final Chunk chunk)
{
public Set<Gate> getNearbyGates(final Chunk chunk) {
SimpleChunk simpleChunk = new SimpleChunk(chunk);
return gatesByChunk.get(simpleChunk);
}
@ -84,11 +79,11 @@ public class GatesManager
/**
* Returns the closest gate.
*
* @param location The location at which to look for a gate.
* @return Might return null if there are no nearby gates.
*/
public Gate getNearestGate(final Location location)
{
public Gate getNearestGate(final Location location) {
Set<Gate> nearbyGates = getNearbyGates(location.getChunk());
if (nearbyGates == null) {
@ -111,22 +106,19 @@ public class GatesManager
}
public Gate getGateAtLocation(final Location location)
{
public Gate getGateAtLocation(final Location location) {
SimpleLocation simpleLocation = new SimpleLocation(location);
return gatesByLocation.get(simpleLocation);
}
public Gate getGateAtFrameLocation(final Location location)
{
public Gate getGateAtFrameLocation(final Location location) {
SimpleLocation simpleLocation = new SimpleLocation(location);
return gatesByFrameLocation.get(simpleLocation);
}
public void saveGatesToDisk()
{
public void saveGatesToDisk() {
if (storageFileIsInvalid) {
Plugin.log(Level.SEVERE, "ERROR: Not saving gates to disk. Storage file is invalid or corrupted!");
return;
@ -138,8 +130,7 @@ public class GatesManager
try {
gatesConfig.save(gatesConfigFile);
Plugin.log("Saved gates to disk.");
}
catch (IOException e) {
} catch (IOException e) {
Plugin.log(Level.SEVERE, "ERROR: Could not save gates to disk.");
e.printStackTrace();
}
@ -147,8 +138,7 @@ public class GatesManager
@SuppressWarnings("unchecked")
public boolean loadGatesFromDisk()
{
public boolean loadGatesFromDisk() {
this.gatesConfigFile = new File(Plugin.getPlugin().getDataFolder(), "gates.yml");
if (!this.gatesConfigFile.exists()) {
@ -158,8 +148,7 @@ public class GatesManager
if (isNew) {
Plugin.log(Level.FINEST, "Created gate storage file.");
}
}
catch (IOException e) {
} catch (IOException e) {
this.storageFileIsInvalid = true;
Plugin.log(Level.SEVERE, "Cannot create gate storage file! No gates will be persisted.");
return false;
@ -170,8 +159,7 @@ public class GatesManager
try {
this.gatesConfig.load(this.gatesConfigFile);
}
catch (Exception e) {
} catch (Exception e) {
this.storageFileIsInvalid = true;
Plugin.log(Level.SEVERE, "Gate file on disk is invalid. No gates loaded. Plugin will be disabled! (" + Arrays.toString(e.getStackTrace()) + ")");
return false;
@ -195,12 +183,11 @@ public class GatesManager
for (Gate g : this.gates) {
try {
g.validate();
}
catch (Exception e) {
} catch (Exception e) {
try {
g.setOpen(false);
} catch (Exception ignored) {
}
catch (Exception ignored) { }
Plugin.log(Level.FINER, "closed gate '" + g.getId() + "' reason: " + e.getMessage());
}
@ -234,8 +221,7 @@ public class GatesManager
}
protected int getChunkRadius()
{
protected int getChunkRadius() {
if (this.chunkRadius == 0) {
this.chunkRadius = Plugin.getPlugin().getConfig().getInt(ConfigurationUtil.confPlayerGateBlockUpdateRadiusKey);
this.chunkRadius = this.chunkRadius >> 4;
@ -245,8 +231,7 @@ public class GatesManager
}
protected void fillGatesById()
{
protected void fillGatesById() {
gatesById = new HashMap<String, Gate>((int) (gates.size() * 1.25));
for (Gate g : gates) {
@ -255,8 +240,7 @@ public class GatesManager
}
protected void fillGatesByChunk()
{
protected void fillGatesByChunk() {
HashSet<SimpleChunk> chunksUsedByGates = new HashSet<SimpleChunk>();
for (Gate g : gates) {
@ -286,8 +270,7 @@ public class GatesManager
}
protected void fillGatesByLocation()
{
protected void fillGatesByLocation() {
Set<Location> gateBlocks = new HashSet<Location>();
for (Gate g : gates) {
@ -312,8 +295,7 @@ public class GatesManager
}
protected void fillGatesByFrameLocation()
{
protected void fillGatesByFrameLocation() {
int numFrameBlocks = 0;
for (Gate g : gates) {
@ -328,20 +310,17 @@ public class GatesManager
}
protected void removeGateById(final String id)
{
protected void removeGateById(final String id) {
gatesById.remove(id);
}
protected void addGateWithId(final Gate g)
{
protected void addGateWithId(final Gate g) {
gatesById.put(g.getId(), g);
}
protected void removeGateByLocation(final Set<Location> gateBlocks)
{
protected void removeGateByLocation(final Set<Location> gateBlocks) {
if (gateBlocks != null) {
for (Location l : gateBlocks) {
@ -356,8 +335,7 @@ public class GatesManager
}
protected void removeGateByFrameLocation(final Set<Block> gateFrameBlocks)
{
protected void removeGateByFrameLocation(final Set<Block> gateFrameBlocks) {
if (gateFrameBlocks != null) {
for (Block block : gateFrameBlocks) {
@ -368,8 +346,7 @@ public class GatesManager
}
protected void addGateByLocations(final Gate g)
{
protected void addGateByLocations(final Gate g) {
for (Location l : g.getGateBlockLocations()) {
SimpleLocation sl = new SimpleLocation(l);
@ -381,8 +358,7 @@ public class GatesManager
}
protected void addGateByFrameLocations(final Gate g)
{
protected void addGateByFrameLocations(final Gate g) {
for (Block block : g.getGateFrameBlocks()) {
SimpleLocation sl = new SimpleLocation(block.getLocation());
gatesByFrameLocation.put(sl, g);
@ -390,8 +366,7 @@ public class GatesManager
}
protected void removeGateFromChunk(final Gate g, final Location l)
{
protected void removeGateFromChunk(final Gate g, final Location l) {
if (l != null) {
Chunk c = l.getChunk();
@ -415,8 +390,7 @@ public class GatesManager
}
protected void addGateByChunk(final Gate g)
{
protected void addGateByChunk(final Gate g) {
Location gateLocation = g.getLocation();
if (gateLocation != null) {
@ -445,8 +419,7 @@ public class GatesManager
}
public void storeInvalidGate(Map<String, Object> map)
{
public void storeInvalidGate(Map<String, Object> map) {
File invalidGatesFile = new File(Plugin.getPlugin().getDataFolder(), "invalid_gates.yml");
Boolean invalidGatesFileExists = invalidGatesFile.exists();
@ -485,22 +458,19 @@ public class GatesManager
fileWriter.write("\t\t" + k + ": " + v.toString() + "\n");
}
}
else {
} else {
fileWriter.write(value.toString() + "\n");
}
}
fileWriter.close();
}
catch (IOException e) {
} catch (IOException e) {
Plugin.log("ERROR: Could not save invalid gates to disk. Reason: \n" + Arrays.toString(e.getStackTrace()));
}
}
public void handleGateIdChange(final Gate g, final String oldId)
{
public void handleGateIdChange(final Gate g, final String oldId) {
this.removeGateById(oldId);
this.addGateWithId(g);
@ -516,8 +486,7 @@ public class GatesManager
public void handleGateLocationChange(final Gate g,
final Location oldLocation,
final Set<Location> oldGateBlockLocations,
final Set<Block> oldGateFrameBlocks)
{
final Set<Block> oldGateFrameBlocks) {
this.removeGateFromChunk(g, oldLocation);
this.addGateByChunk(g);
@ -536,8 +505,7 @@ public class GatesManager
}
public void handleGateExitChange(final Gate g, final Location oldExit)
{
public void handleGateExitChange(final Gate g, final Location oldExit) {
// nothing to do
Map<String, Object> changeSet = new HashMap<String, Object>();
@ -549,8 +517,7 @@ public class GatesManager
}
public void handleNewGate(final Gate g)
{
public void handleNewGate(final Gate g) {
this.gates.add(g);
this.addGateByChunk(g);
@ -568,8 +535,7 @@ public class GatesManager
}
public void handleDeletion(final Gate g)
{
public void handleDeletion(final Gate g) {
this.gates.remove(g);
this.removeGateById(g.getId());
@ -586,14 +552,12 @@ public class GatesManager
}
public boolean gateExists(final String id)
{
public boolean gateExists(final String id) {
return gatesById.containsKey(id.toLowerCase());
}
public List<Gate> allGates ()
{
public List<Gate> allGates() {
return gates;
}
}

View File

@ -38,8 +38,7 @@ import de.craftinc.gates.commands.*;
import org.mcstats.Metrics;
public class Plugin extends JavaPlugin
{
public class Plugin extends JavaPlugin {
public static final String permissionInfo = "craftincgates.info";
public static final String permissionManage = "craftincgates.manage";
public static final String permissionUse = "craftincgates.use";
@ -59,54 +58,45 @@ public class Plugin extends JavaPlugin
protected BlockBreakListener blockBreakListener = new BlockBreakListener();
public Plugin()
{
public Plugin() {
instance = this;
}
public static Plugin getPlugin()
{
public static Plugin getPlugin() {
return instance;
}
public GatesManager getGatesManager()
{
public GatesManager getGatesManager() {
return gatesManager;
}
@Override
public void onLoad()
{
public void onLoad() {
ConfigurationSerialization.registerClass(Gate.class);
}
protected void setupPermissions()
{
protected void setupPermissions() {
if (getServer().getPluginManager().getPlugin("Vault") == null) {
return;
}
RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
if (rsp != null)
{
if (rsp != null) {
log("Using permission provider provided by Vault.");
permission = rsp.getProvider();
}
else
{
} else {
log("Not using setup permission provider provided by Vault.");
}
}
@Override
public void onDisable()
{
public void onDisable() {
// Save gates
gatesManager.saveGatesToDisk();
@ -115,14 +105,12 @@ public class Plugin extends JavaPlugin
@Override
public void onEnable()
{
public void onEnable() {
// Setup Metrics
try {
Metrics metrics = new Metrics(this);
metrics.start();
}
catch (IOException e) {
} catch (IOException e) {
log("Failed to start metrics!");
}
@ -159,16 +147,14 @@ public class Plugin extends JavaPlugin
if (success) {
log("Enabled");
}
else {
} else {
PluginManager pm = this.getServer().getPluginManager();
pm.disablePlugin(this);
}
}
protected void registerEventListeners()
{
protected void registerEventListeners() {
PluginManager pm = this.getServer().getPluginManager();
pm.registerEvents(this.moveListener, this);
@ -187,8 +173,7 @@ public class Plugin extends JavaPlugin
// Commands
// -------------------------------------------- //
public String getBaseCommand()
{
public String getBaseCommand() {
if (this.baseCommand != null)
return this.baseCommand;
@ -201,18 +186,15 @@ public class Plugin extends JavaPlugin
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
{
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
List<String> parameters = new ArrayList<String>(Arrays.asList(args));
this.handleCommand(sender, parameters);
return true;
}
public void handleCommand(CommandSender sender, List<String> parameters)
{
if (parameters.size() == 0)
{
public void handleCommand(CommandSender sender, List<String> parameters) {
if (parameters.size() == 0) {
this.commands.get(0).execute(sender, parameters);
return;
}
@ -220,10 +202,8 @@ public class Plugin extends JavaPlugin
String commandName = parameters.get(0).toLowerCase();
parameters.remove(0);
for (BaseCommand fcommand : this.commands)
{
if (fcommand.getAliases().contains(commandName))
{
for (BaseCommand fcommand : this.commands) {
if (fcommand.getAliases().contains(commandName)) {
fcommand.execute(sender, parameters);
return;
}
@ -237,14 +217,12 @@ public class Plugin extends JavaPlugin
/*
* Logging
*/
public static void log(String msg)
{
public static void log(String msg) {
log(Level.INFO, msg);
}
public static void log(Level level, String msg)
{
public static void log(Level level, String msg) {
Logger.getLogger("Minecraft").log(level, "[" + instance.getDescription().getFullName() + "] " + msg);
}

View File

@ -22,6 +22,7 @@ import java.util.List;
import de.craftinc.gates.util.ConfigurationUtil;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import de.craftinc.gates.Gate;
@ -29,8 +30,8 @@ import de.craftinc.gates.GatesManager;
import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.TextUtil;
public abstract class BaseCommand
{
public abstract class BaseCommand {
protected List<String> aliases = new ArrayList<String>();
protected List<String> requiredParameters = new ArrayList<String>();
protected List<String> optionalParameters = new ArrayList<String>();
@ -70,11 +71,16 @@ public abstract class BaseCommand
this.perform();
if (this.shouldPersistToDisk && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confSaveOnChangesKey)) {
if (this.shouldPersistToDisk && getSaveOnChanges()) {
Plugin.getPlugin().getGatesManager().saveGatesToDisk();
}
}
private boolean getSaveOnChanges() {
FileConfiguration config = Plugin.getPlugin().getConfig();
return config.getBoolean(ConfigurationUtil.confSaveOnChangesKey);
}
abstract protected void perform();
@ -91,8 +97,7 @@ public abstract class BaseCommand
}
protected boolean validateCall()
{
protected boolean validateCall() {
boolean allParametersThere = parameters.size() >= requiredParameters.size();
boolean senderIsPlayer = this.sender instanceof Player;
boolean hasGateParameter = false;
@ -107,20 +112,17 @@ public abstract class BaseCommand
if (this.senderMustBePlayer && !senderIsPlayer) {
sendMessage(ChatColor.RED + "This command can only be used by ingame players.");
valid = false;
}
else {
} else {
if (!allParametersThere) {
sendMessage(ChatColor.RED + "Some parameters are missing! " + ChatColor.AQUA + "Usage: " + this.getUsageTemplate(true));
valid = false;
}
else if ((!senderHasPermission && this.hasGateParam) ||
} else if ((!senderHasPermission && this.hasGateParam) ||
(!senderHasPermission) ||
(this.hasGateParam && !hasGateParameter)) {
sendMessage(ChatColor.RED + "You either provided a invalid gate or do not have permission to " + this.helpDescription.toLowerCase());
valid = false;
}
else {
} else {
valid = true;
}
}
@ -129,14 +131,12 @@ public abstract class BaseCommand
}
protected boolean setGateUsingParameter(String param)
{
protected boolean setGateUsingParameter(String param) {
GatesManager gateManager = Plugin.getPlugin().getGatesManager();
if (!gateManager.gateExists(param)) {
return false;
}
else {
} else {
gate = gateManager.getGateWithId(param);
return true;
}
@ -146,8 +146,7 @@ public abstract class BaseCommand
/**
* This will return false if a gate is required for this command but this.gate == null.
*/
protected boolean hasPermission()
{
protected boolean hasPermission() {
if (Plugin.getPermission() == null) { // fallback - use the standard bukkit permission system
return this.sender.hasPermission(this.requiredPermission);
}
@ -165,24 +164,19 @@ public abstract class BaseCommand
if (this.hasGateParam) {
hasPermission = this.hasPermissionAtGateLocationAndExit(p);
}
else {
} else {
hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
}
}
else if (this.requiredPermission.equals(Plugin.permissionUse) ) {
} else if (this.requiredPermission.equals(Plugin.permissionUse)) {
hasPermission = this.hasPermissionAtGateLocationAndExit(p);
}
else if (this.requiredPermission.equals(Plugin.permissionManage)) {
} else if (this.requiredPermission.equals(Plugin.permissionManage)) {
if (this.needsPermissionAtCurrentLocation && this.hasGateParam) {
boolean hasPersmissionAtCurrentLocation = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
hasPermission = hasPersmissionAtCurrentLocation && this.hasPermissionAtGateLocationAndExit(p);
}
else if (this.needsPermissionAtCurrentLocation) {
} else if (this.needsPermissionAtCurrentLocation) {
hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
}
else {
} else {
hasPermission = this.hasPermissionAtGateLocationAndExit(p);
}
}
@ -191,9 +185,8 @@ public abstract class BaseCommand
}
protected boolean hasPermissionAtGateLocationAndExit(Player p)
{
if (this.gate == null || p == null) { // make sure we don't run into a nullpointer exception
protected boolean hasPermissionAtGateLocationAndExit(Player p) {
if (this.gate == null || p == null) {
return false;
}
@ -207,8 +200,7 @@ public abstract class BaseCommand
// -------------------------------------------- //
// Help and usage description
// -------------------------------------------- //
protected String getUsageTemplate(boolean withColor, boolean withDescription)
{
protected String getUsageTemplate(boolean withColor, boolean withDescription) {
String ret = "";
if (withColor) {
@ -239,14 +231,12 @@ public abstract class BaseCommand
if (withColor) {
ret += ChatColor.YELLOW;
}
ret += this.helpDescription;
}
return ret;
}
protected String getUsageTemplate(boolean withColor)
{
protected String getUsageTemplate(boolean withColor) {
return getUsageTemplate(withColor, false);
}
}

View File

@ -21,10 +21,9 @@ import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
public abstract class BaseLocationCommand extends BaseCommand
{
protected Location getValidPlayerLocation()
{
public abstract class BaseLocationCommand extends BaseCommand {
protected Location getValidPlayerLocation() {
// The player might stand in a half block or a sign or whatever
// Therefore we load some extra locations and blocks
Block playerBlock = player.getLocation().getBlock();
@ -32,8 +31,7 @@ public abstract class BaseLocationCommand extends BaseCommand
if (playerBlock.getType() == Material.AIR) {
return player.getLocation();
}
else if (upBlock.getType() == Material.AIR) {
} else if (upBlock.getType() == Material.AIR) {
return new Location(player.getLocation().getWorld(),
player.getLocation().getX(),
player.getLocation().getY() + 1,

View File

@ -20,19 +20,16 @@ package de.craftinc.gates.commands;
import de.craftinc.gates.Plugin;
import org.bukkit.ChatColor;
public class CommandAllowRiding extends BaseCommand
{
public CommandAllowRiding()
{
public class CommandAllowRiding extends BaseCommand {
public CommandAllowRiding() {
aliases.add("allowRiding");
aliases.add("ar");
requiredParameters.add("id");
helpDescription = "Allow players to travel while riding.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true;
@ -40,8 +37,7 @@ public class CommandAllowRiding extends BaseCommand
}
@Override
protected void perform()
{
protected void perform() {
gate.setAllowsVehicles(true);
sendMessage(ChatColor.GREEN + "Traveling while riding is now enabled for this gate.");
}

View File

@ -24,41 +24,30 @@ import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin;
public class CommandClose extends BaseCommand
{
public CommandClose()
{
public class CommandClose extends BaseCommand {
public CommandClose() {
aliases.add("close");
aliases.add("c");
requiredParameters.add("id");
helpDescription = "Closes a gate to prevent players from using it.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true;
senderMustBePlayer = false;
}
@Override
public void perform()
{
try
{
public void perform() {
try {
gate.setOpen(false);
GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.GREEN + "The gate was closed.");
}
catch(Exception e)
{
} catch (Exception e) {
sendMessage(ChatColor.RED + "Opening the gate failed! See server log for more information");
Plugin.log(Level.WARNING, e.getMessage());
e.printStackTrace();
}
}
}

View File

@ -19,28 +19,22 @@ package de.craftinc.gates.commands;
import de.craftinc.gates.Plugin;
import org.bukkit.ChatColor;
public class CommandDenyRiding extends BaseCommand
{
public CommandDenyRiding()
{
public class CommandDenyRiding extends BaseCommand {
public CommandDenyRiding() {
aliases.add("denyRiding");
aliases.add("dr");
requiredParameters.add("id");
helpDescription = "Deny players to travel while riding.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true;
senderMustBePlayer = false;
}
@Override
protected void perform()
{
protected void perform() {
gate.setAllowsVehicles(false);
sendMessage(ChatColor.GREEN + "Traveling while riding is now disabled for this gate.");
}

View File

@ -24,11 +24,9 @@ import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin;
import org.bukkit.Location;
public class CommandExit extends BaseCommand {
public class CommandExit extends BaseCommand
{
public CommandExit()
{
public CommandExit() {
aliases.add("exit");
aliases.add("e");
@ -44,16 +42,13 @@ public class CommandExit extends BaseCommand
}
public void perform()
{
try
{
public void perform() {
try {
Location oldExit = gate.getExit();
gate.setExit(player.getLocation());
sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand.");
Plugin.getPlugin().getGatesManager().handleGateExitChange(gate, oldExit);
}
catch (Exception e) {
} catch (Exception e) {
GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.RED + "Setting the exit for the gate failed! See server log for more information");
Plugin.log(Level.WARNING, e.getMessage());
@ -61,4 +56,3 @@ public class CommandExit extends BaseCommand
}
}
}

View File

@ -24,29 +24,23 @@ import org.bukkit.Location;
import java.util.logging.Level;
public class CommandExitOpen extends BaseCommand
{
public CommandExitOpen()
{
public class CommandExitOpen extends BaseCommand {
public CommandExitOpen() {
aliases.add("exitopen");
aliases.add("eo");
requiredParameters.add("id");
helpDescription = "Change exit of location and open that gate afterwards.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = true;
shouldPersistToDisk = true;
senderMustBePlayer = true;
}
public void perform()
{
try
{
public void perform() {
try {
Location oldExit = gate.getExit();
gate.setExit(player.getLocation());
sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand.");
@ -68,12 +62,10 @@ public class CommandExitOpen extends BaseCommand
}
sendMessage(ChatColor.GREEN + "The gate was opened.");
}
catch (Exception e) {
} catch (Exception e) {
sendMessage(ChatColor.RED + e.getMessage());
}
}
catch (Exception e) {
} catch (Exception e) {
GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.RED + "Setting the exit for the gate failed! This gate is now closed! (See server log for more information.)");
Plugin.log(Level.WARNING, e.getMessage());

View File

@ -23,12 +23,11 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CommandHelp extends BaseCommand
{
public class CommandHelp extends BaseCommand {
public static List<List<String>> helpPages;
static
{
static {
// sort the usage strings
List<String> allUsageStrings = new ArrayList<String>();
@ -63,8 +62,7 @@ public class CommandHelp extends BaseCommand
}
public CommandHelp()
{
public CommandHelp() {
aliases.add("help");
aliases.add("?");
@ -80,20 +78,17 @@ public class CommandHelp extends BaseCommand
}
public void perform()
{
public void perform() {
int page;
if (parameters.size() > 0) {
try {
page = Integer.parseInt(parameters.get(0));
}
catch (NumberFormatException e) {
} catch (NumberFormatException e) {
// wasn't an integer
page = 1;
}
}
else {
} else {
page = 1;
}

View File

@ -24,10 +24,9 @@ import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin;
public class CommandHide extends BaseCommand
{
public CommandHide()
{
public class CommandHide extends BaseCommand {
public CommandHide() {
aliases.add("hide");
aliases.add("h");
@ -43,14 +42,12 @@ public class CommandHide extends BaseCommand
}
public void perform()
{
public void perform() {
try {
gate.setHidden(true);
GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.GREEN + "The gate '" + gate.getId() + "' is now hidden.");
}
catch (Exception e) {
} catch (Exception e) {
sendMessage(ChatColor.RED + "Hiding the gate failed! See server log for more information");
Plugin.log(Level.WARNING, e.getMessage());
e.printStackTrace();

View File

@ -16,7 +16,6 @@
*/
package de.craftinc.gates.commands;
import de.craftinc.gates.util.GateBlockChangeSender;
import org.bukkit.ChatColor;
@ -24,11 +23,9 @@ import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.TextUtil;
import org.bukkit.entity.Player;
public class CommandInfo extends BaseCommand {
public class CommandInfo extends BaseCommand
{
public CommandInfo()
{
public CommandInfo() {
aliases.add("info");
aliases.add("i");
@ -45,8 +42,7 @@ public class CommandInfo extends BaseCommand
}
public void perform()
{
public void perform() {
if (this.parameters.size() > 0) {
if (!this.setGateUsingParameter(this.parameters.get(0))) {
@ -55,8 +51,7 @@ public class CommandInfo extends BaseCommand
}
sendMessage(TextUtil.titleize("Information about: '" + ChatColor.WHITE + gate.getId() + ChatColor.YELLOW + "'"));
}
else {
} else {
boolean senderIsPlayer = this.sender instanceof Player;
if (!senderIsPlayer) {

View File

@ -28,20 +28,17 @@ import de.craftinc.gates.Gate;
import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.TextUtil;
public class CommandList extends BaseCommand {
public class CommandList extends BaseCommand
{
protected static final int linesPerPage = 10;
protected static final int charactersPerLine = 52; /* this is actually no true. the
font used by minecraft is not
monospace. but I don't think
there is a (easy) way for a
bukkit plugin to calculate
private static final int linesPerPage = 10;
/* this is actually not true. the font used by Minecraft is not
monospaced. but there seems to be no (easy) way to calculate
the drawing-size of a string.
*/
private static final int charactersPerLine = 52;
public CommandList()
{
public CommandList() {
aliases.add("list");
aliases.add("ls");
@ -56,27 +53,43 @@ public class CommandList extends BaseCommand
senderMustBePlayer = false;
}
public void perform() {
int page = this.getPageParameter();
List<String> allPages = this.pagedGateIds();
protected static List<String> linesOfGateIds(List<String> gates)
{
if (allPages == null) { // no gates exist
sendMessage(ChatColor.RED + "There are no gates yet. " + ChatColor.RESET +
"(Note that you might not be allowed to get information about certain gates)");
return;
}
if (page > allPages.size() || page < 1) {
sendMessage(ChatColor.RED + "The requested page is not availible");
return;
}
String message = TextUtil.titleize("List of all gates (" + page + "/" + allPages.size() + ")") + "\n";
message += allPages.get(page - 1);
sendMessage(message);
}
private static List<String> linesOfGateIds(List<String> gates) {
List<String> lines = new ArrayList<String>();
int index = 0;
List<String> gateIdsForCurrentLine = new ArrayList<String>();
int numCharactersInCurrentLine = 0;
while (index < gates.size()) {
String gateId = gates.get(index);
int gateIdLength = gateId.length() + 2; // actual length + comma + whitespace
// special case: very long gate id
if (gateIdLength > charactersPerLine && numCharactersInCurrentLine == 0) {
if (gateIdLength > charactersPerLine && numCharactersInCurrentLine == 0) { // special case: very long gate id
gateIdsForCurrentLine = new ArrayList<String>();
numCharactersInCurrentLine = 0;
while ((gateId.length() + 2) > charactersPerLine) {
int cutPos = charactersPerLine;
// is the id too long to add comma and whitespace but not longer than the line?
@ -86,25 +99,18 @@ public class CommandList extends BaseCommand
lines.add(gateId.substring(0, cutPos));
gateId = gateId.substring(cutPos, gateId.length());
}
gateIdsForCurrentLine.add(gateId);
numCharactersInCurrentLine += gateId.length();
index++;
}
// gate fits into current line
else if ((numCharactersInCurrentLine + gateIdLength) <= charactersPerLine) {
} else if ((numCharactersInCurrentLine + gateIdLength) <= charactersPerLine) { // gate fits into current line
gateIdsForCurrentLine.add(gateId);
numCharactersInCurrentLine += gateIdLength;
index++;
}
// the current gate does not fit on the
else {
} else { // the current gate does not fit on the
lines.add(TextUtil.implode(gateIdsForCurrentLine, ", ") + ", ");
gateIdsForCurrentLine = new ArrayList<String>();
@ -116,40 +122,32 @@ public class CommandList extends BaseCommand
return lines;
}
protected static String intToTitleString(int i, boolean addPreviousPageNote, boolean addNextPageNote)
{
private static String intToTitleString(int i, boolean addPreviousPageNote, boolean addNextPageNote) {
String retVal = ChatColor.DARK_AQUA + "";
if (i < 26) {
retVal += (char) (i + 65);
}
else if ( i == 26 ) {
} else if (i == 26) {
retVal += "0-9";
}
else {
} else {
retVal += "!@#$";
}
if (addPreviousPageNote && addNextPageNote) {
retVal += " (more on previous and next page)";
}
else if (addPreviousPageNote) {
} else if (addPreviousPageNote) {
retVal += " (more on previous page)";
}
else if (addNextPageNote) {
} else if (addNextPageNote) {
retVal += " (more on next page)";
}
return retVal + "\n";
}
/**
* Method for getting a collection of gates the player is allowed to see.
*/
protected Collection<Gate> getAllGates()
{
private Collection<Gate> getAllGates() {
Collection<Gate> gates = Plugin.getPlugin().getGatesManager().allGates();
if (this.sender instanceof Player && Plugin.getPermission() != null) {
@ -159,7 +157,6 @@ public class CommandList extends BaseCommand
Collection<Gate> gatesCopy = new ArrayList<Gate>(gates);
for (Gate gate : gatesCopy) {
if (gate.getLocation() != null) {
boolean permissionAtGateLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), p.getName(), this.requiredPermission);
if (!permissionAtGateLocation) {
@ -169,7 +166,6 @@ public class CommandList extends BaseCommand
}
if (gate.getExit() != null) {
boolean permissionAtGateExit = Plugin.getPermission().has(gate.getExit().getWorld(), p.getName(), this.requiredPermission);
if (!permissionAtGateExit) {
gates.remove(gate);
@ -181,7 +177,6 @@ public class CommandList extends BaseCommand
return gates;
}
/**
* Sorts all gates by there first character.
* Puts gates in corresponding Lists: (all returned lists will be sorted alphabetically)
@ -189,8 +184,7 @@ public class CommandList extends BaseCommand
* list 26: 0-9
* list 27: other
*/
protected static List<List<String>> gatesSortedByName(Collection<Gate> allGates)
{
private static List<List<String>> gatesSortedByName(Collection<Gate> allGates) {
// create the lists
List<List<String>> ids = new ArrayList<List<String>>();
@ -205,14 +199,11 @@ public class CommandList extends BaseCommand
if (first > 96 && first < 123) { // convert lower case chars
first -= 97;
}
else if (first > 64 && first < 91) { // convert upper case chars
} else if (first > 64 && first < 91) { // convert upper case chars
first -= 65;
}
else if (first > 47 && first < 58) { // convert numbers
} else if (first > 47 && first < 58) { // convert numbers
first = 26;
}
else { // everything else
} else { // everything else
first = 27;
}
@ -227,15 +218,13 @@ public class CommandList extends BaseCommand
return ids;
}
/**
* Returns a list of strings.
* Each string is the text for a page.
* The maximum number of lines per page is 'linesPerPage' minus 1.
* Will return an empty list if no gates are availible.
*/
protected List<String> pagedGateIds()
{
private List<String> pagedGateIds() {
Collection<Gate> gates = this.getAllGates();
if (gates.size() == 0) {
@ -276,8 +265,7 @@ public class CommandList extends BaseCommand
if (linesNecessaryForCurrentGates < linesLeftOnPage) {
linesToFill = linesNecessaryForCurrentGates;
moreGatesOnNextPage = false;
}
else {
} else {
linesToFill = linesLeftOnPage - 1;
moreGatesOnNextPage = true;
}
@ -313,39 +301,14 @@ public class CommandList extends BaseCommand
}
protected int getPageParameter()
{
private int getPageParameter() {
int page = 1;
try {
page = new Integer(parameters.get(0));
} catch (Exception ignored) {
}
catch (Exception ignored) { }
return page;
}
public void perform()
{
int page = this.getPageParameter();
List<String> allPages = this.pagedGateIds();
if (allPages == null) { // no gates exist
sendMessage(ChatColor.RED + "There are no gates yet. " + ChatColor.RESET +
"(Note that you might not be allowed to get information about certain gates)");
return;
}
if (page > allPages.size() || page < 1) {
sendMessage(ChatColor.RED + "The requested page is not availible");
return;
}
String message = TextUtil.titleize("List of all gates (" + page + "/" + allPages.size() + ")") + "\n";
message += allPages.get(page-1);
sendMessage(message);
}
}

View File

@ -16,7 +16,6 @@
*/
package de.craftinc.gates.commands;
import java.util.Set;
import de.craftinc.gates.util.GateBlockChangeSender;
@ -26,18 +25,14 @@ import org.bukkit.Location;
import de.craftinc.gates.Plugin;
import org.bukkit.block.Block;
public class CommandLocation extends BaseLocationCommand {
public class CommandLocation extends BaseLocationCommand
{
public CommandLocation()
{
public CommandLocation() {
aliases.add("location");
aliases.add("lo");
requiredParameters.add("id");
helpDescription = "Set the entrance of the gate to your current location.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = true;
@ -45,13 +40,10 @@ public class CommandLocation extends BaseLocationCommand
senderMustBePlayer = true;
}
public void perform()
{
public void perform() {
Location playerLocation = getValidPlayerLocation();
if (playerLocation == null)
{
if (playerLocation == null) {
sendMessage("There is not enough room for a gate to open here");
return;
}
@ -60,25 +52,19 @@ public class CommandLocation extends BaseLocationCommand
Set<Location> oldGateBlockLocations = gate.getGateBlockLocations();
Set<Block> oldFrameBlocks = gate.getGateFrameBlocks();
try
{
try {
if (gate.isOpen()) {
GateBlockChangeSender.updateGateBlocks(gate, true);
}
gate.setLocation(playerLocation);
sendMessage(ChatColor.GREEN + "The location of '" + gate.getId() + "' is now at your current location.");
}
catch (Exception e)
{
} catch (Exception e) {
sendMessage(ChatColor.RED + "There seems to be no frame at your new location! The gate got closed!" + ChatColor.AQUA + " You should build a frame now and execute:");
sendMessage(new CommandOpen().getUsageTemplate(true, true));
}
finally {
} finally {
Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, oldLocation, oldGateBlockLocations, oldFrameBlocks);
GateBlockChangeSender.updateGateBlocks(gate);
}
}
}

View File

@ -1,6 +1,5 @@
package de.craftinc.gates.commands;
import de.craftinc.gates.Gate;
import de.craftinc.gates.GatesManager;
import de.craftinc.gates.Plugin;
@ -10,33 +9,27 @@ import de.craftinc.gates.util.TextUtil;
import java.util.ArrayList;
import java.util.Set;
public class CommandNearby extends BaseLocationCommand
{
public CommandNearby()
{
public class CommandNearby extends BaseLocationCommand {
public CommandNearby() {
aliases.add("nearby");
aliases.add("nb");
helpDescription = "Highlight nearby gates";
requiredPermission = Plugin.permissionInfo;
needsPermissionAtCurrentLocation = true;
shouldPersistToDisk = false;
senderMustBePlayer = true;
hasGateParam = false;
}
public void perform()
{
public void perform() {
GatesManager manager = Plugin.getPlugin().getGatesManager();
Set<Gate> nearbyGates = manager.getNearbyGates(player.getLocation().getChunk());
if (nearbyGates == null) {
player.sendMessage("There are no gates near you!");
}
else {
} else {
GateBlockChangeSender.temporaryHighlightGatesFrames(player, nearbyGates);
ArrayList<String> gateNames = new ArrayList<String>();
@ -44,10 +37,7 @@ public class CommandNearby extends BaseLocationCommand
for (Gate g : nearbyGates) {
gateNames.add(g.getId());
}
player.sendMessage("Nearby gates: " + TextUtil.implode(gateNames, ", "));
}
}
}

View File

@ -23,11 +23,9 @@ import de.craftinc.gates.Gate;
import de.craftinc.gates.GatesManager;
import de.craftinc.gates.Plugin;
public class CommandNew extends BaseLocationCommand {
public class CommandNew extends BaseLocationCommand
{
public CommandNew()
{
public CommandNew() {
aliases.add("new");
aliases.add("n");
@ -35,20 +33,14 @@ public class CommandNew extends BaseLocationCommand
senderMustBePlayer = true;
hasGateParam = false;
helpDescription = "Create a gate at your current location.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = true;
shouldPersistToDisk = true;
senderMustBePlayer = true;
}
public void perform()
{
public void perform() {
String id = parameters.get(0);
GatesManager gatesManager = Plugin.getPlugin().getGatesManager();
@ -60,19 +52,15 @@ public class CommandNew extends BaseLocationCommand
gate = new Gate(id);
sendMessage(ChatColor.GREEN + "Gate with id '" + id + "' was created.");
Location playerLocation = getValidPlayerLocation();
if (playerLocation != null) {
try {
gate.setLocation(playerLocation);
sendMessage(ChatColor.AQUA + "The gates location has been set to your current location.");
} catch (Exception ignored) {
}
catch (Exception ignored) {}
}
else
{
} else {
sendMessage(ChatColor.RED + "Your location is invalid!" + ChatColor.AQUA + "Go somewhere else and execute:");
sendMessage(new CommandLocation().getUsageTemplate(true, true));
}
@ -80,4 +68,3 @@ public class CommandNew extends BaseLocationCommand
gatesManager.handleNewGate(gate);
}
}

View File

@ -16,25 +16,19 @@
*/
package de.craftinc.gates.commands;
import de.craftinc.gates.util.GateBlockChangeSender;
import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin;
public class CommandOpen extends BaseCommand {
public class CommandOpen extends BaseCommand
{
public CommandOpen()
{
public CommandOpen() {
aliases.add("open");
aliases.add("o");
requiredParameters.add("id");
helpDescription = "Open a gate so players can use it.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false;
@ -42,9 +36,7 @@ public class CommandOpen extends BaseCommand
senderMustBePlayer = false;
}
public void perform()
{
public void perform() {
try {
boolean needsGateManagerUpdate = false;
@ -61,10 +53,8 @@ public class CommandOpen extends BaseCommand
}
sendMessage(ChatColor.GREEN + "The gate was opened.");
}
catch (Exception e) {
} catch (Exception e) {
sendMessage(ChatColor.RED + e.getMessage());
}
}
}

View File

@ -21,11 +21,9 @@ import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin;
public class CommandRemove extends BaseCommand {
public class CommandRemove extends BaseCommand
{
public CommandRemove()
{
public CommandRemove() {
aliases.add("delete");
aliases.add("del");
aliases.add("remove");
@ -43,9 +41,7 @@ public class CommandRemove extends BaseCommand
senderMustBePlayer = false;
}
public void perform()
{
public void perform() {
Plugin.getPlugin().getGatesManager().handleDeletion(gate);
GateBlockChangeSender.updateGateBlocks(gate, true);
sendMessage(ChatColor.GREEN + "Gate with id '" + gate.getId() + "' was deleted.");

View File

@ -21,11 +21,9 @@ import org.bukkit.ChatColor;
import de.craftinc.gates.GatesManager;
import de.craftinc.gates.Plugin;
public class CommandRename extends BaseCommand {
public class CommandRename extends BaseCommand
{
public CommandRename()
{
public CommandRename() {
aliases.add("rename");
aliases.add("rn");
@ -45,15 +43,13 @@ public class CommandRename extends BaseCommand
}
public void perform()
{
public void perform() {
String newId = parameters.get(1);
GatesManager gatesManager = Plugin.getPlugin().getGatesManager();
if (gatesManager.gateExists(newId)) {
sendMessage(ChatColor.RED + "Cannot rename " + gate.getId() + ". There is already a gate named " + newId + ".");
}
else {
} else {
String oldId = gate.getId();
gate.setId(newId);
@ -62,5 +58,4 @@ public class CommandRename extends BaseCommand
sendMessage(ChatColor.GREEN + "Gate " + gate.getId() + " is now known as " + newId + ".");
}
}
}

View File

@ -21,38 +21,27 @@ import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin;
public class CommandUnhide extends BaseCommand {
public class CommandUnhide extends BaseCommand
{
public CommandUnhide()
{
public CommandUnhide() {
aliases.add("unhide");
aliases.add("u");
requiredParameters.add("id");
helpDescription = "Make that gate visible";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true;
senderMustBePlayer = false;
}
public void perform()
{
try
{
public void perform() {
try {
gate.setHidden(false);
GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.GREEN + "The gate " + gate.getId() + " is now visible.");
}
catch (Exception e) {
} catch (Exception e) {
sendMessage(ChatColor.RED + e.getMessage());
}
}
}

View File

@ -25,11 +25,9 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
public class BlockBreakListener implements Listener
{
public class BlockBreakListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onBlockBreak(BlockBreakEvent event)
{
public void onBlockBreak(BlockBreakEvent event) {
if (event.isCancelled()) {
return;
}
@ -39,8 +37,8 @@ public class BlockBreakListener implements Listener
if (gate != null && !gate.isHidden()) {
try {
gate.setOpen(false);
} catch (Exception ignored) {
}
catch (Exception ignored) { }
GateBlockChangeSender.updateGateBlocks(gate);
}

View File

@ -24,11 +24,9 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
public class PlayerChangedWorldListener implements Listener
{
public class PlayerChangedWorldListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerChangeWorld(PlayerChangedWorldEvent event)
{
public void onPlayerChangeWorld(PlayerChangedWorldEvent event) {
GateBlockChangeSender.updateGateBlocks(event.getPlayer());
}
}

View File

@ -23,11 +23,9 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
public class PlayerJoinListener implements Listener
{
public class PlayerJoinListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerJoin(PlayerJoinEvent event)
{
public void onPlayerJoin(PlayerJoinEvent event) {
GateBlockChangeSender.updateGateBlocks(event.getPlayer());
}
}

View File

@ -38,13 +38,11 @@ import de.craftinc.gates.Plugin;
import org.bukkit.scheduler.BukkitScheduler;
public class PlayerMoveListener implements Listener
{
public class PlayerMoveListener implements Listener {
protected HashMap<String, Long> lastNoPermissionMessages = new HashMap<String, Long>();
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerMove(PlayerMoveEvent event)
{
public void onPlayerMove(PlayerMoveEvent event) {
if (event.isCancelled()) {
return;
}
@ -80,8 +78,7 @@ public class PlayerMoveListener implements Listener
event.getPlayer().sendMessage(ChatColor.RED + noPermissionString);
this.lastNoPermissionMessages.put(playerName, now);
}
}
else {
} else {
this.teleportPlayer(event.getPlayer(), gateAtLocation);
}
}
@ -89,11 +86,11 @@ public class PlayerMoveListener implements Listener
/**
* Teleports a player.
*
* @param player The player to teleport.
* @param gate The gate to which exit the player will be teleported.
*/
private void teleportPlayer(final Player player, final Gate gate)
{
private void teleportPlayer(final Player player, final Gate gate) {
// Destination
final Float newYaw = gate.getExit().getYaw() - gate.getLocation().getYaw() + player.getLocation().getYaw();
final Location destLocation = new Location(gate.getExit().getWorld(),
@ -135,8 +132,7 @@ public class PlayerMoveListener implements Listener
destLocation.getChunk().load(); // load the destination chunk, no new entity will be created otherwise
scheduler.scheduleSyncDelayedTask(plugin, new Runnable() {
public void run()
{
public void run() {
// FIXME: the code below should be executed after the chunk got loaded and not after a fixed time!
// create a new entity at the destination location
@ -154,12 +150,10 @@ public class PlayerMoveListener implements Listener
}
protected boolean hasPermission(final Player player, final Gate gate)
{
protected boolean hasPermission(final Player player, final Gate gate) {
if (Plugin.getPermission() == null) { // fallback: use the standard bukkit permission system
return player.hasPermission(Plugin.permissionUse);
}
else {
} else {
final boolean permAtLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), player.getName(), Plugin.permissionUse);
final boolean permAtExit = Plugin.getPermission().has(gate.getExit().getWorld(), player.getName(), Plugin.permissionUse);

View File

@ -24,11 +24,9 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerRespawnEvent;
public class PlayerRespawnListener implements Listener
{
public class PlayerRespawnListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerRespawn(PlayerRespawnEvent event)
{
public void onPlayerRespawn(PlayerRespawnEvent event) {
GateBlockChangeSender.updateGateBlocks(event.getPlayer(), event.getRespawnLocation(), true);
}
}

View File

@ -23,11 +23,9 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerTeleportEvent;
public class PlayerTeleportListener implements Listener
{
public class PlayerTeleportListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerTeleport(PlayerTeleportEvent event)
{
public void onPlayerTeleport(PlayerTeleportEvent event) {
if (event.isCancelled()) {
return;
}

View File

@ -18,22 +18,21 @@ package de.craftinc.gates.persistence;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.World;
import de.craftinc.gates.Plugin;
public class LocationUtil
{
public class LocationUtil {
protected final static String worldKey = "world";
protected final static String xKey = "x";
protected final static String yKey = "y";
protected final static String zKey = "z";
protected static World getWorld(final String name) throws Exception
{
protected static World getWorld(final String name) throws Exception {
if (name == null) {
throw new IllegalArgumentException("The name of the world must not be 'null");
}
@ -55,8 +54,7 @@ public class LocationUtil
* @param l The location to serialize. Supplying 'null' is ok..
* @return A Map object ready for storing inside a yaml file. Will return 'null' if 'l' is null.
*/
public static Map<String, Object> serializeLocation(final Location l)
{
public static Map<String, Object> serializeLocation(final Location l) {
if (l == null) {
return null;
}
@ -73,14 +71,12 @@ public class LocationUtil
/**
*
* @param map A map generated with the 'serializeLocation' method. Supplying 'null' is ok.
* @return A deserialized location. This method will return 'null' if 'map' is null!
* @throws Exception This method will throw an exception if the world of the supplied serialized location
* does not exist or if 'map' does not contain all necessary keys!
*/
public static Location deserializeLocation(final Map<String, Object> map) throws Exception
{
public static Location deserializeLocation(final Map<String, Object> map) throws Exception {
if (map == null) {
return null;
}

View File

@ -27,30 +27,25 @@ import java.util.List;
import java.util.logging.Level;
public class MigrationUtil
{
public static boolean performMigration(int storageVersion, int currentVersion, List<Gate> gates)
{
public class MigrationUtil {
public static boolean performMigration(int storageVersion, int currentVersion, List<Gate> gates) {
if (storageVersion == 0 && currentVersion >= 2) {
removePortalBlocks(gates);
updateAllowVehicles(gates);
return true;
}
else if (storageVersion == 1 && currentVersion >= 2) {
} else if (storageVersion == 1 && currentVersion >= 2) {
updateAllowVehicles(gates);
return true;
}
else {
} else {
Plugin.log(Level.SEVERE, "Supplied storage version is currently not supported! Make sure you have the latest version of Craft Inc. Gates installed. Plugin will be disabled!");
return false;
}
}
protected static void removePortalBlocks(List<Gate> gates)
{
protected static void removePortalBlocks(List<Gate> gates) {
for (Gate g : gates) {
for (Location l : g.getGateBlockLocations()) {
@ -64,8 +59,7 @@ public class MigrationUtil
}
protected static void updateAllowVehicles(List<Gate> gates)
{
protected static void updateAllowVehicles(List<Gate> gates) {
for (Gate g : gates) {
g.setAllowsVehicles(true);

View File

@ -23,8 +23,7 @@ import org.bukkit.Material;
import java.util.logging.Level;
public class ConfigurationUtil
{
public class ConfigurationUtil {
public static final String confMaxGateBlocksKey = "maxGateBlocks";
public static final String confPlayerGateBlockUpdateRadiusKey = "playerGateBlockUpdateRadius";
public static final String confCheckForBrokenGateFramesKey = "checkForBrokenGateFrames";
@ -38,79 +37,56 @@ public class ConfigurationUtil
public static final String confGateMaterialKey = "gateMaterial";
public static GateMaterial getPortalMaterial()
{
public static GateMaterial getPortalMaterial() {
String materialString = Plugin.getPlugin().getConfig().getString(confGateMaterialKey);
GateMaterial material = new GateMaterial();
if (materialString.equals("sapling")) {
material.material = Material.SAPLING;
}
else if (materialString.equals("water")) {
} else if (materialString.equals("water")) {
material.material = Material.STATIONARY_WATER;
}
else if (materialString.equals("lava")) {
} else if (materialString.equals("lava")) {
material.material = Material.STATIONARY_LAVA;
}
else if (materialString.equals("cobweb")) {
} else if (materialString.equals("cobweb")) {
material.material = Material.WEB;
}
else if (materialString.equals("grass")) {
} else if (materialString.equals("grass")) {
material.material = Material.LONG_GRASS;
material.data = 1;
}
else if (materialString.equals("dead bush")) {
} else if (materialString.equals("dead bush")) {
material.material = Material.DEAD_BUSH;
}
else if (materialString.equals("dandelion")) {
} else if (materialString.equals("dandelion")) {
material.material = Material.YELLOW_FLOWER;
}
else if (materialString.equals("poppy")) {
} else if (materialString.equals("poppy")) {
material.material = Material.RED_ROSE;
}
else if (materialString.equals("brown mushroom")) {
} else if (materialString.equals("brown mushroom")) {
material.material = Material.BROWN_MUSHROOM;
}
else if (materialString.equals("red mushroom")) {
} else if (materialString.equals("red mushroom")) {
material.material = Material.RED_MUSHROOM;
}
else if (materialString.equals("torch")) {
} else if (materialString.equals("torch")) {
material.material = Material.TORCH;
}
else if (materialString.equals("redstone torch (off)")) {
} else if (materialString.equals("redstone torch (off)")) {
material.material = Material.REDSTONE_TORCH_OFF;
}
else if (materialString.equals("redstone torch (on)")) {
} else if (materialString.equals("redstone torch (on)")) {
material.material = Material.REDSTONE_TORCH_ON;
}
else if (materialString.equals("fence")) {
} else if (materialString.equals("fence")) {
material.material = Material.FENCE;
}
else if (materialString.equals("nether portal")) {
} else if (materialString.equals("nether portal")) {
material.material = Material.PORTAL;
}
else if (materialString.equals("iron bars")) {
} else if (materialString.equals("iron bars")) {
material.material = Material.IRON_FENCE;
}
else if (materialString.equals("glass pane")) {
} else if (materialString.equals("glass pane")) {
material.material = Material.THIN_GLASS;
}
else if (materialString.equals("fence gate")) {
} else if (materialString.equals("fence gate")) {
material.material = Material.FENCE_GATE;
}
else if (materialString.equals("nether brick fence")) {
} else if (materialString.equals("nether brick fence")) {
material.material = Material.NETHER_FENCE;
}
else if (materialString.equals("nether wart")) {
} else if (materialString.equals("nether wart")) {
material.material = Material.NETHER_WARTS;
}
else if (materialString.equals("end portal")) {
} else if (materialString.equals("end portal")) {
material.material = Material.ENDER_PORTAL;
}
else if (materialString.equals("cobblestone wall")) {
} else if (materialString.equals("cobblestone wall")) {
material.material = Material.COBBLE_WALL;
}
else { // fallback!
} else { // fallback!
material.material = Material.PORTAL;
Plugin.log(Level.WARNING, "Gate material invalid! Please check and correct your configuration file!");
}
@ -120,8 +96,7 @@ public class ConfigurationUtil
}
class GateMaterial
{
class GateMaterial {
public Material material = Material.PORTAL;
public byte data = 0;
}

View File

@ -28,13 +28,11 @@ import org.bukkit.block.BlockFace;
import de.craftinc.gates.Plugin;
public class FloodUtil
{
public class FloodUtil {
protected static final Set<BlockFace> exp1 = new HashSet<BlockFace>();
protected static final Set<BlockFace> exp2 = new HashSet<BlockFace>();
static
{
static {
exp1.add(BlockFace.UP);
exp1.add(BlockFace.DOWN);
exp1.add(BlockFace.EAST);
@ -49,11 +47,11 @@ public class FloodUtil
/**
* Returns the all frame blocks of an gate.
*
* @param blocks All blocks inside the gate.
* @return A Set containing all frame block. Will never return 'null'.
*/
public static Set<Block> getFrame(final Set<Block> blocks)
{
public static Set<Block> getFrame(final Set<Block> blocks) {
if (blocks == null || blocks.isEmpty()) {
return new HashSet<Block>();
}
@ -81,8 +79,7 @@ public class FloodUtil
if (gateFrameSearchFaces != null) {
return _getFrame(blocks, gateFrameSearchFaces);
}
else { // no direction found (the gate might only consist of blocks one over another)
} else { // no direction found (the gate might only consist of blocks one over another)
// Try one direction and check if the found blocks are not air.
// If air is found (frame broken or wrong direction) return the other direction
@ -100,9 +97,7 @@ public class FloodUtil
}
protected static Set<Block> _getFrame(final Set<Block> blocks, final Set<BlockFace> searchDirections)
{
protected static Set<Block> _getFrame(final Set<Block> blocks, final Set<BlockFace> searchDirections) {
Set<Block> frameBlocks = new HashSet<Block>();
for (Block b : blocks) {
@ -122,11 +117,11 @@ public class FloodUtil
/**
* Returns the all frame blocks of an gate.
*
* @param locations All locations inside the gate.
* @return A Set containing all frame block. Will never return 'null'.
*/
public static Set<Block> getFrameWithLocations(final Set<Location> locations)
{
public static Set<Block> getFrameWithLocations(final Set<Location> locations) {
if (locations == null) {
throw new IllegalArgumentException("'locations' must not be 'null'");
}
@ -142,8 +137,7 @@ public class FloodUtil
// For the same frame and location this set of blocks is deterministic
public static Set<Block> getGatePortalBlocks(final Block block)
{
public static Set<Block> getGatePortalBlocks(final Block block) {
if (block == null) {
throw new IllegalArgumentException("'block' must not be 'null'");
}
@ -176,8 +170,7 @@ public class FloodUtil
protected static Set<Block> getAirFloodBlocks(final Block startBlock,
Set<Block> foundBlocks,
final Set<BlockFace> expandFaces,
int limit)
{
int limit) {
if (foundBlocks == null) {
return null;
}

View File

@ -32,17 +32,16 @@ import java.util.Set;
import static de.craftinc.gates.util.ConfigurationUtil.*;
public class GateBlockChangeSender
{
public class GateBlockChangeSender {
/**
* Replaces gate frame blocks with glowstone for a short period of time.
* Uses the data stored in 'highlightDuration' inside the config file
* for determining when to de-highlight the frames.
*
* @param player The player for whom the frame should be highlighted.
* Must not be null!
*/
public static void temporaryHighlightGatesFrames(final Player player, final Set<Gate> gates)
{
public static void temporaryHighlightGatesFrames(final Player player, final Set<Gate> gates) {
if (player == null) {
throw new IllegalArgumentException("'player' must not be 'null'!");
}
@ -71,8 +70,7 @@ public class GateBlockChangeSender
}
public static void temporaryHighlightGateFrame(final Player player, final Gate gate)
{
public static void temporaryHighlightGateFrame(final Player player, final Gate gate) {
if (gate == null) {
throw new IllegalArgumentException("'gate' must not be 'null!");
}
@ -99,8 +97,7 @@ public class GateBlockChangeSender
}
protected static void dehighlightGatesFrames(final Player player, final Set<Gate> gates)
{
protected static void dehighlightGatesFrames(final Player player, final Set<Gate> gates) {
for (Gate g : gates) {
Set<Block> frameBlocks = g.getGateFrameBlocks();
@ -111,8 +108,7 @@ public class GateBlockChangeSender
}
protected static void dehighlightGateFrame(final Player player, final Gate gate)
{
protected static void dehighlightGateFrame(final Player player, final Gate gate) {
Set<Block> frameBlocks = gate.getGateFrameBlocks();
for (Block b : frameBlocks) {
@ -124,13 +120,13 @@ public class GateBlockChangeSender
/**
* Sends gate blocks to player at a given location. Will send the updates either immediately or
* immediately and after a short delay.
*
* @param player A player to send block changes to. Must not be null!
* @param location The location to look for gates nearby. Must not be null!
* @param sendDelayed Set to 'true' if the block changes shall be send a second time after a one
* second delay.
*/
public static void updateGateBlocks(final Player player, final Location location, boolean sendDelayed)
{
public static void updateGateBlocks(final Player player, final Location location, boolean sendDelayed) {
if (player == null) {
throw new IllegalArgumentException("'player' must not be 'null'!");
}
@ -161,11 +157,9 @@ public class GateBlockChangeSender
}
if (sendDelayed) {
Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.getPlugin(), new Runnable()
{
Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.getPlugin(), new Runnable() {
@Override
public void run()
{
public void run() {
updateGateBlocks(player, location, false);
}
}, 20L);
@ -176,16 +170,14 @@ public class GateBlockChangeSender
/**
* This method calls: updateGateBlocks(player, location, false);
*/
public static void updateGateBlocks(final Player player, final Location location)
{
public static void updateGateBlocks(final Player player, final Location location) {
updateGateBlocks(player, location, false);
}
/**
* This method calls: updateGateBlocks(player, player.getLocation(), false);
*/
public static void updateGateBlocks(final Player player)
{
public static void updateGateBlocks(final Player player) {
if (player == null) {
throw new IllegalArgumentException("'player' must not be 'null'!");
}
@ -194,18 +186,17 @@ public class GateBlockChangeSender
}
public static void updateGateBlocks(final Gate gate)
{
public static void updateGateBlocks(final Gate gate) {
updateGateBlocks(gate, false);
}
/**
* Sends block changes to players near a given gate.
*
* @param gate Must not be 'null'!
* @param remove Set to true if all visible gate blocks shall be removed.
*/
public static void updateGateBlocks(final Gate gate, boolean remove)
{
public static void updateGateBlocks(final Gate gate, boolean remove) {
if (gate == null) {
throw new IllegalArgumentException("'gate must not be 'null'!");
}
@ -234,8 +225,7 @@ public class GateBlockChangeSender
if (gate.isOpen() && !gate.isHidden() && !remove) {
material = gateMaterial.material;
data = gateMaterial.data;
}
else {
} else {
material = Material.AIR;
}

View File

@ -19,22 +19,19 @@ package de.craftinc.gates.util;
import org.bukkit.Chunk;
import org.bukkit.World;
public class SimpleChunk
{
public class SimpleChunk {
private int x;
private int z;
private String world;
public SimpleChunk(Chunk c)
{
public SimpleChunk(Chunk c) {
this.x = c.getX();
this.z = c.getZ();
this.world = c.getWorld().getName();
}
public SimpleChunk(int x, int z, World w)
{
public SimpleChunk(int x, int z, World w) {
this.x = x;
this.z = z;
this.world = w.getName();
@ -42,8 +39,7 @@ public class SimpleChunk
@Override
public boolean equals(Object o)
{
public boolean equals(Object o) {
if (o instanceof SimpleChunk) {
SimpleChunk otherLocation = (SimpleChunk) o;
@ -60,8 +56,7 @@ public class SimpleChunk
@Override
public int hashCode()
{
public int hashCode() {
int hash = 11;
hash = 29 * hash + x;
hash = 37 * hash + z;
@ -72,8 +67,7 @@ public class SimpleChunk
@Override
public String toString()
{
public String toString() {
return this.getClass().toString() + " (x=" + this.x + " z=" + this.z + " world='" + this.world + "')";
}
}

View File

@ -18,16 +18,14 @@ package de.craftinc.gates.util;
import org.bukkit.Location;
public class SimpleLocation
{
public class SimpleLocation {
private String world;
private int x;
private int y;
private int z;
public SimpleLocation(Location l)
{
public SimpleLocation(Location l) {
this.world = l.getWorld().getName();
// Using Block coordinates makes it possible to compare block locations with player locations.
@ -38,8 +36,7 @@ public class SimpleLocation
}
public SimpleLocation(Location l, boolean isHeadPosition)
{
public SimpleLocation(Location l, boolean isHeadPosition) {
this.world = l.getWorld().getName();
// Using Block coordinates makes it possible to compare block locations with player locations.
@ -55,16 +52,13 @@ public class SimpleLocation
@Override
public String toString()
{
public String toString() {
return super.toString() + " x: " + x + " y: " + y + " z: " + z + " world: " + world;
}
@Override
public boolean equals(final Object o)
{
public boolean equals(final Object o) {
if (o instanceof SimpleLocation) {
SimpleLocation otherLocation = (SimpleLocation) o;
@ -82,8 +76,7 @@ public class SimpleLocation
@Override
public int hashCode()
{
public int hashCode() {
int hash = 13;
hash = 37 * hash + x;
hash = 31 * hash + y;

View File

@ -20,16 +20,13 @@ import org.bukkit.ChatColor;
import java.util.List;
public class TextUtil
{
public static String titleize(String str)
{
public class TextUtil {
public static String titleize(String str) {
String center = ".[ " + ChatColor.YELLOW + str + ChatColor.GOLD + " ].";
if (center.length() >= 60) {
return ChatColor.GOLD + center;
}
else {
} else {
String line = ChatColor.GOLD + repeat("_", 60);
int pivot = line.length() / 2;
@ -41,8 +38,7 @@ public class TextUtil
}
public static String repeat(String s, int times)
{
public static String repeat(String s, int times) {
if (times <= 0)
return "";
@ -53,8 +49,7 @@ public class TextUtil
/**
* Joins all elements of list into a single string, separating the original strings with glue.
*/
public static String implode(List<String> list, String glue)
{
public static String implode(List<String> list, String glue) {
if (list.size() == 0) {
return "";
}

View File

@ -20,10 +20,8 @@ import org.bukkit.Location;
import org.bukkit.entity.*;
public class VehicleCloner
{
public static Vehicle clone(Vehicle parent, Location cloneLocation)
{
public class VehicleCloner {
public static Vehicle clone(Vehicle parent, Location cloneLocation) {
Vehicle clone = cloneLocation.getWorld().spawn(cloneLocation, parent.getClass());
clone.setFallDistance(parent.getFallDistance());
@ -41,8 +39,7 @@ public class VehicleCloner
boat.setUnoccupiedDeceleration(parentBoat.getUnoccupiedDeceleration());
boat.setWorkOnLand(parentBoat.getWorkOnLand());
boat.setVelocity(parentBoat.getVelocity());
}
else if (clone instanceof Animals) {
} else if (clone instanceof Animals) {
Animals animal = (Animals) clone;
Animals parentAnimal = (Animals) parent;
@ -81,21 +78,18 @@ public class VehicleCloner
if (parentHorse.isAdult()) {
horse.setAdult();
}
else {
} else {
horse.setBaby();
}
horse.setBreed(parentHorse.canBreed());
}
else if (clone instanceof Pig) {
} else if (clone instanceof Pig) {
Pig pig = (Pig) clone;
Pig parentPig = (Pig) parent;
pig.setSaddle(parentPig.hasSaddle());
}
}
else if (clone instanceof Minecart) {
} else if (clone instanceof Minecart) {
Minecart minecart = (Minecart) clone;
Minecart parentMinecart = (Minecart) parent;