refactoring: make plugin work for testing (no persistence yet)
This commit is contained in:
parent
81e59b1eed
commit
87e56742d4
@ -66,13 +66,7 @@ public class Plugin extends JavaPlugin
|
|||||||
commands.add(new CommandInfo());
|
commands.add(new CommandInfo());
|
||||||
commands.add(new CommandHide());
|
commands.add(new CommandHide());
|
||||||
commands.add(new CommandUnhide());
|
commands.add(new CommandUnhide());
|
||||||
|
|
||||||
// Ensure basefolder exists!
|
|
||||||
this.getDataFolder().mkdirs();
|
|
||||||
|
|
||||||
// Load from disc
|
|
||||||
Conf.load();
|
|
||||||
Gate.load();
|
|
||||||
|
|
||||||
// Register events
|
// Register events
|
||||||
PluginManager pm = this.getServer().getPluginManager();
|
PluginManager pm = this.getServer().getPluginManager();
|
||||||
@ -106,8 +100,10 @@ public class Plugin extends JavaPlugin
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleCommand(CommandSender sender, List<String> parameters) {
|
public void handleCommand(CommandSender sender, List<String> parameters)
|
||||||
if (parameters.size() == 0) {
|
{
|
||||||
|
if (parameters.size() == 0)
|
||||||
|
{
|
||||||
this.commands.get(0).execute(sender, parameters);
|
this.commands.get(0).execute(sender, parameters);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -115,14 +111,16 @@ public class Plugin extends JavaPlugin
|
|||||||
String commandName = parameters.get(0).toLowerCase();
|
String commandName = parameters.get(0).toLowerCase();
|
||||||
parameters.remove(0);
|
parameters.remove(0);
|
||||||
|
|
||||||
for (BaseCommand fcommand : this.commands) {
|
for (BaseCommand fcommand : this.commands)
|
||||||
if (fcommand.getAliases().contains(commandName)) {
|
{
|
||||||
|
if (fcommand.getAliases().contains(commandName))
|
||||||
|
{
|
||||||
fcommand.execute(sender, parameters);
|
fcommand.execute(sender, parameters);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sender.sendMessage(Conf.colorSystem+"Unknown gate-command \""+commandName+"\". Try "+Conf.colorCommand+"/"+getBaseCommand()+" help");
|
sender.sendMessage("Unknown gate-command \"" + commandName + "\". Try " + "/" + getBaseCommand() + " help");
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
@ -5,7 +5,6 @@ import java.util.logging.Level;
|
|||||||
import org.bukkit.Chunk;
|
import org.bukkit.Chunk;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
|
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.BlockFace;
|
import org.bukkit.block.BlockFace;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@ -13,53 +12,48 @@ import org.bukkit.event.EventPriority;
|
|||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerMoveEvent;
|
import org.bukkit.event.player.PlayerMoveEvent;
|
||||||
|
|
||||||
import org.mcteam.ancientgates.Conf;
|
|
||||||
import org.mcteam.ancientgates.Gate;
|
import org.mcteam.ancientgates.Gate;
|
||||||
import org.mcteam.ancientgates.Plugin;
|
import org.mcteam.ancientgates.Plugin;
|
||||||
import org.mcteam.ancientgates.util.GeometryUtil;
|
|
||||||
|
|
||||||
|
|
||||||
public class PluginPlayerListener implements Listener
|
public class PluginPlayerListener implements Listener
|
||||||
{
|
{
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.NORMAL)
|
@EventHandler(priority = EventPriority.NORMAL)
|
||||||
public void onPlayerMove(PlayerMoveEvent event)
|
public void onPlayerMove(PlayerMoveEvent event)
|
||||||
{
|
{
|
||||||
if (event.isCancelled())
|
if (event.isCancelled())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!(event.getPlayer().hasPermission("ancientgates.use") || sender.hasPermission(permissionInfo) || sender.hasPermission(permissionManage)))
|
// check for permission
|
||||||
|
if (!event.getPlayer().hasPermission("ancientgates.use")) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Find the nearest gate!
|
||||||
|
Gate nearestGate = null;
|
||||||
|
|
||||||
|
Location playerLocation = event.getPlayer().getLocation();
|
||||||
|
World playerWorld = playerLocation.getWorld();
|
||||||
|
|
||||||
Block blockTo = event.getTo().getBlock();
|
Block blockTo = event.getTo().getBlock();
|
||||||
Block blockToUp = blockTo.getRelative(BlockFace.UP);
|
Block blockToUp = blockTo.getRelative(BlockFace.UP);
|
||||||
|
|
||||||
// Find the nearest gate!
|
|
||||||
Gate nearestGate = null;
|
|
||||||
Location playerLocation = event.getPlayer().getLocation();
|
|
||||||
|
|
||||||
for (Gate gate : Gate.getAll())
|
for (Gate gate : Gate.getAll())
|
||||||
{
|
{
|
||||||
if (gate.getFrom() == null ||
|
// Check if the gate is open and useable
|
||||||
gate.getTo() == null ||
|
World gateWorld = gate.getLocation().getWorld();
|
||||||
gate.isOpen() == false ||
|
|
||||||
!gate.getFrom().getWorld().equals(playerLocation.getWorld()))
|
if (gate.getLocation() == null || gate.getExit() == null || gate.isOpen() == false || !gateWorld.equals(playerWorld)) {
|
||||||
{
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
double distance = GeometryUtil.distanceBetweenLocations(playerLocation, gate.getFrom());
|
|
||||||
|
|
||||||
if (distance > Conf.getGateSearchRadius())
|
|
||||||
continue;
|
// Check if the location matches
|
||||||
|
for (Location l: gate.getGateBlockLocations())
|
||||||
Plugin.log(Level.ALL, "in gate search radius of " + gate.getId());
|
|
||||||
|
|
||||||
for (Integer[] blockXYZ: gate.getGateBlocks())
|
|
||||||
{
|
{
|
||||||
if ((blockTo.getX() == blockXYZ[0] || blockToUp.getX() == blockXYZ[0]) &&
|
if (locationsAreAtSamePositions(l, blockTo.getLocation()) || locationsAreAtSamePositions(l, blockToUp.getLocation()))
|
||||||
(blockTo.getY() == blockXYZ[1] || blockToUp.getY() == blockXYZ[1]) &&
|
|
||||||
(blockTo.getZ() == blockXYZ[2] || blockToUp.getZ() == blockXYZ[2]))
|
|
||||||
{
|
{
|
||||||
nearestGate = gate;
|
nearestGate = gate;
|
||||||
break;
|
break;
|
||||||
@ -69,12 +63,12 @@ public class PluginPlayerListener implements Listener
|
|||||||
|
|
||||||
if (nearestGate != null)
|
if (nearestGate != null)
|
||||||
{
|
{
|
||||||
checkChunkLoad(nearestGate.getTo().getBlock());
|
checkChunkLoad(nearestGate.getLocation().getBlock());
|
||||||
Float newYaw = nearestGate.getFrom().getYaw() - nearestGate.getTo().getYaw() + playerLocation.getYaw();
|
Float newYaw = nearestGate.getExit().getYaw() - nearestGate.getExit().getYaw() + playerLocation.getYaw();
|
||||||
Location teleportToLocation = new Location( nearestGate.getTo().getWorld(),
|
Location teleportToLocation = new Location( nearestGate.getExit().getWorld(),
|
||||||
nearestGate.getTo().getX(),
|
nearestGate.getExit().getX(),
|
||||||
nearestGate.getTo().getY(),
|
nearestGate.getExit().getY(),
|
||||||
nearestGate.getTo().getZ(),
|
nearestGate.getExit().getZ(),
|
||||||
newYaw, playerLocation.getPitch() );
|
newYaw, playerLocation.getPitch() );
|
||||||
|
|
||||||
event.getPlayer().teleport(teleportToLocation);
|
event.getPlayer().teleport(teleportToLocation);
|
||||||
@ -94,4 +88,26 @@ public class PluginPlayerListener implements Listener
|
|||||||
w.loadChunk(c);
|
w.loadChunk(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does the same as the equal method of Location but ignores fitch and yaw.
|
||||||
|
*/
|
||||||
|
private static boolean locationsAreAtSamePositions(final Location l1, final Location l2)
|
||||||
|
{
|
||||||
|
if (l1.getWorld() != l2.getWorld() && (l1.getWorld() == null || !l1.getWorld().equals(l2.getWorld()))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (Double.doubleToLongBits(l1.getX()) != Double.doubleToLongBits(l2.getX())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (Double.doubleToLongBits(l1.getY()) != Double.doubleToLongBits(l2.getY())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (Double.doubleToLongBits(l1.getZ()) != Double.doubleToLongBits(l2.getZ())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
19
src/org/mcteam/ancientgates/util/LocationSerializer.java
Normal file
19
src/org/mcteam/ancientgates/util/LocationSerializer.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package org.mcteam.ancientgates.util;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.bukkit.Location;
|
||||||
|
|
||||||
|
public class LocationSerializer
|
||||||
|
{
|
||||||
|
public static Map<String, Object> serializeLocation(Location l)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Location deserializeLocation(Map<String, Object> map)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user