Moves some classes to the new container package, and improves some code

This commit is contained in:
Kristian Knarvik 2021-09-18 21:51:29 +02:00
parent 4851a0b5e2
commit b191ac1de5
16 changed files with 171 additions and 131 deletions

View File

@ -41,7 +41,7 @@ public class LanguageLoader {
File tmp = new File(languageFolder, chosenLanguage + ".txt"); File tmp = new File(languageFolder, chosenLanguage + ".txt");
if (!tmp.exists()) { if (!tmp.exists()) {
if (tmp.getParentFile().mkdirs() && Stargate.debug) { if (tmp.getParentFile().mkdirs() && Stargate.debuggingEnabled) {
Stargate.log.info("[stargate] Created language folder"); Stargate.log.info("[stargate] Created language folder");
} }
} }
@ -112,7 +112,7 @@ public class LanguageLoader {
if (inputStream == null) { if (inputStream == null) {
Stargate.log.info("[stargate] The language " + language + " is not available. Falling back to " + Stargate.log.info("[stargate] The language " + language + " is not available. Falling back to " +
"english, You can add a custom language by creating a new text file in the lang directory."); "english, You can add a custom language by creating a new text file in the lang directory.");
if (Stargate.debug) { if (Stargate.debuggingEnabled) {
Stargate.log.info("[stargate] Unable to load /lang/" + language + ".txt"); Stargate.log.info("[stargate] Unable to load /lang/" + language + ".txt");
} }
return; return;
@ -242,7 +242,7 @@ public class LanguageLoader {
} }
readLanguageFile(inputStreamReader, strings); readLanguageFile(inputStreamReader, strings);
} catch (Exception e) { } catch (Exception e) {
if (Stargate.debug) { if (Stargate.debuggingEnabled) {
Stargate.log.info("Unable to load chosen language"); Stargate.log.info("Unable to load chosen language");
} }
return null; return null;

View File

@ -2,6 +2,7 @@ package net.knarcraft.stargate;
import net.knarcraft.stargate.command.CommandStarGate; import net.knarcraft.stargate.command.CommandStarGate;
import net.knarcraft.stargate.command.StarGateTabCompleter; import net.knarcraft.stargate.command.StarGateTabCompleter;
import net.knarcraft.stargate.container.BlockChangeRequest;
import net.knarcraft.stargate.event.StargateAccessEvent; import net.knarcraft.stargate.event.StargateAccessEvent;
import net.knarcraft.stargate.listener.BlockEventListener; import net.knarcraft.stargate.listener.BlockEventListener;
import net.knarcraft.stargate.listener.BungeeCordListener; import net.knarcraft.stargate.listener.BungeeCordListener;
@ -66,19 +67,18 @@ public class Stargate extends JavaPlugin {
// Temp workaround for snowmen, don't check gate entrance // Temp workaround for snowmen, don't check gate entrance
public static boolean ignoreEntrance = false; public static boolean ignoreEntrance = false;
// Used for debug // Used for debug
public static boolean debug = false; public static boolean debuggingEnabled = false;
public static boolean permDebug = false; public static boolean permissionDebuggingEnabled = false;
public static final ConcurrentLinkedQueue<Portal> activeList = new ConcurrentLinkedQueue<>(); public static final ConcurrentLinkedQueue<Portal> activeList = new ConcurrentLinkedQueue<>();
// Used for populating gate open/closed material. // Used for populating gate open/closed material.
public static final Queue<BlockChangeRequest> blockChangeRequestQueue = new LinkedList<>(); public static final Queue<BlockChangeRequest> blockChangeRequestQueue = new LinkedList<>();
// HashMap of player names for Bungee support // HashMap of player names for Bungee support
public static final Map<String, String> bungeeQueue = new HashMap<>(); public static final Map<String, String> bungeeQueue = new HashMap<>();
// World names that contain stargates //World names that contain stargates
public static final HashSet<String> managedWorlds = new HashSet<>(); public static final HashSet<String> managedWorlds = new HashSet<>();
private static String pluginVersion; private static String pluginVersion;
private static String portalFolder; private static String portalFolder;
private static String gateFolder; private static String gateFolder;
private static String langFolder;
private static String defaultGateNetwork = "central"; private static String defaultGateNetwork = "central";
private static boolean destroyExplosion = false; private static boolean destroyExplosion = false;
private static String langName = "en"; private static String langName = "en";
@ -119,7 +119,7 @@ public class Stargate extends JavaPlugin {
} }
public static void debug(String rout, String msg) { public static void debug(String rout, String msg) {
if (Stargate.debug) { if (Stargate.debuggingEnabled) {
log.info("[stargate::" + rout + "] " + msg); log.info("[stargate::" + rout + "] " + msg);
} else { } else {
log.log(Level.FINEST, "[stargate::" + rout + "] " + msg); log.log(Level.FINEST, "[stargate::" + rout + "] " + msg);
@ -211,9 +211,20 @@ public class Stargate extends JavaPlugin {
/* /*
* Check whether the player has the given permissions. * Check whether the player has the given permissions.
*/ */
public static boolean hasPerm(Player player, String perm) {
if (permDebug) /**
* Checks whether a player has the given permission
*
* <p>This is the same as player.hasPermission(), but this function allows for printing permission debugging info.</p>
*
* @param player <p>The player to check</p>
* @param perm <p>The permission to check</p>
* @return <p>True if the player has the permission</p>
*/
public static boolean hasPermission(Player player, String perm) {
if (permissionDebuggingEnabled) {
Stargate.debug("hasPerm::SuperPerm(" + player.getName() + ")", perm + " => " + player.hasPermission(perm)); Stargate.debug("hasPerm::SuperPerm(" + player.getName() + ")", perm + " => " + player.hasPermission(perm));
}
return player.hasPermission(perm); return player.hasPermission(perm);
} }
@ -229,12 +240,12 @@ public class Stargate extends JavaPlugin {
*/ */
public static boolean hasPermDeep(Player player, String permission) { public static boolean hasPermDeep(Player player, String permission) {
if (!player.isPermissionSet(permission)) { if (!player.isPermissionSet(permission)) {
if (permDebug) { if (permissionDebuggingEnabled) {
Stargate.debug("hasPermDeep::SuperPerm", permission + " => true"); Stargate.debug("hasPermDeep::SuperPerm", permission + " => true");
} }
return true; return true;
} }
if (permDebug) { if (permissionDebuggingEnabled) {
Stargate.debug("hasPermDeep::SuperPerms", permission + " => " + player.hasPermission(permission)); Stargate.debug("hasPermDeep::SuperPerms", permission + " => " + player.hasPermission(permission));
} }
return player.hasPermission(permission); return player.hasPermission(permission);
@ -243,31 +254,31 @@ public class Stargate extends JavaPlugin {
/* /*
* Check whether player can teleport to dest world * Check whether player can teleport to dest world
*/ */
public static boolean canAccessWorld(Player player, String world) { public static boolean cannotAccessWorld(Player player, String world) {
// Can use all stargate player features or access all worlds // Can use all stargate player features or access all worlds
if (hasPerm(player, "stargate.use") || hasPerm(player, "stargate.world")) { if (hasPermission(player, "stargate.use") || hasPermission(player, "stargate.world")) {
// Do a deep check to see if the player lacks this specific world node // Do a deep check to see if the player lacks this specific world node
return hasPermDeep(player, "stargate.world." + world); return !hasPermDeep(player, "stargate.world." + world);
} }
// Can access dest world // Can access dest world
return hasPerm(player, "stargate.world." + world); return !hasPermission(player, "stargate.world." + world);
} }
/* /*
* Check whether player can use network * Check whether player can use network
*/ */
public static boolean canAccessNetwork(Player player, String network) { public static boolean cannotAccessNetwork(Player player, String network) {
// Can user all stargate player features, or access all networks // Can user all stargate player features, or access all networks
if (hasPerm(player, "stargate.use") || hasPerm(player, "stargate.network")) { if (hasPermission(player, "stargate.use") || hasPermission(player, "stargate.network")) {
// Do a deep check to see if the player lacks this specific network node // Do a deep check to see if the player lacks this specific network node
return hasPermDeep(player, "stargate.network." + network); return !hasPermDeep(player, "stargate.network." + network);
} }
// Can access this network // Can access this network
if (hasPerm(player, "stargate.network." + network)) return true; if (hasPermission(player, "stargate.network." + network)) return false;
// Is able to create personal gates (Assumption is made they can also access them) // Is able to create personal gates (Assumption is made they can also access them)
String playerName = player.getName(); String playerName = player.getName();
if (playerName.length() > 11) playerName = playerName.substring(0, 11); if (playerName.length() > 11) playerName = playerName.substring(0, 11);
return network.equals(playerName) && hasPerm(player, "stargate.create.personal"); return !network.equals(playerName) || !hasPermission(player, "stargate.create.personal");
} }
/* /*
@ -275,12 +286,12 @@ public class Stargate extends JavaPlugin {
*/ */
public static boolean canAccessServer(Player player, String server) { public static boolean canAccessServer(Player player, String server) {
// Can user all stargate player features, or access all servers // Can user all stargate player features, or access all servers
if (hasPerm(player, "stargate.use") || hasPerm(player, "stargate.servers")) { if (hasPermission(player, "stargate.use") || hasPermission(player, "stargate.servers")) {
// Do a deep check to see if the player lacks this specific server node // Do a deep check to see if the player lacks this specific server node
return hasPermDeep(player, "stargate.server." + server); return hasPermDeep(player, "stargate.server." + server);
} }
// Can access this server // Can access this server
return hasPerm(player, "stargate.server." + server); return hasPermission(player, "stargate.server." + server);
} }
/* /*
@ -317,10 +328,10 @@ public class Stargate extends JavaPlugin {
if (entrancePortal.isBungee() && !Stargate.canAccessServer(player, entrancePortal.getNetwork())) { if (entrancePortal.isBungee() && !Stargate.canAccessServer(player, entrancePortal.getNetwork())) {
Stargate.debug("cannotAccessPortal", "Cannot access server"); Stargate.debug("cannotAccessPortal", "Cannot access server");
deny = true; deny = true;
} else if (!Stargate.canAccessNetwork(player, entrancePortal.getNetwork())) { } else if (Stargate.cannotAccessNetwork(player, entrancePortal.getNetwork())) {
Stargate.debug("cannotAccessPortal", "Cannot access network"); Stargate.debug("cannotAccessPortal", "Cannot access network");
deny = true; deny = true;
} else if (!entrancePortal.isBungee() && !Stargate.canAccessWorld(player, destination.getWorld().getName())) { } else if (!entrancePortal.isBungee() && Stargate.cannotAccessWorld(player, destination.getWorld().getName())) {
Stargate.debug("cannotAccessPortal", "Cannot access world"); Stargate.debug("cannotAccessPortal", "Cannot access world");
deny = true; deny = true;
} }
@ -334,7 +345,7 @@ public class Stargate extends JavaPlugin {
// This gate is free // This gate is free
if (src.isFree()) return true; if (src.isFree()) return true;
// Player gets free use // Player gets free use
if (hasPerm(player, "stargate.free") || Stargate.hasPerm(player, "stargate.free.use")) return true; if (hasPermission(player, "stargate.free") || Stargate.hasPermission(player, "stargate.free.use")) return true;
// Don't charge for free destination gates // Don't charge for free destination gates
return dest != null && !EconomyHandler.chargeFreeDestination && dest.isFree(); return dest != null && !EconomyHandler.chargeFreeDestination && dest.isFree();
} }
@ -358,7 +369,7 @@ public class Stargate extends JavaPlugin {
return true; return true;
} }
// The player is an admin with the ability to see hidden gates // The player is an admin with the ability to see hidden gates
if (hasPerm(player, "stargate.admin") || hasPerm(player, "stargate.admin.hidden")) { if (hasPermission(player, "stargate.admin") || hasPermission(player, "stargate.admin.hidden")) {
return true; return true;
} }
// The player is the owner of the gate // The player is the owner of the gate
@ -372,7 +383,7 @@ public class Stargate extends JavaPlugin {
// Check if the player is the owner of the gate // Check if the player is the owner of the gate
if (portal.isOwner(player)) return true; if (portal.isOwner(player)) return true;
// The player is an admin with the ability to use private gates // The player is an admin with the ability to use private gates
return hasPerm(player, "stargate.admin") || hasPerm(player, "stargate.admin.private"); return hasPermission(player, "stargate.admin") || hasPermission(player, "stargate.admin.private");
} }
/* /*
@ -380,11 +391,11 @@ public class Stargate extends JavaPlugin {
*/ */
public static boolean canOption(Player player, PortalOption option) { public static boolean canOption(Player player, PortalOption option) {
// Check if the player can use all options // Check if the player can use all options
if (hasPerm(player, "stargate.option") || option == PortalOption.BUNGEE) { if (hasPermission(player, "stargate.option") || option == PortalOption.BUNGEE) {
return true; return true;
} }
// Check if they can use this specific option // Check if they can use this specific option
return hasPerm(player, option.getPermissionString()); return hasPermission(player, option.getPermissionString());
} }
/* /*
@ -392,14 +403,14 @@ public class Stargate extends JavaPlugin {
*/ */
public static boolean canCreate(Player player, String network) { public static boolean canCreate(Player player, String network) {
// Check for general create // Check for general create
if (hasPerm(player, "stargate.create")) return true; if (hasPermission(player, "stargate.create")) return true;
// Check for all network create permission // Check for all network create permission
if (hasPerm(player, "stargate.create.network")) { if (hasPermission(player, "stargate.create.network")) {
// Do a deep check to see if the player lacks this specific network node // Do a deep check to see if the player lacks this specific network node
return hasPermDeep(player, "stargate.create.network." + network); return hasPermDeep(player, "stargate.create.network." + network);
} }
// Check for this specific network // Check for this specific network
return hasPerm(player, "stargate.create.network." + network); return hasPermission(player, "stargate.create.network." + network);
} }
@ -408,9 +419,9 @@ public class Stargate extends JavaPlugin {
*/ */
public static boolean canCreatePersonal(Player player) { public static boolean canCreatePersonal(Player player) {
// Check for general create // Check for general create
if (hasPerm(player, "stargate.create")) return true; if (hasPermission(player, "stargate.create")) return true;
// Check for personal // Check for personal
return hasPerm(player, "stargate.create.personal"); return hasPermission(player, "stargate.create.personal");
} }
/* /*
@ -418,14 +429,14 @@ public class Stargate extends JavaPlugin {
*/ */
public static boolean canCreateGate(Player player, String gate) { public static boolean canCreateGate(Player player, String gate) {
// Check for general create // Check for general create
if (hasPerm(player, "stargate.create")) return true; if (hasPermission(player, "stargate.create")) return true;
// Check for all gate create permissions // Check for all gate create permissions
if (hasPerm(player, "stargate.create.gate")) { if (hasPermission(player, "stargate.create.gate")) {
// Do a deep check to see if the player lacks this specific gate node // Do a deep check to see if the player lacks this specific gate node
return hasPermDeep(player, "stargate.create.gate." + gate); return hasPermDeep(player, "stargate.create.gate." + gate);
} }
// Check for this specific gate // Check for this specific gate
return hasPerm(player, "stargate.create.gate." + gate); return hasPermission(player, "stargate.create.gate." + gate);
} }
/* /*
@ -434,16 +445,16 @@ public class Stargate extends JavaPlugin {
public static boolean canDestroy(Player player, Portal portal) { public static boolean canDestroy(Player player, Portal portal) {
String network = portal.getNetwork(); String network = portal.getNetwork();
// Check for general destroy // Check for general destroy
if (hasPerm(player, "stargate.destroy")) return true; if (hasPermission(player, "stargate.destroy")) return true;
// Check for all network destroy permission // Check for all network destroy permission
if (hasPerm(player, "stargate.destroy.network")) { if (hasPermission(player, "stargate.destroy.network")) {
// Do a deep check to see if the player lacks permission for this network node // Do a deep check to see if the player lacks permission for this network node
return hasPermDeep(player, "stargate.destroy.network." + network); return hasPermDeep(player, "stargate.destroy.network." + network);
} }
// Check for this specific network // Check for this specific network
if (hasPerm(player, "stargate.destroy.network." + network)) return true; if (hasPermission(player, "stargate.destroy.network." + network)) return true;
// Check for personal gate // Check for personal gate
return portal.isOwner(player) && hasPerm(player, "stargate.destroy.personal"); return portal.isOwner(player) && hasPermission(player, "stargate.destroy.personal");
} }
/* /*
@ -483,7 +494,7 @@ public class Stargate extends JavaPlugin {
// Cost is 0 if the player owns this gate and funds go to the owner // Cost is 0 if the player owns this gate and funds go to the owner
if (src.getGate().getToOwner() && src.isOwner(player)) return 0; if (src.getGate().getToOwner() && src.isOwner(player)) return 0;
// Player gets free gate use // Player gets free gate use
if (hasPerm(player, "stargate.free") || hasPerm(player, "stargate.free.use")) return 0; if (hasPermission(player, "stargate.free") || hasPermission(player, "stargate.free.use")) return 0;
return src.getGate().getUseCost(); return src.getGate().getUseCost();
} }
@ -495,7 +506,7 @@ public class Stargate extends JavaPlugin {
// Not using Economy // Not using Economy
if (!EconomyHandler.useEconomy()) return 0; if (!EconomyHandler.useEconomy()) return 0;
// Player gets free gate destruction // Player gets free gate destruction
if (hasPerm(player, "stargate.free") || hasPerm(player, "stargate.free.create")) return 0; if (hasPermission(player, "stargate.free") || hasPermission(player, "stargate.free.create")) return 0;
return gate.getCreateCost(); return gate.getCreateCost();
} }
@ -507,7 +518,7 @@ public class Stargate extends JavaPlugin {
// Not using Economy // Not using Economy
if (!EconomyHandler.useEconomy()) return 0; if (!EconomyHandler.useEconomy()) return 0;
// Player gets free gate destruction // Player gets free gate destruction
if (hasPerm(player, "stargate.free") || hasPerm(player, "stargate.free.destroy")) return 0; if (hasPermission(player, "stargate.free") || hasPermission(player, "stargate.free.destroy")) return 0;
return gate.getDestroyCost(); return gate.getDestroyCost();
} }
@ -563,7 +574,7 @@ public class Stargate extends JavaPlugin {
String dataFolderPath = getDataFolder().getPath().replaceAll("\\\\", "/"); String dataFolderPath = getDataFolder().getPath().replaceAll("\\\\", "/");
portalFolder = dataFolderPath + "/portals/"; portalFolder = dataFolderPath + "/portals/";
gateFolder = dataFolderPath + "/gates/"; gateFolder = dataFolderPath + "/gates/";
langFolder = dataFolderPath + "/lang/"; String languageFolder = dataFolderPath + "/lang/";
pluginVersion = pluginDescriptionFile.getVersion(); pluginVersion = pluginDescriptionFile.getVersion();
@ -588,7 +599,7 @@ public class Stargate extends JavaPlugin {
} }
// It is important to load languages here, as they are used during reloadGates() // It is important to load languages here, as they are used during reloadGates()
languageLoader = new LanguageLoader(langFolder, Stargate.langName); languageLoader = new LanguageLoader(languageFolder, Stargate.langName);
this.migrate(); this.migrate();
this.loadGates(); this.loadGates();
@ -642,8 +653,8 @@ public class Stargate extends JavaPlugin {
// Sign color // Sign color
loadSignColor(newConfig.getString("signColor")); loadSignColor(newConfig.getString("signColor"));
// Debug // Debug
debug = newConfig.getBoolean("debug"); debuggingEnabled = newConfig.getBoolean("debug");
permDebug = newConfig.getBoolean("permdebug"); permissionDebuggingEnabled = newConfig.getBoolean("permdebug");
// Economy // Economy
EconomyHandler.economyEnabled = newConfig.getBoolean("useeconomy"); EconomyHandler.economyEnabled = newConfig.getBoolean("useeconomy");
EconomyHandler.setCreateCost(newConfig.getInt("createcost")); EconomyHandler.setCreateCost(newConfig.getInt("createcost"));

View File

@ -1,4 +1,4 @@
package net.knarcraft.stargate; package net.knarcraft.stargate.container;
import org.bukkit.Axis; import org.bukkit.Axis;
import org.bukkit.Material; import org.bukkit.Material;

View File

@ -1,4 +1,4 @@
package net.knarcraft.stargate; package net.knarcraft.stargate.container;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;

View File

@ -1,4 +1,4 @@
package net.knarcraft.stargate; package net.knarcraft.stargate.container;
/** /**
* This stores a block location as a vector relative to a position * This stores a block location as a vector relative to a position

View File

@ -1,4 +1,4 @@
package net.knarcraft.stargate; package net.knarcraft.stargate.container;
/** /**
* This class allows storing two values of any type * This class allows storing two values of any type

View File

@ -1,6 +1,6 @@
package net.knarcraft.stargate.listener; package net.knarcraft.stargate.listener;
import net.knarcraft.stargate.BlockLocation; import net.knarcraft.stargate.container.BlockLocation;
import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.Portal;
import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.portal.PortalHandler;
import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.Stargate;
@ -231,7 +231,7 @@ public class PlayerEventListener implements Listener {
* @return <p>True if the player should be denied</p> * @return <p>True if the player should be denied</p>
*/ */
private boolean cannotAccessPortal(Player player, Portal portal) { private boolean cannotAccessPortal(Player player, Portal portal) {
boolean deny = !Stargate.canAccessNetwork(player, portal.getNetwork()); boolean deny = Stargate.cannotAccessNetwork(player, portal.getNetwork());
if (Stargate.cannotAccessPortal(player, portal, deny)) { if (Stargate.cannotAccessPortal(player, portal, deny)) {
Stargate.sendMessage(player, Stargate.getString("denyMsg")); Stargate.sendMessage(player, Stargate.getString("denyMsg"));

View File

@ -1,7 +1,7 @@
package net.knarcraft.stargate.portal; package net.knarcraft.stargate.portal;
import net.knarcraft.stargate.BlockLocation; import net.knarcraft.stargate.container.BlockLocation;
import net.knarcraft.stargate.RelativeBlockVector; import net.knarcraft.stargate.container.RelativeBlockVector;
import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.utility.DirectionHelper; import net.knarcraft.stargate.utility.DirectionHelper;
import net.knarcraft.stargate.utility.EconomyHandler; import net.knarcraft.stargate.utility.EconomyHandler;

View File

@ -1,6 +1,6 @@
package net.knarcraft.stargate.portal; package net.knarcraft.stargate.portal;
import net.knarcraft.stargate.RelativeBlockVector; import net.knarcraft.stargate.container.RelativeBlockVector;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;

View File

@ -1,8 +1,8 @@
package net.knarcraft.stargate.portal; package net.knarcraft.stargate.portal;
import net.knarcraft.stargate.BlockLocation; import net.knarcraft.stargate.container.BlockLocation;
import net.knarcraft.stargate.BlockChangeRequest; import net.knarcraft.stargate.container.BlockChangeRequest;
import net.knarcraft.stargate.RelativeBlockVector; import net.knarcraft.stargate.container.RelativeBlockVector;
import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.event.StargateActivateEvent; import net.knarcraft.stargate.event.StargateActivateEvent;
import net.knarcraft.stargate.event.StargateCloseEvent; import net.knarcraft.stargate.event.StargateCloseEvent;

View File

@ -1,9 +1,9 @@
package net.knarcraft.stargate.portal; package net.knarcraft.stargate.portal;
import net.knarcraft.stargate.BlockLocation; import net.knarcraft.stargate.container.BlockLocation;
import net.knarcraft.stargate.RelativeBlockVector; import net.knarcraft.stargate.container.RelativeBlockVector;
import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.TwoTuple; import net.knarcraft.stargate.container.TwoTuple;
import net.knarcraft.stargate.event.StargateCreateEvent; import net.knarcraft.stargate.event.StargateCreateEvent;
import net.knarcraft.stargate.utility.DirectionHelper; import net.knarcraft.stargate.utility.DirectionHelper;
import net.knarcraft.stargate.utility.EconomyHelper; import net.knarcraft.stargate.utility.EconomyHelper;
@ -38,8 +38,8 @@ public class PortalHandler {
private static final Map<BlockLocation, Portal> lookupEntrances = new HashMap<>(); private static final Map<BlockLocation, Portal> lookupEntrances = new HashMap<>();
private static final Map<BlockLocation, Portal> lookupControls = new HashMap<>(); private static final Map<BlockLocation, Portal> lookupControls = new HashMap<>();
private static final List<Portal> allPortals = new ArrayList<>(); private static final List<Portal> allPortals = new ArrayList<>();
private static final HashMap<String, List<String>> allPortalsNet = new HashMap<>(); private static final HashMap<String, List<String>> allPortalNetworks = new HashMap<>();
private static final HashMap<String, HashMap<String, Portal>> lookupNamesNet = new HashMap<>(); private static final HashMap<String, HashMap<String, Portal>> portalLookupByNetwork = new HashMap<>();
// A list of Bungee gates // A list of Bungee gates
private static final Map<String, Portal> bungeePortals = new HashMap<>(); private static final Map<String, Portal> bungeePortals = new HashMap<>();
@ -49,7 +49,7 @@ public class PortalHandler {
} }
public static List<String> getNetwork(String network) { public static List<String> getNetwork(String network) {
return allPortalsNet.get(network.toLowerCase()); return allPortalNetworks.get(network.toLowerCase());
} }
/** /**
@ -62,7 +62,7 @@ public class PortalHandler {
*/ */
public static List<String> getDestinations(Portal entrancePortal, Player player, String network) { public static List<String> getDestinations(Portal entrancePortal, Player player, String network) {
List<String> destinations = new ArrayList<>(); List<String> destinations = new ArrayList<>();
for (String destination : allPortalsNet.get(network.toLowerCase())) { for (String destination : allPortalNetworks.get(network.toLowerCase())) {
Portal portal = getByName(destination, network); Portal portal = getByName(destination, network);
if (portal == null) { if (portal == null) {
continue; continue;
@ -89,7 +89,7 @@ public class PortalHandler {
continue; continue;
} }
// Check if this player can access the dest world // Check if this player can access the dest world
if (!Stargate.canAccessWorld(player, portal.getWorld().getName())) { if (Stargate.cannotAccessWorld(player, portal.getWorld().getName())) {
Stargate.log.info("cannot access world"); Stargate.log.info("cannot access world");
continue; continue;
} }
@ -111,10 +111,15 @@ public class PortalHandler {
Stargate.debug("Unregister", "Unregistering gate " + portal.getName()); Stargate.debug("Unregister", "Unregistering gate " + portal.getName());
portal.close(true); portal.close(true);
String portalName = portal.getName().toLowerCase();
String networkName = portal.getNetwork().toLowerCase();
//Remove portal from lookup blocks
for (BlockLocation block : portal.getFrame()) { for (BlockLocation block : portal.getFrame()) {
lookupBlocks.remove(block); lookupBlocks.remove(block);
} }
// Include the sign and button
//Remove registered info about the lookup controls and blocks
lookupBlocks.remove(portal.getId()); lookupBlocks.remove(portal.getId());
lookupControls.remove(portal.getId()); lookupControls.remove(portal.getId());
if (portal.getButton() != null) { if (portal.getButton() != null) {
@ -122,30 +127,42 @@ public class PortalHandler {
lookupControls.remove(portal.getButton()); lookupControls.remove(portal.getButton());
} }
//Remove entrances
for (BlockLocation entrance : portal.getEntrances()) { for (BlockLocation entrance : portal.getEntrances()) {
lookupEntrances.remove(entrance); lookupEntrances.remove(entrance);
} }
//Remove the portal from the list of all portals
if (removeAll) { if (removeAll) {
allPortals.remove(portal); allPortals.remove(portal);
} }
if (portal.isBungee()) { if (portal.isBungee()) {
bungeePortals.remove(portal.getName().toLowerCase()); //Remove the bungee listing
bungeePortals.remove(portalName);
} else { } else {
lookupNamesNet.get(portal.getNetwork().toLowerCase()).remove(portal.getName().toLowerCase()); //Remove from network lists
allPortalsNet.get(portal.getNetwork().toLowerCase()).remove(portal.getName().toLowerCase()); portalLookupByNetwork.get(networkName).remove(portalName);
allPortalNetworks.get(networkName).remove(portalName);
for (String originName : allPortalsNet.get(portal.getNetwork().toLowerCase())) { //Update all portals in the same network with this portal as its destination
for (String originName : allPortalNetworks.get(networkName)) {
Portal origin = getByName(originName, portal.getNetwork()); Portal origin = getByName(originName, portal.getNetwork());
if (origin == null) continue; if (origin == null || !origin.getDestinationName().equalsIgnoreCase(portalName) || !origin.isVerified()) {
if (!origin.getDestinationName().equalsIgnoreCase(portal.getName())) continue; continue;
if (!origin.isVerified()) continue; }
if (origin.isFixed()) origin.drawSign(); //Update the portal's sign
if (origin.isAlwaysOn()) origin.close(true); if (origin.isFixed()) {
origin.drawSign();
}
//Close portal without destination
if (origin.isAlwaysOn()) {
origin.close(true);
}
} }
} }
//Clear sign data
if (portal.getId().getBlock().getBlockData() instanceof WallSign) { if (portal.getId().getBlock().getBlockData() instanceof WallSign) {
Sign sign = (Sign) portal.getId().getBlock().getState(); Sign sign = (Sign) portal.getId().getBlock().getState();
sign.setLine(0, portal.getName()); sign.setLine(0, portal.getName());
@ -166,29 +183,34 @@ public class PortalHandler {
private static void registerPortal(Portal portal) { private static void registerPortal(Portal portal) {
portal.setFixed(portal.getDestinationName().length() > 0 || portal.isRandom() || portal.isBungee()); portal.setFixed(portal.getDestinationName().length() > 0 || portal.isRandom() || portal.isBungee());
String portalName = portal.getName().toLowerCase();
String networkName = portal.getNetwork().toLowerCase();
// Bungee gates are stored in their own list // Bungee gates are stored in their own list
if (portal.isBungee()) { if (portal.isBungee()) {
bungeePortals.put(portal.getName().toLowerCase(), portal); bungeePortals.put(portalName, portal);
} else { } else {
// Check if network exists in our network list //Check if network exists in the lookup list. If not, register the new network
if (!lookupNamesNet.containsKey(portal.getNetwork().toLowerCase())) { if (!portalLookupByNetwork.containsKey(networkName)) {
Stargate.debug("register", "Network " + portal.getNetwork() + " not in lookupNamesNet, adding"); Stargate.debug("register", "Network " + portal.getNetwork() + " not in lookupNamesNet, adding");
lookupNamesNet.put(portal.getNetwork().toLowerCase(), new HashMap<>()); portalLookupByNetwork.put(networkName, new HashMap<>());
} }
lookupNamesNet.get(portal.getNetwork().toLowerCase()).put(portal.getName().toLowerCase(), portal); //Check if this network exists in the network list. If not, register the network
if (!allPortalNetworks.containsKey(networkName)) {
// Check if this network exists
if (!allPortalsNet.containsKey(portal.getNetwork().toLowerCase())) {
Stargate.debug("register", "Network " + portal.getNetwork() + " not in allPortalsNet, adding"); Stargate.debug("register", "Network " + portal.getNetwork() + " not in allPortalsNet, adding");
allPortalsNet.put(portal.getNetwork().toLowerCase(), new ArrayList<>()); allPortalNetworks.put(networkName, new ArrayList<>());
} }
allPortalsNet.get(portal.getNetwork().toLowerCase()).add(portal.getName().toLowerCase());
//Register the portal
portalLookupByNetwork.get(networkName).put(portalName, portal);
allPortalNetworks.get(networkName).add(portalName);
} }
//Register all frame blocks to the lookup list
for (BlockLocation block : portal.getFrame()) { for (BlockLocation block : portal.getFrame()) {
lookupBlocks.put(block, portal); lookupBlocks.put(block, portal);
} }
// Include the sign and button //Register the sign and button to the lookup lists
lookupBlocks.put(portal.getId(), portal); lookupBlocks.put(portal.getId(), portal);
lookupControls.put(portal.getId(), portal); lookupControls.put(portal.getId(), portal);
if (portal.getButton() != null) { if (portal.getButton() != null) {
@ -196,7 +218,7 @@ public class PortalHandler {
lookupControls.put(portal.getButton(), portal); lookupControls.put(portal.getButton(), portal);
} }
//Register entrances to the lookup list
for (BlockLocation entrance : portal.getEntrances()) { for (BlockLocation entrance : portal.getEntrances()) {
lookupEntrances.put(entrance, portal); lookupEntrances.put(entrance, portal);
} }
@ -227,6 +249,7 @@ public class PortalHandler {
return null; return null;
} }
//Get necessary information from the gate's sign
BlockLocation parent = new BlockLocation(player.getWorld(), idParent.getX(), idParent.getY(), idParent.getZ()); BlockLocation parent = new BlockLocation(player.getWorld(), idParent.getX(), idParent.getY(), idParent.getZ());
BlockLocation topLeft = null; BlockLocation topLeft = null;
String name = filterName(event.getLine(0)); String name = filterName(event.getLine(0));
@ -234,6 +257,7 @@ public class PortalHandler {
String network = filterName(event.getLine(2)); String network = filterName(event.getLine(2));
String options = filterName(event.getLine(3)).toLowerCase(); String options = filterName(event.getLine(3)).toLowerCase();
//Get portal options available to the player creating the portal
Map<PortalOption, Boolean> portalOptions = getPortalOptions(player, destinationName, options); Map<PortalOption, Boolean> portalOptions = getPortalOptions(player, destinationName, options);
//Get the yaw //Get the yaw
@ -247,34 +271,38 @@ public class PortalHandler {
int modX = -direction.getBlockZ(); int modX = -direction.getBlockZ();
int modZ = direction.getBlockX(); int modZ = direction.getBlockX();
//Get all gates with the used type of control blocks
Gate[] possibleGates = GateHandler.getGatesByControlBlock(idParent); Gate[] possibleGates = GateHandler.getGatesByControlBlock(idParent);
Gate gate = null; Gate gate = null;
RelativeBlockVector buttonVector = null; RelativeBlockVector buttonVector = null;
RelativeBlockVector signVector = null;
for (Gate possibility : possibleGates) { //Try to find a matching gate configuration
if (gate != null || buttonVector != null) { for (Gate possibleGate : possibleGates) {
break; //Get gate controls
} RelativeBlockVector[] vectors = possibleGate.getLayout().getControls();
RelativeBlockVector[] vectors = possibility.getLayout().getControls();
RelativeBlockVector otherControl = null;
for (RelativeBlockVector vector : vectors) { for (RelativeBlockVector controlVector : vectors) {
BlockLocation tl = parent.modRelative(-vector.getRight(), -vector.getDepth(), -vector.getDistance(), modX, 1, modZ); //Assuming the top-left location is pointing to the gate's top-left location, check if it's a match
BlockLocation possibleTopLocation = parent.modRelative(-controlVector.getRight(),
if (gate == null) { -controlVector.getDepth(), -controlVector.getDistance(), modX, 1, modZ);
if (possibility.matches(tl, modX, modZ, true)) { if (possibleGate.matches(possibleTopLocation, modX, modZ, true)) {
gate = possibility; gate = possibleGate;
topLeft = tl; topLeft = possibleTopLocation;
signVector = controlVector;
if (otherControl != null) { break;
buttonVector = otherControl;
}
}
} else if (otherControl != null) {
buttonVector = vector;
} }
}
}
otherControl = vector; //Find the button position if a match was found
if (gate != null) {
RelativeBlockVector[] vectors = gate.getLayout().getControls();
for (RelativeBlockVector controlVector : vectors) {
if (!controlVector.equals(signVector)) {
buttonVector = controlVector;
break;
}
} }
} }
@ -283,10 +311,9 @@ public class PortalHandler {
return null; return null;
} }
// If the player is trying to create a Bungee gate without permissions, drop out here //If the player is trying to create a Bungee gate without permissions, drop out here
// Do this after the gate layout check, in the least
if (options.indexOf(PortalOption.BUNGEE.getCharacterRepresentation()) != -1) { if (options.indexOf(PortalOption.BUNGEE.getCharacterRepresentation()) != -1) {
if (!Stargate.hasPerm(player, "stargate.admin.bungee")) { if (!Stargate.hasPermission(player, "stargate.admin.bungee")) {
Stargate.sendMessage(player, Stargate.getString("bungeeDeny")); Stargate.sendMessage(player, Stargate.getString("bungeeDeny"));
return null; return null;
} }
@ -345,7 +372,7 @@ public class PortalHandler {
Portal p = getByName(destinationName, network); Portal p = getByName(destinationName, network);
if (p != null) { if (p != null) {
String world = p.getWorld().getName(); String world = p.getWorld().getName();
if (!Stargate.canAccessWorld(player, world)) { if (Stargate.cannotAccessWorld(player, world)) {
Stargate.debug("canCreate", "Player does not have access to destination world"); Stargate.debug("canCreate", "Player does not have access to destination world");
deny = true; deny = true;
denyMsg = Stargate.getString("createWorldDeny"); denyMsg = Stargate.getString("createWorldDeny");
@ -405,7 +432,7 @@ public class PortalHandler {
} }
// Check if there are too many gates in this network // Check if there are too many gates in this network
List<String> netList = allPortalsNet.get(portal.getNetwork().toLowerCase()); List<String> netList = allPortalNetworks.get(portal.getNetwork().toLowerCase());
if (Stargate.maxGates > 0 && netList != null && netList.size() >= Stargate.maxGates) { if (Stargate.maxGates > 0 && netList != null && netList.size() >= Stargate.maxGates) {
Stargate.sendMessage(player, Stargate.getString("createFull")); Stargate.sendMessage(player, Stargate.getString("createFull"));
return null; return null;
@ -451,7 +478,7 @@ public class PortalHandler {
// Don't do network stuff for bungee gates // Don't do network stuff for bungee gates
if (!portal.isBungee()) { if (!portal.isBungee()) {
// Open any always on gate pointing at this gate // Open any always on gate pointing at this gate
for (String originName : allPortalsNet.get(portal.getNetwork().toLowerCase())) { for (String originName : allPortalNetworks.get(portal.getNetwork().toLowerCase())) {
Portal origin = getByName(originName, portal.getNetwork()); Portal origin = getByName(originName, portal.getNetwork());
if (origin == null) continue; if (origin == null) continue;
if (!origin.getDestinationName().equalsIgnoreCase(portal.getName())) continue; if (!origin.getDestinationName().equalsIgnoreCase(portal.getName())) continue;
@ -513,10 +540,10 @@ public class PortalHandler {
* @return <p>The portal with the given name or null</p> * @return <p>The portal with the given name or null</p>
*/ */
public static Portal getByName(String name, String network) { public static Portal getByName(String name, String network) {
if (!lookupNamesNet.containsKey(network.toLowerCase())) { if (!portalLookupByNetwork.containsKey(network.toLowerCase())) {
return null; return null;
} }
return lookupNamesNet.get(network.toLowerCase()).get(name.toLowerCase()); return portalLookupByNetwork.get(network.toLowerCase()).get(name.toLowerCase());
} }
@ -695,11 +722,11 @@ public class PortalHandler {
*/ */
public static void clearGates() { public static void clearGates() {
lookupBlocks.clear(); lookupBlocks.clear();
lookupNamesNet.clear(); portalLookupByNetwork.clear();
lookupEntrances.clear(); lookupEntrances.clear();
lookupControls.clear(); lookupControls.clear();
allPortals.clear(); allPortals.clear();
allPortalsNet.clear(); allPortalNetworks.clear();
} }
/** /**
@ -728,16 +755,16 @@ public class PortalHandler {
List<String> portalNames = new ArrayList<>(); List<String> portalNames = new ArrayList<>();
portalsToRemove.forEach((portal) -> portalNames.add(portal.getName())); portalsToRemove.forEach((portal) -> portalNames.add(portal.getName()));
lookupBlocks.keySet().removeIf((key) -> portalsToRemove.contains(lookupBlocks.get(key))); lookupBlocks.keySet().removeIf((key) -> portalsToRemove.contains(lookupBlocks.get(key)));
lookupNamesNet.keySet().forEach((network) -> lookupNamesNet.get(network).keySet().removeIf((key) -> portalLookupByNetwork.keySet().forEach((network) -> portalLookupByNetwork.get(network).keySet().removeIf((key) ->
portalsToRemove.contains(lookupNamesNet.get(network).get(key)))); portalsToRemove.contains(portalLookupByNetwork.get(network).get(key))));
//Remove any networks with no portals //Remove any networks with no portals
lookupNamesNet.keySet().removeIf((key) -> lookupNamesNet.get(key).isEmpty()); portalLookupByNetwork.keySet().removeIf((key) -> portalLookupByNetwork.get(key).isEmpty());
lookupEntrances.keySet().removeIf((key) -> portalsToRemove.contains(lookupEntrances.get(key))); lookupEntrances.keySet().removeIf((key) -> portalsToRemove.contains(lookupEntrances.get(key)));
lookupControls.keySet().removeIf((key) -> portalsToRemove.contains(lookupControls.get(key))); lookupControls.keySet().removeIf((key) -> portalsToRemove.contains(lookupControls.get(key)));
allPortals.removeIf(portalsToRemove::contains); allPortals.removeIf(portalsToRemove::contains);
allPortalsNet.keySet().forEach((network) -> allPortalsNet.get(network).removeIf(portalNames::contains)); allPortalNetworks.keySet().forEach((network) -> allPortalNetworks.get(network).removeIf(portalNames::contains));
//Remove any networks with no portals //Remove any networks with no portals
allPortalsNet.keySet().removeIf((network) -> allPortalsNet.get(network).isEmpty()); allPortalNetworks.keySet().removeIf((network) -> allPortalNetworks.get(network).isEmpty());
} }
/** /**

View File

@ -1,6 +1,6 @@
package net.knarcraft.stargate.thread; package net.knarcraft.stargate.thread;
import net.knarcraft.stargate.BlockChangeRequest; import net.knarcraft.stargate.container.BlockChangeRequest;
import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.Stargate;
import org.bukkit.Axis; import org.bukkit.Axis;
import org.bukkit.Material; import org.bukkit.Material;

View File

@ -1,7 +1,7 @@
package net.knarcraft.stargate.utility; package net.knarcraft.stargate.utility;
import net.knarcraft.stargate.BlockLocation; import net.knarcraft.stargate.container.BlockLocation;
import net.knarcraft.stargate.RelativeBlockVector; import net.knarcraft.stargate.container.RelativeBlockVector;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;

View File

@ -2,6 +2,7 @@ package net.knarcraft.stargate;
import be.seeseemelk.mockbukkit.MockBukkit; import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.WorldMock; import be.seeseemelk.mockbukkit.WorldMock;
import net.knarcraft.stargate.container.BlockLocation;
import org.bukkit.Material; import org.bukkit.Material;
import org.junit.After; import org.junit.After;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;

View File

@ -1,5 +1,6 @@
package net.knarcraft.stargate; package net.knarcraft.stargate;
import net.knarcraft.stargate.container.RelativeBlockVector;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;

View File

@ -2,7 +2,7 @@ package net.knarcraft.stargate.portal;
import be.seeseemelk.mockbukkit.ServerMock; import be.seeseemelk.mockbukkit.ServerMock;
import be.seeseemelk.mockbukkit.WorldMock; import be.seeseemelk.mockbukkit.WorldMock;
import net.knarcraft.stargate.RelativeBlockVector; import net.knarcraft.stargate.container.RelativeBlockVector;
import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.Stargate;
import org.bukkit.Material; import org.bukkit.Material;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;