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

View File

@ -240,7 +240,7 @@ public class GatesManager
fileWriter.close(); fileWriter.close();
} }
catch (IOException e) { 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 senderHasPermission = this.hasPermission();
boolean valid = false; boolean valid;
if (this.senderMustBePlayer && !senderIsPlayer) { 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 ingame players.");
@ -233,9 +233,4 @@ public abstract class BaseCommand
{ {
return getUsageTemplate(withColor, false); return getUsageTemplate(withColor, false);
} }
protected String getUsageTemplate()
{
return getUsageTemplate(true);
}
} }

View File

@ -9,7 +9,7 @@ public abstract class BaseLocationCommand extends BaseCommand
{ {
protected Location getValidPlayerLocation() protected Location getValidPlayerLocation()
{ {
// The player might stand in a halfblock or a sign or whatever // The player might stand in a half block or a sign or whatever
// Therefore we load som extra locations and blocks // Therefore we load som extra locations and blocks
Block playerBlock = player.getLocation().getBlock(); Block playerBlock = player.getLocation().getBlock();
Block upBlock = playerBlock.getRelative(BlockFace.UP); Block upBlock = playerBlock.getRelative(BlockFace.UP);

View File

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

View File

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

View File

@ -282,13 +282,7 @@ public class CommandList extends BaseCommand
} }
// cleanup // cleanup
if (linesNecessaryForCurrentGates < linesLeftOnPage) { moreGatesOnLastPage = linesNecessaryForCurrentGates >= linesLeftOnPage;
moreGatesOnLastPage = false;
}
else {
moreGatesOnLastPage = true;
}
linesLeftOnPage -= linesToFill; linesLeftOnPage -= linesToFill;
} }
} }
@ -310,7 +304,7 @@ public class CommandList extends BaseCommand
try { try {
page = new Integer(parameters.get(0)); page = new Integer(parameters.get(0));
} }
catch (Exception e) { } catch (Exception ignored) { }
return page; 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; package de.craftinc.gates.util;
import java.util.List;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material;
import java.util.List;
public class TextUtil 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) public static String implode(List<String> list, String glue)
{ {
@ -45,24 +44,6 @@ public class TextUtil
return ret; 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);
}
} }