Refactoring and code cleanup.

This commit is contained in:
Tobias Ottenweller 2013-05-18 11:26:45 +02:00
parent e9027900ce
commit 040377cb36
11 changed files with 30 additions and 302 deletions

View File

@ -98,7 +98,7 @@ public class Gate implements ConfigurationSerializable
{
this.isHidden = isHidden;
if (isHidden == true) {
if (isHidden) {
emptyGate();
}
else if (isOpen()) {
@ -117,14 +117,14 @@ public class Gate implements ConfigurationSerializable
public void setOpen(boolean isOpen) throws Exception
{
if (isOpen == true && this.isOpen == false) {
if (isOpen && !this.isOpen) {
findPortalBlocks();
if (!isHidden) {
fillGate();
}
}
else if (isOpen == false && this.isOpen == true) {
else if (!isOpen && this.isOpen) {
emptyGate();
}
@ -213,7 +213,7 @@ public class Gate implements ConfigurationSerializable
}
if (isHidden == false) {
if (!isHidden) {
for (Location l : gateBlockLocations) {
if (l.getBlock().getType() == Material.AIR) {
setOpen(false);
@ -240,7 +240,7 @@ public class Gate implements ConfigurationSerializable
@SuppressWarnings("unchecked")
public Gate(Map<String, Object> map)
private Gate(Map<String, Object> map)
{
try {
id = map.get(idKey).toString();
@ -272,8 +272,6 @@ public class Gate implements ConfigurationSerializable
Plugin.log("NOTE: This gate will be removed from 'gates.yml' and added to 'invalid_gates.yml'!");
Plugin.getPlugin().getGatesManager().storeInvalidGate(map);
return;
}
}

View File

@ -240,7 +240,7 @@ public class GatesManager
fileWriter.close();
}
catch (IOException e) {
Plugin.log("ERROR: Could not save invalid gates to disk. Reason: \n" + e.getStackTrace());
Plugin.log("ERROR: Could not save invalid gates to disk. Reason: \n" + Arrays.toString(e.getStackTrace()));
}
}

View File

@ -85,7 +85,7 @@ public abstract class BaseCommand
}
boolean senderHasPermission = this.hasPermission();
boolean valid = false;
boolean valid;
if (this.senderMustBePlayer && !senderIsPlayer) {
sendMessage(ChatColor.RED + "This command can only be used by ingame players.");
@ -233,9 +233,4 @@ public abstract class BaseCommand
{
return getUsageTemplate(withColor, false);
}
protected String getUsageTemplate()
{
return getUsageTemplate(true);
}
}

View File

@ -54,7 +54,7 @@ public class CommandCreate extends BaseLocationCommand
gate.setLocation(playerLocation);
sendMessage(ChatColor.AQUA + "The gates location has been set to your current location.");
}
catch (Exception e) {}
catch (Exception ignored) {}
}
else
{

View File

@ -1,15 +1,12 @@
package de.craftinc.gates.commands;
import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.TextUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.bukkit.command.CommandSender;
import de.craftinc.gates.Gate;
import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.TextUtil;
public class CommandHelp extends BaseCommand
{
public static List<List<String>> helpPages;
@ -38,8 +35,7 @@ public class CommandHelp extends BaseCommand
// put 5 commands on one page
helpPages = new ArrayList<List<String>>();
while (!allUsageStrings.isEmpty())
{
while (!allUsageStrings.isEmpty()) {
int toIndex = allUsageStrings.size() >= 6 ? 5 : allUsageStrings.size();
List<String> currentHelpPage = new ArrayList<String>(allUsageStrings.subList(0, toIndex));
helpPages.add(currentHelpPage);
@ -66,33 +62,27 @@ public class CommandHelp extends BaseCommand
}
public boolean hasPermission(CommandSender sender, Gate gate)
{
return true;
}
public void perform()
{
int page = 1;
int page;
if (parameters.size() > 0)
{
try
{
if (parameters.size() > 0) {
try {
page = Integer.parseInt(parameters.get(0));
}
catch (NumberFormatException e)
{
catch (NumberFormatException e) {
// wasn't an integer
page = 1;
}
}
else {
page = 1;
}
sendMessage(TextUtil.titleize("Craft Inc. Gates Help (" + page + "/" + helpPages.size() + ")"));
page -= 1;
if (page < 0 || page >= helpPages.size())
{
if (page < 0 || page >= helpPages.size()) {
sendMessage("This page does not exist");
return;
}

View File

@ -282,13 +282,7 @@ public class CommandList extends BaseCommand
}
// cleanup
if (linesNecessaryForCurrentGates < linesLeftOnPage) {
moreGatesOnLastPage = false;
}
else {
moreGatesOnLastPage = true;
}
moreGatesOnLastPage = linesNecessaryForCurrentGates >= linesLeftOnPage;
linesLeftOnPage -= linesToFill;
}
}
@ -310,7 +304,7 @@ public class CommandList extends BaseCommand
try {
page = new Integer(parameters.get(0));
}
catch (Exception e) { }
catch (Exception ignored) { }
return page;
}

View File

@ -1,52 +0,0 @@
package de.craftinc.gates.listeners;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
// TODO: remove this class!!!
public class PluginBlockListener implements Listener
{
@EventHandler(priority = EventPriority.NORMAL)
public void onBlockPhysics(BlockPhysicsEvent event)
{
if (event.isCancelled())
return;
if (event.getBlock().getType() != Material.PORTAL) {
return;
}
if (isBlockInPortal(event.getBlock())) {
event.setCancelled(true);
}
}
public boolean isBlockInPortal(Block block)
{
if (block.getRelative(BlockFace.UP).getType() == Material.AIR) {
return false;
}
if (block.getRelative(BlockFace.DOWN).getType() == Material.AIR) {
return false;
}
if ( block.getRelative(BlockFace.NORTH).getType() != Material.AIR && block.getRelative(BlockFace.SOUTH).getType() != Material.AIR ) {
return true;
}
if ( block.getRelative(BlockFace.WEST).getType() != Material.AIR && block.getRelative(BlockFace.EAST).getType() != Material.AIR ) {
return true;
}
return false;
}
}

View File

@ -1,91 +0,0 @@
package de.craftinc.gates.listeners;
import java.util.HashMap;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPortalEnterEvent;
import org.bukkit.event.player.PlayerPortalEvent;
import de.craftinc.gates.Gate;
import de.craftinc.gates.util.GateUtil;
public class PluginPortalListener implements Listener
{
private HashMap<Player, Gate> currentGateAtEvent = new HashMap<Player, Gate>();
// TODO: check if this class can be deleted!
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerPortal(PlayerPortalEvent event)
{
if (event.isCancelled())
{
return;
}
Location playerLocation = event.getPlayer().getLocation();
Gate gateAtLocation = GateUtil.getGateAtPlayerLocation(playerLocation);
// If the player's gamemode is creative no gate might be found!
// It seems like players get teleported on a move event when the 'to' location is
// inside a gate. This meens the location obtained earlier is NOT inside a gate.
if (gateAtLocation == null && event.getPlayer().getGameMode() == GameMode.CREATIVE)
{
gateAtLocation = this.currentGateAtEvent.get(event.getPlayer());
}
if (gateAtLocation != null)
{
event.setCancelled(true);
}
this.currentGateAtEvent.put(event.getPlayer(), null);
}
@EventHandler(priority = EventPriority.NORMAL)
public void onEntityPortalEnterEvent(EntityPortalEnterEvent event)
{
if (event.getEntity() instanceof Player)
{
Player player = (Player)event.getEntity();
if (player.getGameMode() == GameMode.CREATIVE)
{
if (this.currentGateAtEvent.get(player) != null)
{
return;
}
Location eventLocation = event.getLocation();
Gate closestGate = GateUtil.closestGate(eventLocation);
if (closestGate != null)
{
// Make sure gate and event locations are on the same height (y-value).
// Otherwise the distance will be messed up when players are flying.
// FIX ME: this could potentially let a nearby nether portal fail!
eventLocation.setY(closestGate.getLocation().getY());
double distToClosestGate = closestGate.getLocation().distance(eventLocation);
if (distToClosestGate < 2.0) {
this.currentGateAtEvent.put(player, closestGate);
return;
}
}
}
this.currentGateAtEvent.put(player, null);
}
}
}

View File

@ -1,87 +0,0 @@
package de.craftinc.gates.util;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import de.craftinc.gates.Gate;
import de.craftinc.gates.Plugin;
public class GateUtil
{
public static Gate closestGate(Location location)
{
Gate gate = null;
double minmalDist = Double.MAX_VALUE;
for (Gate g : Plugin.getPlugin().getGatesManager().allGates()) {
if (!g.getLocation().getWorld().equals(location.getWorld()))
{
continue;
}
double tempDist = g.getLocation().distance(location);
if (tempDist < minmalDist)
{
gate = g;
minmalDist = tempDist;
}
}
return gate;
}
public static Gate getGateAtPlayerLocation(Location location)
{
Gate gate = null;
World playerWorld = location.getWorld();
// players are sometime stuck into the ground
Location locationUp = location.getBlock().getRelative(BlockFace.UP).getLocation();
for (Gate g : Plugin.getPlugin().getGatesManager().allGates())
{
if (gate != null)
{
break;
}
// Check if the gate is open and useable
if (g.getLocation() == null) {
continue;
}
World gateWorld = g.getLocation().getWorld();
if (!g.isOpen() || !gateWorld.equals(playerWorld)) {
continue;
}
// Check if the location matches
for (Location l: g.getGateBlockLocations()) {
if (LocationUtil.locationsAreAtSamePositions(l, location) || LocationUtil.locationsAreAtSamePositions(l, locationUp))
{
// Check if the gate is still valid
try {
g.validate();
gate = g;
break;
}
catch (Exception e2) {
break; // do nothing - gate got closed
}
}
}
}
return gate;
}
}

View File

@ -1,9 +1,8 @@
package de.craftinc.gates.util;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import java.util.List;
public class TextUtil
{
@ -29,7 +28,7 @@ public class TextUtil
/**
* Joins all emements of list into a single string, sperating the original strings with glue.
* Joins all elements of list into a single string, separating the original strings with glue.
*/
public static String implode(List<String> list, String glue)
{
@ -45,24 +44,6 @@ public class TextUtil
return ret;
}
/**
* Joins all emements of list into a single string.
*/
public static String implode(List<String> list) {
return implode(list, "");
}
public static String getMaterialName(Material material)
{
String ret = material.toString();
ret = ret.replace('_', ' ');
ret = ret.toLowerCase();
return ret.substring(0, 1).toUpperCase() + ret.substring(1);
}
}