Updated access modifiers, fixed all warnings.

This commit is contained in:
Tobias Ottenweller 2016-12-31 13:51:21 +01:00
parent 927b70fd2d
commit 3bc5ba500c
21 changed files with 287 additions and 357 deletions

View File

@ -11,6 +11,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<!-- License -->

View File

@ -29,18 +29,23 @@ import java.util.*;
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>();
private Set<Location> gateBlockLocations = new HashSet<>(); /* Locations of the blocks inside the gate */
private Set<Block> gateFrameBlocks = new HashSet<>();
protected Location exit;
protected boolean isHidden = false;
protected boolean isOpen = false;
private boolean isHidden = false;
private boolean isOpen = false;
protected boolean allowsVehicles = true;
private boolean allowsVehicles = true;
protected String id;
public static String getGateBlocksKey() {
return gateBlocksKey;
}
/**
* You should never create two gates with the same 'id'. Also see 'setId(String id)'.
*
@ -77,12 +82,11 @@ public class Gate implements ConfigurationSerializable {
findPortalBlocks();
validate();
} else {
this.gateBlockLocations = new HashSet<Location>();
this.gateFrameBlocks = new HashSet<Block>();
this.gateBlockLocations = new HashSet<>();
this.gateFrameBlocks = new HashSet<>();
}
}
/**
* @return This method might return a 'null' value.
*/
@ -90,7 +94,6 @@ public class Gate implements ConfigurationSerializable {
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
@ -102,7 +105,6 @@ public class Gate implements ConfigurationSerializable {
validate();
}
/**
* @return This method will never return 'null'.
*/
@ -110,7 +112,6 @@ public class Gate implements ConfigurationSerializable {
return id;
}
/**
* 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!
@ -125,23 +126,19 @@ public class Gate implements ConfigurationSerializable {
this.id = id.toLowerCase();
}
public boolean isHidden() {
return isHidden;
}
public void setHidden(boolean isHidden) throws Exception {
this.isHidden = isHidden;
this.validate();
}
public boolean isOpen() {
return isOpen;
}
public void setOpen(boolean isOpen) throws Exception {
if (isOpen && !this.isOpen) {
findPortalBlocks();
@ -151,17 +148,14 @@ public class Gate implements ConfigurationSerializable {
validate();
}
public void setAllowsVehicles(boolean allowsVehicles) {
this.allowsVehicles = allowsVehicles;
}
public boolean getAllowsVehicles() {
return this.allowsVehicles;
}
/**
* @return Will never return 'null' but might return an empty Set.
*/
@ -169,7 +163,6 @@ public class Gate implements ConfigurationSerializable {
return gateBlockLocations;
}
/**
* @return Will never return 'null' but might return an empty Set.
*/
@ -177,9 +170,8 @@ public class Gate implements ConfigurationSerializable {
return gateFrameBlocks;
}
protected void findPortalBlocks() {
gateBlockLocations = new HashSet<Location>();
private void findPortalBlocks() {
gateBlockLocations = new HashSet<>();
Set<Block> gateBlocks = FloodUtil.getGatePortalBlocks(location.getBlock());
if (gateBlocks != null) {
@ -191,35 +183,34 @@ public class Gate implements ConfigurationSerializable {
gateFrameBlocks = FloodUtil.getFrame(gateBlocks);
}
/**
* Checks if values attributes do add up; will close gate on wrong values.
*/
public void validate() throws Exception {
void validate() throws Exception {
if (!isOpen) {
return;
}
if (location == null) {
isOpen = false;
this.gateBlockLocations = new HashSet<Location>();
this.gateFrameBlocks = new HashSet<Block>();
this.gateBlockLocations = new HashSet<>();
this.gateFrameBlocks = new HashSet<>();
throw new Exception("Gate got closed. It has no location.");
}
if (exit == null) {
isOpen = false;
this.gateBlockLocations = new HashSet<Location>();
this.gateFrameBlocks = new HashSet<Block>();
this.gateBlockLocations = new HashSet<>();
this.gateFrameBlocks = new HashSet<>();
throw new Exception("Gate got closed. It has no exit.");
}
if (gateBlockLocations.size() == 0) {
isOpen = false;
this.gateBlockLocations = new HashSet<Location>();
this.gateFrameBlocks = new HashSet<Block>();
this.gateBlockLocations = new HashSet<>();
this.gateFrameBlocks = new HashSet<>();
throw new Exception("Gate got closed. The frame is missing or broken. (no gate blocks)");
}
@ -230,8 +221,8 @@ public class Gate implements ConfigurationSerializable {
if (b.getType() == Material.AIR) {
isOpen = false;
this.gateBlockLocations = new HashSet<Location>();
this.gateFrameBlocks = new HashSet<Block>();
this.gateBlockLocations = new HashSet<>();
this.gateFrameBlocks = new HashSet<>();
throw new Exception("Gate got closed. The frame is missing or broken. (missing frame block(s))");
}
@ -243,17 +234,17 @@ public class Gate implements ConfigurationSerializable {
/*
* INTERFACE: ConfigurationSerializable
*/
static protected String idKey = "id";
static protected String locationKey = "location";
static protected String gateBlocksKey = "gateBlocks";
static protected String exitKey = "exit";
static protected String isHiddenKey = "hidden";
static protected String isOpenKey = "open";
static protected String locationYawKey = "locationYaw";
static protected String locationPitchKey = "locationPitch";
static protected String exitYawKey = "exitYaw";
static protected String exitPitchKey = "exitPitch";
static protected String allowsVehiclesKey = "allowsVehiclesKey";
static private String idKey = "id";
static private String locationKey = "location";
static private String gateBlocksKey = "gateBlocks";
static private String exitKey = "exit";
static private String isHiddenKey = "hidden";
static private String isOpenKey = "open";
static private String locationYawKey = "locationYaw";
static private String locationPitchKey = "locationPitch";
static private String exitYawKey = "exitYaw";
static private String exitPitchKey = "exitPitch";
static private String allowsVehiclesKey = "allowsVehiclesKey";
@SuppressWarnings("unchecked")
@ -281,7 +272,7 @@ public class Gate implements ConfigurationSerializable {
allowsVehicles = (Boolean) map.get(allowsVehiclesKey);
}
gateBlockLocations = new HashSet<Location>();
gateBlockLocations = new HashSet<>();
List<Map<String, Object>> serializedGateBlocks = (List<Map<String, Object>>) map.get(gateBlocksKey);
for (Map<String, Object> sgb : serializedGateBlocks) {
@ -305,7 +296,7 @@ public class Gate implements ConfigurationSerializable {
public Map<String, Object> serialize() {
Map<String, Object> retVal = new HashMap<String, Object>();
Map<String, Object> retVal = new HashMap<>();
retVal.put(idKey, id);
retVal.put(locationKey, LocationUtil.serializeLocation(location));
@ -324,7 +315,7 @@ public class Gate implements ConfigurationSerializable {
retVal.put(locationYawKey, location.getYaw());
}
List<Map<String, Object>> serializedGateBlocks = new ArrayList<Map<String, Object>>();
List<Map<String, Object>> serializedGateBlocks = new ArrayList<>();
for (Location l : gateBlockLocations) {
serializedGateBlocks.add(LocationUtil.serializeLocation(l));

View File

@ -20,11 +20,11 @@ package de.craftinc.gates;
import java.util.Map;
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
public static final String changedLocation = "GateChangeListener-changedLocation"; // value will the old location
public static final String changedExit = "GateChangeListener-changedExit"; // value will be the old exit
String newGate = "GateChangeListener-newGate"; // value will be null
String removedGate = "GateChangeListener-removedGate"; // value will be null
String changedID = "GateChangeListener-changedID"; // value will be the old ID
String changedLocation = "GateChangeListener-changedLocation"; // value will the old location
String changedExit = "GateChangeListener-changedExit"; // value will be the old exit
public void gateChangedHandler(final Gate g, final Map<String, Object> changeSet);
void gateChangedHandler(final Gate g, final Map<String, Object> changeSet);
}

View File

@ -36,47 +36,30 @@ import de.craftinc.gates.util.SimpleLocation;
public class GatesManager {
protected File gatesConfigFile;
protected FileConfiguration gatesConfig;
protected static final String gatesPath = "gates"; // path to gates inside the yaml file
protected static final String storageVersionPath = "version";
protected static final int storageVersion = 2;
protected int chunkRadius;
protected Map<String, Gate> gatesById;
protected Map<SimpleChunk, Set<Gate>> gatesByChunk;
protected Map<SimpleLocation, Gate> gatesByLocation;
protected Map<SimpleLocation, Gate> gatesByFrameLocation;
protected List<Gate> gates;
protected boolean storageFileIsInvalid = false;
protected Set<GateChangeListener> changeListeners = new HashSet<GateChangeListener>();
public void addGateChangeListener(GateChangeListener listener) {
this.changeListeners.add(listener);
}
public void removeGateChangeListener(GateChangeListener listener) {
this.changeListeners.remove(listener);
}
private static final String gatesPath = "gates"; // path to gates inside the yaml file
private static final String storageVersionPath = "version";
private static final int storageVersion = 2;
private File gatesConfigFile;
private FileConfiguration gatesConfig;
private int chunkRadius;
private Map<String, Gate> gatesById;
private Map<SimpleChunk, Set<Gate>> gatesByChunk;
private Map<SimpleLocation, Gate> gatesByLocation;
private Map<SimpleLocation, Gate> gatesByFrameLocation;
private boolean storageFileIsInvalid = false;
public Gate getGateWithId(final String id) {
return gatesById.get(id.toLowerCase());
}
public Set<Gate> getNearbyGates(final Chunk chunk) {
SimpleChunk simpleChunk = new SimpleChunk(chunk);
return gatesByChunk.get(simpleChunk);
}
/**
* Returns the closest gate.
*
@ -138,7 +121,7 @@ public class GatesManager {
@SuppressWarnings("unchecked")
public boolean loadGatesFromDisk() {
boolean loadGatesFromDisk() {
this.gatesConfigFile = new File(Plugin.getPlugin().getDataFolder(), "gates.yml");
if (!this.gatesConfigFile.exists()) {
@ -168,7 +151,7 @@ public class GatesManager {
this.gates = (List<Gate>) gatesConfig.getList(gatesPath);
if (this.gates == null) {
this.gates = new ArrayList<Gate>();
this.gates = new ArrayList<>();
}
for (Object o : this.gates) {
@ -220,8 +203,7 @@ public class GatesManager {
return true;
}
protected int getChunkRadius() {
private int getChunkRadius() {
if (this.chunkRadius == 0) {
this.chunkRadius = Plugin.getPlugin().getConfig().getInt(ConfigurationUtil.confPlayerGateBlockUpdateRadiusKey);
this.chunkRadius = this.chunkRadius >> 4;
@ -230,97 +212,80 @@ public class GatesManager {
return this.chunkRadius;
}
protected void fillGatesById() {
gatesById = new HashMap<String, Gate>((int) (gates.size() * 1.25));
private void fillGatesById() {
gatesById = new HashMap<>((int) (gates.size() * 1.25));
for (Gate g : gates) {
this.addGateWithId(g);
}
}
protected void fillGatesByChunk() {
HashSet<SimpleChunk> chunksUsedByGates = new HashSet<SimpleChunk>();
private void fillGatesByChunk() {
HashSet<SimpleChunk> chunksUsedByGates = new HashSet<>();
for (Gate g : gates) {
if (g.getLocation() != null) {
Chunk c = g.getLocation().getChunk();
int x = c.getX();
int z = c.getZ();
for (int i = x - getChunkRadius(); i < x + getChunkRadius(); i++) {
for (int j = z - getChunkRadius(); j < z + getChunkRadius(); j++) {
chunksUsedByGates.add(new SimpleChunk(i, j, c.getWorld()));
}
}
}
}
gatesByChunk = new HashMap<SimpleChunk, Set<Gate>>((int) (chunksUsedByGates.size() * 1.25));
gatesByChunk = new HashMap<>((int) (chunksUsedByGates.size() * 1.25));
for (Gate g : gates) {
this.addGateByChunk(g);
}
}
protected void fillGatesByLocation() {
Set<Location> gateBlocks = new HashSet<Location>();
private void fillGatesByLocation() {
Set<Location> gateBlocks = new HashSet<>();
for (Gate g : gates) {
for (Location l : g.getGateBlockLocations()) {
gateBlocks.add(l);
Location headLocation = new Location(l.getWorld(),
l.getX(),
l.getY() + 1,
l.getZ());
Location headLocation = l.clone().add(0, 1, 0);
gateBlocks.add(headLocation);
}
}
gatesByLocation = new HashMap<SimpleLocation, Gate>((int) (gateBlocks.size() * 1.25));
gatesByLocation = new HashMap<>((int) (gateBlocks.size() * 1.25));
for (Gate g : gates) {
this.addGateByLocations(g);
}
}
protected void fillGatesByFrameLocation() {
private void fillGatesByFrameLocation() {
int numFrameBlocks = 0;
for (Gate g : gates) {
numFrameBlocks += g.gateFrameBlocks.size();
numFrameBlocks += g.getGateFrameBlocks().size();
}
gatesByFrameLocation = new HashMap<SimpleLocation, Gate>((int) (numFrameBlocks * 1.25));
gatesByFrameLocation = new HashMap<>((int) (numFrameBlocks * 1.25));
for (Gate g : gates) {
this.addGateByFrameLocations(g);
}
}
protected void removeGateById(final String id) {
private void removeGateById(final String id) {
gatesById.remove(id);
}
protected void addGateWithId(final Gate g) {
private void addGateWithId(final Gate g) {
gatesById.put(g.getId(), g);
}
protected void removeGateByLocation(final Set<Location> gateBlocks) {
private void removeGateByLocation(final Set<Location> gateBlocks) {
if (gateBlocks != null) {
for (Location l : gateBlocks) {
@ -334,8 +299,7 @@ public class GatesManager {
}
}
protected void removeGateByFrameLocation(final Set<Block> gateFrameBlocks) {
private void removeGateByFrameLocation(final Set<Block> gateFrameBlocks) {
if (gateFrameBlocks != null) {
for (Block block : gateFrameBlocks) {
@ -345,8 +309,7 @@ public class GatesManager {
}
}
protected void addGateByLocations(final Gate g) {
private void addGateByLocations(final Gate g) {
for (Location l : g.getGateBlockLocations()) {
SimpleLocation sl = new SimpleLocation(l);
@ -357,16 +320,14 @@ public class GatesManager {
}
}
protected void addGateByFrameLocations(final Gate g) {
private void addGateByFrameLocations(final Gate g) {
for (Block block : g.getGateFrameBlocks()) {
SimpleLocation sl = new SimpleLocation(block.getLocation());
gatesByFrameLocation.put(sl, g);
}
}
protected void removeGateFromChunk(final Gate g, final Location l) {
private void removeGateFromChunk(final Gate g, final Location l) {
if (l != null) {
Chunk c = l.getChunk();
@ -389,8 +350,7 @@ public class GatesManager {
}
}
protected void addGateByChunk(final Gate g) {
private void addGateByChunk(final Gate g) {
Location gateLocation = g.getLocation();
if (gateLocation != null) {
@ -418,8 +378,7 @@ public class GatesManager {
}
}
public void storeInvalidGate(Map<String, Object> map) {
void storeInvalidGate(Map<String, Object> map) {
File invalidGatesFile = new File(Plugin.getPlugin().getDataFolder(), "invalid_gates.yml");
Boolean invalidGatesFileExists = invalidGatesFile.exists();
@ -473,13 +432,6 @@ public class GatesManager {
public void handleGateIdChange(final Gate g, final String oldId) {
this.removeGateById(oldId);
this.addGateWithId(g);
Map<String, Object> changeSet = new HashMap<String, Object>();
changeSet.put(GateChangeListener.changedID, oldId);
for (GateChangeListener l : this.changeListeners) {
l.gateChangedHandler(g, changeSet);
}
}
@ -495,25 +447,11 @@ public class GatesManager {
this.removeGateByFrameLocation(oldGateFrameBlocks);
this.addGateByFrameLocations(g);
Map<String, Object> changeSet = new HashMap<String, Object>();
changeSet.put(GateChangeListener.changedLocation, oldLocation);
for (GateChangeListener l : this.changeListeners) {
l.gateChangedHandler(g, changeSet);
}
}
public void handleGateExitChange(final Gate g, final Location oldExit) {
// nothing to do
Map<String, Object> changeSet = new HashMap<String, Object>();
changeSet.put(GateChangeListener.changedExit, oldExit);
for (GateChangeListener l : this.changeListeners) {
l.gateChangedHandler(g, changeSet);
}
}
@ -524,14 +462,6 @@ public class GatesManager {
this.addGateByLocations(g);
this.addGateWithId(g);
this.addGateByFrameLocations(g);
Map<String, Object> changeSet = new HashMap<String, Object>();
changeSet.put(GateChangeListener.newGate, null);
for (GateChangeListener l : this.changeListeners) {
l.gateChangedHandler(g, changeSet);
}
}
@ -542,13 +472,6 @@ public class GatesManager {
this.removeGateFromChunk(g, g.getLocation());
this.removeGateByLocation(g.getGateBlockLocations());
this.removeGateByFrameLocation(g.getGateFrameBlocks());
Map<String, Object> changeSet = new HashMap<String, Object>();
changeSet.put(GateChangeListener.removedGate, null);
for (GateChangeListener l : this.changeListeners) {
l.gateChangedHandler(g, changeSet);
}
}

View File

@ -46,16 +46,16 @@ public class Plugin extends JavaPlugin {
private static Plugin instance;
private static Permission permission;
protected String baseCommand;
protected List<BaseCommand> commands = new ArrayList<BaseCommand>();
protected GatesManager gatesManager = new GatesManager();
private String baseCommand;
protected List<BaseCommand> commands = new ArrayList<>();
private GatesManager gatesManager = new GatesManager();
protected PlayerMoveListener moveListener = new PlayerMoveListener();
protected PlayerTeleportListener teleportListener = new PlayerTeleportListener();
protected PlayerRespawnListener respawnListener = new PlayerRespawnListener();
protected PlayerChangedWorldListener worldChangeListener = new PlayerChangedWorldListener();
protected PlayerJoinListener joinListener = new PlayerJoinListener();
protected BlockBreakListener blockBreakListener = new BlockBreakListener();
private PlayerMoveListener moveListener = new PlayerMoveListener();
private PlayerTeleportListener teleportListener = new PlayerTeleportListener();
private PlayerRespawnListener respawnListener = new PlayerRespawnListener();
private PlayerChangedWorldListener worldChangeListener = new PlayerChangedWorldListener();
private PlayerJoinListener joinListener = new PlayerJoinListener();
private BlockBreakListener blockBreakListener = new BlockBreakListener();
public Plugin() {
@ -79,7 +79,7 @@ public class Plugin extends JavaPlugin {
}
protected void setupPermissions() {
private void setupPermissions() {
if (getServer().getPluginManager().getPlugin("Vault") == null) {
return;
}
@ -154,7 +154,7 @@ public class Plugin extends JavaPlugin {
}
protected void registerEventListeners() {
private void registerEventListeners() {
PluginManager pm = this.getServer().getPluginManager();
pm.registerEvents(this.moveListener, this);
@ -187,13 +187,13 @@ public class Plugin extends JavaPlugin {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
List<String> parameters = new ArrayList<String>(Arrays.asList(args));
List<String> parameters = new ArrayList<>(Arrays.asList(args));
this.handleCommand(sender, parameters);
return true;
}
public void handleCommand(CommandSender sender, List<String> parameters) {
private void handleCommand(CommandSender sender, List<String> parameters) {
if (parameters.size() == 0) {
this.commands.get(0).execute(sender, parameters);
return;
@ -202,9 +202,9 @@ public class Plugin extends JavaPlugin {
String commandName = parameters.get(0).toLowerCase();
parameters.remove(0);
for (BaseCommand fcommand : this.commands) {
if (fcommand.getAliases().contains(commandName)) {
fcommand.execute(sender, parameters);
for (BaseCommand command : this.commands) {
if (command.getAliases().contains(commandName)) {
command.execute(sender, parameters);
return;
}
}
@ -213,7 +213,6 @@ public class Plugin extends JavaPlugin {
ChatColor.GREEN + " Try " + "/" + getBaseCommand() + " help");
}
/*
* Logging
*/

View File

@ -32,31 +32,29 @@ import de.craftinc.gates.util.TextUtil;
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>();
protected List<String> aliases = new ArrayList<>();
protected List<String> requiredParameters = new ArrayList<>();
List<String> optionalParameters = new ArrayList<>();
protected String helpDescription = "no description";
protected List<String> parameters;
protected CommandSender sender;
List<String> parameters;
CommandSender sender;
protected Player player;
protected Gate gate;
protected boolean senderMustBePlayer = true;
protected boolean hasGateParam = true;
boolean hasGateParam = true;
protected String requiredPermission;
protected boolean needsPermissionAtCurrentLocation;
protected boolean shouldPersistToDisk;
public List<String> getAliases() {
return aliases;
}
public void execute(CommandSender sender, List<String> parameters) {
this.sender = sender;
this.parameters = parameters;
@ -66,7 +64,7 @@ public abstract class BaseCommand {
}
if (this.senderMustBePlayer) {
this.player = (Player) sender;
this.player = (Player)sender;
}
this.perform();
@ -81,23 +79,19 @@ public abstract class BaseCommand {
return config.getBoolean(ConfigurationUtil.confSaveOnChangesKey);
}
abstract protected void perform();
protected void sendMessage(String message) {
sender.sendMessage(message);
}
protected void sendMessage(List<String> messages) {
for (String message : messages) {
this.sendMessage(message);
}
}
protected boolean validateCall() {
private boolean validateCall() {
boolean allParametersThere = parameters.size() >= requiredParameters.size();
boolean senderIsPlayer = this.sender instanceof Player;
boolean hasGateParameter = false;
@ -110,17 +104,23 @@ public abstract class BaseCommand {
boolean valid;
if (this.senderMustBePlayer && !senderIsPlayer) {
sendMessage(ChatColor.RED + "This command can only be used by ingame players.");
sendMessage(ChatColor.RED + "This command can only be used by in-game players.");
valid = false;
} else {
if (!allParametersThere) {
sendMessage(ChatColor.RED + "Some parameters are missing! " + ChatColor.AQUA + "Usage: " + this.getUsageTemplate(true));
sendMessage(ChatColor.RED + "Some parameters are missing! " +
ChatColor.AQUA + "Usage: " +
this.getUsageTemplate()
);
valid = false;
} 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());
sendMessage(ChatColor.RED +
"You either provided a invalid gate or do not have permission to " +
this.helpDescription.toLowerCase()
);
valid = false;
} else {
valid = true;
@ -130,8 +130,7 @@ public abstract class BaseCommand {
return valid;
}
protected boolean setGateUsingParameter(String param) {
boolean setGateUsingParameter(String param) {
GatesManager gateManager = Plugin.getPlugin().getGatesManager();
if (!gateManager.gateExists(param)) {
@ -142,11 +141,10 @@ public abstract class BaseCommand {
}
}
/**
* This will return false if a gate is required for this command but this.gate == null.
*/
protected boolean hasPermission() {
boolean hasPermission() {
if (Plugin.getPermission() == null) { // fallback - use the standard bukkit permission system
return this.sender.hasPermission(this.requiredPermission);
}
@ -160,32 +158,35 @@ public abstract class BaseCommand {
Player p = (Player) this.sender;
boolean hasPermission = false;
if (this.requiredPermission.equals(Plugin.permissionInfo)) {
switch (this.requiredPermission) {
case Plugin.permissionInfo:
if (this.hasGateParam) {
hasPermission = this.hasPermissionAtGateLocationAndExit(p);
} else {
hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
hasPermission = hasPermissionAtPlayerLocation(p);
}
} else if (this.requiredPermission.equals(Plugin.permissionUse)) {
break;
case Plugin.permissionUse:
hasPermission = this.hasPermissionAtGateLocationAndExit(p);
} else if (this.requiredPermission.equals(Plugin.permissionManage)) {
break;
case Plugin.permissionManage:
if (this.needsPermissionAtCurrentLocation && this.hasGateParam) {
boolean hasPersmissionAtCurrentLocation = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
hasPermission = hasPersmissionAtCurrentLocation && this.hasPermissionAtGateLocationAndExit(p);
boolean hasPermissionAtCurrentLocation = hasPermissionAtPlayerLocation(p);
hasPermission = hasPermissionAtCurrentLocation && this.hasPermissionAtGateLocationAndExit(p);
} else if (this.needsPermissionAtCurrentLocation) {
hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
hasPermission = hasPermissionAtPlayerLocation(p);
} else {
hasPermission = this.hasPermissionAtGateLocationAndExit(p);
}
break;
}
return hasPermission;
}
protected boolean hasPermissionAtGateLocationAndExit(Player p) {
private boolean hasPermissionAtGateLocationAndExit(Player p) {
if (this.gate == null || p == null) {
return false;
}
@ -196,20 +197,21 @@ public abstract class BaseCommand {
return permAtLocation & permAtExit;
}
// -------------------------------------------- //
// Help and usage description
// -------------------------------------------- //
protected String getUsageTemplate(boolean withColor, boolean withDescription) {
String ret = "";
if (withColor) {
ret += ChatColor.AQUA;
private boolean hasPermissionAtPlayerLocation(Player p) {
return Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
}
/*
Help and usage description
*/
String getUsageTemplate(boolean withDescription) {
String ret = "";
ret += ChatColor.AQUA;
ret += "/" + Plugin.getPlugin().getBaseCommand() + " " + TextUtil.implode(this.getAliases(), ",") + " ";
List<String> parts = new ArrayList<String>();
List<String> parts = new ArrayList<>();
for (String requiredParameter : this.requiredParameters) {
parts.add("[" + requiredParameter + "]");
@ -219,24 +221,19 @@ public abstract class BaseCommand {
parts.add("*[" + optionalParameter + "]");
}
if (withColor) {
ret += ChatColor.DARK_AQUA;
}
ret += ChatColor.DARK_AQUA;
ret += TextUtil.implode(parts, " ");
if (withDescription) {
ret += " ";
if (withColor) {
ret += ChatColor.YELLOW;
}
ret += this.helpDescription;
}
return ret;
}
protected String getUsageTemplate(boolean withColor) {
return getUsageTemplate(withColor, false);
private String getUsageTemplate() {
return getUsageTemplate(false);
}
}

View File

@ -21,25 +21,21 @@ import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
public abstract class BaseLocationCommand extends BaseCommand {
abstract class BaseLocationCommand extends BaseCommand {
protected Location getValidPlayerLocation() {
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();
Location location = player.getLocation().clone();
Block playerBlock = location.getBlock();
Block upBlock = playerBlock.getRelative(BlockFace.UP);
if (playerBlock.getType() == Material.AIR) {
return player.getLocation();
return location;
} else if (upBlock.getType() == Material.AIR) {
return new Location(player.getLocation().getWorld(),
player.getLocation().getX(),
player.getLocation().getY() + 1,
player.getLocation().getZ(),
player.getLocation().getYaw(),
player.getLocation().getPitch());
}
return location.add(0, 1, 0);
} else {
return null;
}
}
}

View File

@ -25,36 +25,36 @@ import java.util.List;
public class CommandHelp extends BaseCommand {
public static List<List<String>> helpPages;
private static List<List<String>> helpPages;
static {
// sort the usage strings
List<String> allUsageStrings = new ArrayList<String>();
List<String> allUsageStrings = new ArrayList<>();
allUsageStrings.add(new CommandHelp().getUsageTemplate(true, true));
allUsageStrings.add(new CommandNew().getUsageTemplate(true, true));
allUsageStrings.add(new CommandRemove().getUsageTemplate(true, true));
allUsageStrings.add(new CommandLocation().getUsageTemplate(true, true));
allUsageStrings.add(new CommandExit().getUsageTemplate(true, true));
allUsageStrings.add(new CommandOpen().getUsageTemplate(true, true));
allUsageStrings.add(new CommandRename().getUsageTemplate(true, true));
allUsageStrings.add(new CommandClose().getUsageTemplate(true, true));
allUsageStrings.add(new CommandList().getUsageTemplate(true, true));
allUsageStrings.add(new CommandInfo().getUsageTemplate(true, true));
allUsageStrings.add(new CommandHide().getUsageTemplate(true, true));
allUsageStrings.add(new CommandUnhide().getUsageTemplate(true, true));
allUsageStrings.add(new CommandExitOpen().getUsageTemplate(true, true));
allUsageStrings.add(new CommandNearby().getUsageTemplate(true, true));
allUsageStrings.add(new CommandHelp().getUsageTemplate(true));
allUsageStrings.add(new CommandNew().getUsageTemplate(true));
allUsageStrings.add(new CommandRemove().getUsageTemplate(true));
allUsageStrings.add(new CommandLocation().getUsageTemplate(true));
allUsageStrings.add(new CommandExit().getUsageTemplate(true));
allUsageStrings.add(new CommandOpen().getUsageTemplate(true));
allUsageStrings.add(new CommandRename().getUsageTemplate(true));
allUsageStrings.add(new CommandClose().getUsageTemplate(true));
allUsageStrings.add(new CommandList().getUsageTemplate(true));
allUsageStrings.add(new CommandInfo().getUsageTemplate(true));
allUsageStrings.add(new CommandHide().getUsageTemplate(true));
allUsageStrings.add(new CommandUnhide().getUsageTemplate(true));
allUsageStrings.add(new CommandExitOpen().getUsageTemplate(true));
allUsageStrings.add(new CommandNearby().getUsageTemplate(true));
Collections.sort(allUsageStrings);
// put 5 commands on one page
helpPages = new ArrayList<List<String>>();
helpPages = new ArrayList<>();
while (!allUsageStrings.isEmpty()) {
int toIndex = allUsageStrings.size() >= 6 ? 5 : allUsageStrings.size();
List<String> currentHelpPage = new ArrayList<String>(allUsageStrings.subList(0, toIndex));
List<String> currentHelpPage = new ArrayList<>(allUsageStrings.subList(0, toIndex));
helpPages.add(currentHelpPage);
allUsageStrings.removeAll(currentHelpPage);
@ -92,7 +92,7 @@ public class CommandHelp extends BaseCommand {
page = 1;
}
sendMessage(TextUtil.titleize("Craft Inc. Gates Help (" + page + "/" + helpPages.size() + ")"));
sendMessage(TextUtil.titleSize("Craft Inc. Gates Help (" + page + "/" + helpPages.size() + ")"));
page -= 1;
if (page < 0 || page >= helpPages.size()) {

View File

@ -50,7 +50,7 @@ public class CommandInfo extends BaseCommand {
return;
}
sendMessage(TextUtil.titleize("Information about: '" + ChatColor.WHITE + gate.getId() + ChatColor.YELLOW + "'"));
sendMessage(TextUtil.titleSize("Information about: '" + ChatColor.WHITE + gate.getId() + ChatColor.YELLOW + "'"));
} else {
boolean senderIsPlayer = this.sender instanceof Player;
@ -69,7 +69,7 @@ public class CommandInfo extends BaseCommand {
Plugin.log(this.gate.toString());
sendMessage(TextUtil.titleize("Information about closest gate: '" + ChatColor.WHITE + gate.getId() + ChatColor.YELLOW + "'"));
sendMessage(TextUtil.titleSize("Information about closest gate: '" + ChatColor.WHITE + gate.getId() + ChatColor.YELLOW + "'"));
}
String openHiddenMessage = ChatColor.DARK_AQUA + "This gate is";

View File

@ -68,17 +68,17 @@ public class CommandList extends BaseCommand {
return;
}
String message = TextUtil.titleize("List of all gates (" + page + "/" + allPages.size() + ")") + "\n";
String message = TextUtil.titleSize("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>();
List<String> lines = new ArrayList<>();
int index = 0;
List<String> gateIdsForCurrentLine = new ArrayList<String>();
List<String> gateIdsForCurrentLine = new ArrayList<>();
int numCharactersInCurrentLine = 0;
while (index < gates.size()) {
@ -86,7 +86,7 @@ public class CommandList extends BaseCommand {
int gateIdLength = gateId.length() + 2; // actual length + comma + whitespace
if (gateIdLength > charactersPerLine && numCharactersInCurrentLine == 0) { // special case: very long gate id
gateIdsForCurrentLine = new ArrayList<String>();
gateIdsForCurrentLine = new ArrayList<>();
numCharactersInCurrentLine = 0;
while ((gateId.length() + 2) > charactersPerLine) {
@ -113,7 +113,7 @@ public class CommandList extends BaseCommand {
} else { // the current gate does not fit on the
lines.add(TextUtil.implode(gateIdsForCurrentLine, ", ") + ", ");
gateIdsForCurrentLine = new ArrayList<String>();
gateIdsForCurrentLine = new ArrayList<>();
numCharactersInCurrentLine = 0;
}
}
@ -154,7 +154,7 @@ public class CommandList extends BaseCommand {
Player p = (Player) this.sender;
// create a copy since we cannot iterate over a collection while modifying it!
Collection<Gate> gatesCopy = new ArrayList<Gate>(gates);
Collection<Gate> gatesCopy = new ArrayList<>(gates);
for (Gate gate : gatesCopy) {
if (gate.getLocation() != null) {
@ -186,7 +186,7 @@ public class CommandList extends BaseCommand {
*/
private static List<List<String>> gatesSortedByName(Collection<Gate> allGates) {
// create the lists
List<List<String>> ids = new ArrayList<List<String>>();
List<List<String>> ids = new ArrayList<>();
for (int i = 0; i < 28; i++) {
ids.add(new ArrayList<String>());
@ -232,7 +232,7 @@ public class CommandList extends BaseCommand {
}
List<List<String>> gatesSortedByName = gatesSortedByName(gates);
List<String> allPages = new ArrayList<String>();
List<String> allPages = new ArrayList<>();
int linesLeftOnPage = linesPerPage - 1;
String currentPageString = "";

View File

@ -61,7 +61,7 @@ public class CommandLocation extends BaseLocationCommand {
sendMessage(ChatColor.GREEN + "The location of '" + gate.getId() + "' is now at your current location.");
} 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));
sendMessage(new CommandOpen().getUsageTemplate(true));
} finally {
Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, oldLocation, oldGateBlockLocations, oldFrameBlocks);
GateBlockChangeSender.updateGateBlocks(gate);

View File

@ -32,7 +32,7 @@ public class CommandNearby extends BaseLocationCommand {
} else {
GateBlockChangeSender.temporaryHighlightGatesFrames(player, nearbyGates);
ArrayList<String> gateNames = new ArrayList<String>();
ArrayList<String> gateNames = new ArrayList<>();
for (Gate g : nearbyGates) {
gateNames.add(g.getId());

View File

@ -62,7 +62,7 @@ public class CommandNew extends BaseLocationCommand {
}
} else {
sendMessage(ChatColor.RED + "Your location is invalid!" + ChatColor.AQUA + "Go somewhere else and execute:");
sendMessage(new CommandLocation().getUsageTemplate(true, true));
sendMessage(new CommandLocation().getUsageTemplate(true));
}
gatesManager.handleNewGate(gate);

View File

@ -39,7 +39,7 @@ import org.bukkit.scheduler.BukkitScheduler;
public class PlayerMoveListener implements Listener {
protected HashMap<String, Long> lastNoPermissionMessages = new HashMap<String, Long>();
private HashMap<String, Long> lastNoPermissionMessages = new HashMap<>();
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerMove(PlayerMoveEvent event) {
@ -150,7 +150,7 @@ public class PlayerMoveListener implements Listener {
}
protected boolean hasPermission(final Player player, final Gate gate) {
private 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 {

View File

@ -26,11 +26,11 @@ import de.craftinc.gates.Plugin;
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";
private final static String worldKey = "world";
private final static String xKey = "x";
private final static String yKey = "y";
private final static String zKey = "z";
protected static World getWorld(final String name) throws Exception {
if (name == null) {
@ -46,7 +46,6 @@ public class LocationUtil {
return world;
}
/**
* Serializes a location. Helps storing locations inside yaml files. NOTE: We do not care about yaw
* and pitch for gate locations. So we won't serialize them.
@ -59,7 +58,7 @@ public class LocationUtil {
return null;
}
Map<String, Object> serializedLocation = new HashMap<String, Object>();
Map<String, Object> serializedLocation = new HashMap<>();
serializedLocation.put(worldKey, l.getWorld().getName());
serializedLocation.put(xKey, l.getX());
@ -69,7 +68,6 @@ public class LocationUtil {
return serializedLocation;
}
/**
* @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!

View File

@ -28,6 +28,7 @@ import java.util.logging.Level;
public class MigrationUtil {
public static boolean performMigration(int storageVersion, int currentVersion, List<Gate> gates) {
if (storageVersion == 0 && currentVersion >= 2) {
removePortalBlocks(gates);
@ -44,8 +45,7 @@ public class MigrationUtil {
}
}
protected static void removePortalBlocks(List<Gate> gates) {
private static void removePortalBlocks(List<Gate> gates) {
for (Gate g : gates) {
for (Location l : g.getGateBlockLocations()) {
@ -58,10 +58,8 @@ public class MigrationUtil {
}
}
protected static void updateAllowVehicles(List<Gate> gates) {
private static void updateAllowVehicles(List<Gate> gates) {
for (Gate g : gates) {
g.setAllowsVehicles(true);
}
}

View File

@ -37,58 +37,82 @@ public class ConfigurationUtil {
public static final String confGateMaterialKey = "gateMaterial";
public static GateMaterial getPortalMaterial() {
static GateMaterial getPortalMaterial() {
String materialString = Plugin.getPlugin().getConfig().getString(confGateMaterialKey);
GateMaterial material = new GateMaterial();
if (materialString.equals("sapling")) {
switch (materialString) {
case "sapling":
material.material = Material.SAPLING;
} else if (materialString.equals("water")) {
break;
case "water":
material.material = Material.STATIONARY_WATER;
} else if (materialString.equals("lava")) {
break;
case "lava":
material.material = Material.STATIONARY_LAVA;
} else if (materialString.equals("cobweb")) {
break;
case "cobweb":
material.material = Material.WEB;
} else if (materialString.equals("grass")) {
break;
case "grass":
material.material = Material.LONG_GRASS;
material.data = 1;
} else if (materialString.equals("dead bush")) {
break;
case "dead bush":
material.material = Material.DEAD_BUSH;
} else if (materialString.equals("dandelion")) {
break;
case "dandelion":
material.material = Material.YELLOW_FLOWER;
} else if (materialString.equals("poppy")) {
break;
case "poppy":
material.material = Material.RED_ROSE;
} else if (materialString.equals("brown mushroom")) {
break;
case "brown mushroom":
material.material = Material.BROWN_MUSHROOM;
} else if (materialString.equals("red mushroom")) {
break;
case "red mushroom":
material.material = Material.RED_MUSHROOM;
} else if (materialString.equals("torch")) {
break;
case "torch":
material.material = Material.TORCH;
} else if (materialString.equals("redstone torch (off)")) {
break;
case "redstone torch (off)":
material.material = Material.REDSTONE_TORCH_OFF;
} else if (materialString.equals("redstone torch (on)")) {
break;
case "redstone torch (on)":
material.material = Material.REDSTONE_TORCH_ON;
} else if (materialString.equals("fence")) {
break;
case "fence":
material.material = Material.FENCE;
} else if (materialString.equals("nether portal")) {
break;
case "nether portal":
material.material = Material.PORTAL;
} else if (materialString.equals("iron bars")) {
break;
case "iron bars":
material.material = Material.IRON_FENCE;
} else if (materialString.equals("glass pane")) {
break;
case "glass pane":
material.material = Material.THIN_GLASS;
} else if (materialString.equals("fence gate")) {
break;
case "fence gate":
material.material = Material.FENCE_GATE;
} else if (materialString.equals("nether brick fence")) {
break;
case "nether brick fence":
material.material = Material.NETHER_FENCE;
} else if (materialString.equals("nether wart")) {
break;
case "nether wart":
material.material = Material.NETHER_WARTS;
} else if (materialString.equals("end portal")) {
break;
case "end portal":
material.material = Material.ENDER_PORTAL;
} else if (materialString.equals("cobblestone wall")) {
break;
case "cobblestone wall":
material.material = Material.COBBLE_WALL;
} else { // fallback!
break;
default: // fallback!
material.material = Material.PORTAL;
Plugin.log(Level.WARNING, "Gate material invalid! Please check and correct your configuration file!");
break;
}
return material;

View File

@ -29,8 +29,8 @@ import de.craftinc.gates.Plugin;
public class FloodUtil {
protected static final Set<BlockFace> exp1 = new HashSet<BlockFace>();
protected static final Set<BlockFace> exp2 = new HashSet<BlockFace>();
private static final Set<BlockFace> exp1 = new HashSet<>();
private static final Set<BlockFace> exp2 = new HashSet<>();
static {
exp1.add(BlockFace.UP);
@ -53,7 +53,7 @@ public class FloodUtil {
*/
public static Set<Block> getFrame(final Set<Block> blocks) {
if (blocks == null || blocks.isEmpty()) {
return new HashSet<Block>();
return new HashSet<>();
}
// try to find gate's direction (north-south or east-west)
@ -97,8 +97,8 @@ public class FloodUtil {
}
protected static Set<Block> _getFrame(final Set<Block> blocks, final Set<BlockFace> searchDirections) {
Set<Block> frameBlocks = new HashSet<Block>();
private static Set<Block> _getFrame(final Set<Block> blocks, final Set<BlockFace> searchDirections) {
Set<Block> frameBlocks = new HashSet<>();
for (Block b : blocks) {
@ -126,7 +126,7 @@ public class FloodUtil {
throw new IllegalArgumentException("'locations' must not be 'null'");
}
Set<Block> blocks = new HashSet<Block>();
Set<Block> blocks = new HashSet<>();
for (Location l : locations) {
blocks.add(l.getBlock());
@ -167,7 +167,7 @@ public class FloodUtil {
}
protected static Set<Block> getAirFloodBlocks(final Block startBlock,
private static Set<Block> getAirFloodBlocks(final Block startBlock,
Set<Block> foundBlocks,
final Set<BlockFace> expandFaces,
int limit) {

View File

@ -97,7 +97,7 @@ public class GateBlockChangeSender {
}
protected static void dehighlightGatesFrames(final Player player, final Set<Gate> gates) {
private static void dehighlightGatesFrames(final Player player, final Set<Gate> gates) {
for (Gate g : gates) {
Set<Block> frameBlocks = g.getGateFrameBlocks();
@ -108,7 +108,7 @@ public class GateBlockChangeSender {
}
protected static void dehighlightGateFrame(final Player player, final Gate gate) {
private static void dehighlightGateFrame(final Player player, final Gate gate) {
Set<Block> frameBlocks = gate.getGateFrameBlocks();
for (Block b : frameBlocks) {
@ -207,7 +207,7 @@ public class GateBlockChangeSender {
return;
}
ArrayList<Player> playersNearby = new ArrayList<Player>();
ArrayList<Player> playersNearby = new ArrayList<>();
int searchRadius = Plugin.getPlugin().getConfig().getInt(confPlayerGateBlockUpdateRadiusKey);

View File

@ -21,7 +21,8 @@ import org.bukkit.ChatColor;
import java.util.List;
public class TextUtil {
public static String titleize(String str) {
public static String titleSize(String str) {
String center = ".[ " + ChatColor.YELLOW + str + ChatColor.GOLD + " ].";
if (center.length() >= 60) {
@ -38,7 +39,7 @@ public class TextUtil {
}
public static String repeat(String s, int times) {
private static String repeat(String s, int times) {
if (times <= 0)
return "";

View File

@ -21,6 +21,7 @@ import org.bukkit.entity.*;
public class VehicleCloner {
public static Vehicle clone(Vehicle parent, Location cloneLocation) {
Vehicle clone = cloneLocation.getWorld().spawn(cloneLocation, parent.getClass());