diff --git a/src/main/java/net/knarcraft/stargate/Stargate.java b/src/main/java/net/knarcraft/stargate/Stargate.java index b2b428b..2f00c07 100644 --- a/src/main/java/net/knarcraft/stargate/Stargate.java +++ b/src/main/java/net/knarcraft/stargate/Stargate.java @@ -20,13 +20,13 @@ import net.knarcraft.stargate.listener.PlayerEventListener; import net.knarcraft.stargate.listener.StargateBreakListener; import net.knarcraft.stargate.listener.StargateCreateDestroyListener; import net.knarcraft.stargate.listener.StargateTeleportListener; -import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.portal.PortalRegistry; import net.knarcraft.stargate.thread.BlockChangeThread; import net.knarcraft.stargate.thread.ChunkUnloadThread; import net.knarcraft.stargate.thread.ControlBlocksUpdateThread; import net.knarcraft.stargate.thread.StarGateThread; import net.knarcraft.stargate.utility.BStatsHelper; +import net.knarcraft.stargate.utility.PortalUtil; import org.bukkit.Bukkit; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; @@ -336,7 +336,7 @@ public class Stargate extends ConfigCommentPlugin { @Override public void onDisable() { - PortalHandler.closeAllPortals(); + PortalUtil.closeAllPortals(); PortalRegistry.clearPortals(); if (stargateConfig != null) { stargateConfig.clearManagedWorlds(); diff --git a/src/main/java/net/knarcraft/stargate/config/Permission.java b/src/main/java/net/knarcraft/stargate/config/Permission.java index 2d3f6c2..fc4e7a2 100644 --- a/src/main/java/net/knarcraft/stargate/config/Permission.java +++ b/src/main/java/net/knarcraft/stargate/config/Permission.java @@ -15,7 +15,7 @@ public enum Permission { SEE_HIDDEN("admin.hidden"), USE_PRIVATE("admin.private"), - + FREE_USAGE("free.use"), FREE_DESTRUCTION("free.destroy"), FREE_CREATION("free.create"), diff --git a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java index 5a8ad9f..478b3a4 100644 --- a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java @@ -12,12 +12,12 @@ import net.knarcraft.stargate.config.formatting.Message; import net.knarcraft.stargate.config.formatting.SGFormatBuilder; import net.knarcraft.stargate.container.BlockChangeRequest; import net.knarcraft.stargate.portal.Portal; -import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.portal.PortalRegistry; import net.knarcraft.stargate.portal.property.gate.GateHandler; import net.knarcraft.stargate.thread.BlockChangeThread; import net.knarcraft.stargate.utility.BungeeHelper; import net.knarcraft.stargate.utility.PortalFileHelper; +import net.knarcraft.stargate.utility.PortalUtil; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.command.CommandSender; @@ -284,7 +284,7 @@ public final class StargateConfig { } //Force all portals to close closeAllOpenPortals(); - PortalHandler.closeAllPortals(); + PortalUtil.closeAllPortals(); //Clear queues and lists activePortalsQueue.clear(); diff --git a/src/main/java/net/knarcraft/stargate/config/addons/DynmapManager.java b/src/main/java/net/knarcraft/stargate/config/addons/DynmapManager.java index 8a85872..929df77 100644 --- a/src/main/java/net/knarcraft/stargate/config/addons/DynmapManager.java +++ b/src/main/java/net/knarcraft/stargate/config/addons/DynmapManager.java @@ -74,7 +74,7 @@ public final class DynmapManager { if (markerSet == null || Stargate.getStargateConfig().isDynmapDisabled()) { return; } - World world = portal.getWorld(); + World world = portal.getLocation().getWorld(); if (portal.getOptions().isHidden() || world == null) { return; } @@ -82,7 +82,7 @@ public final class DynmapManager { Location location; @Nullable RelativeBlockVector exit = portal.getGate().getLayout().getExit(); if (exit == null) { - location = portal.getTopLeft(); + location = portal.getLocation().getTopLeft(); } else { location = portal.getBlockAt(exit); } diff --git a/src/main/java/net/knarcraft/stargate/container/BlockChangeRequest.java b/src/main/java/net/knarcraft/stargate/container/BlockChangeRequest.java index 3603503..55d8ba7 100644 --- a/src/main/java/net/knarcraft/stargate/container/BlockChangeRequest.java +++ b/src/main/java/net/knarcraft/stargate/container/BlockChangeRequest.java @@ -2,6 +2,7 @@ package net.knarcraft.stargate.container; import org.bukkit.Axis; import org.bukkit.Material; +import org.bukkit.block.Block; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -10,7 +11,7 @@ import org.jetbrains.annotations.Nullable; */ public class BlockChangeRequest { - private final BlockLocation blockLocation; + private final Block blockLocation; private final Material newMaterial; private final Axis newAxis; @@ -21,7 +22,7 @@ public class BlockChangeRequest { * @param material
The new material to change the block to
* @param axisThe new axis to orient the block along
*/ - public BlockChangeRequest(@NotNull BlockLocation blockLocation, @NotNull Material material, @Nullable Axis axis) { + public BlockChangeRequest(@NotNull Block blockLocation, @NotNull Material material, @Nullable Axis axis) { this.blockLocation = blockLocation; newMaterial = material; newAxis = axis; @@ -33,7 +34,7 @@ public class BlockChangeRequest { * @returnThe location of the block
*/ @NotNull - public BlockLocation getBlockLocation() { + public Block getBlockLocation() { return blockLocation; } diff --git a/src/main/java/net/knarcraft/stargate/container/BlockLocation.java b/src/main/java/net/knarcraft/stargate/container/BlockLocation.java index bf84940..e38b6f1 100644 --- a/src/main/java/net/knarcraft/stargate/container/BlockLocation.java +++ b/src/main/java/net/knarcraft/stargate/container/BlockLocation.java @@ -5,10 +5,6 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.block.data.BlockData; -import org.bukkit.block.data.Directional; -import org.bukkit.block.data.type.Sign; import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -22,8 +18,6 @@ import org.jetbrains.annotations.Nullable; */ public class BlockLocation extends Location { - private BlockLocation parent = null; - /** * Creates a new block location * @@ -148,50 +142,6 @@ public class BlockLocation extends Location { return this.clone(); } - /** - * Gets this block location's parent block - * - *The parent block is the block the item at this block location is attached to. Usually this is the block a - * sign or wall sign is attached to.
- * - * @returnThis block location's parent block
- */ - @Nullable - public Block getParent() { - if (parent == null) { - findParent(); - } - if (parent == null) { - return null; - } - return parent.getBlock(); - } - - /** - * Tries to find the parent block location - * - *If this block location is a sign, the parent is the block location of the block the sign is connected to.
- */ - private void findParent() { - int offsetX = 0; - int offsetY = 0; - int offsetZ = 0; - - BlockData blockData = getBlock().getBlockData(); - if (blockData instanceof Directional) { - //Get the offset of the block "behind" this block - BlockFace facing = ((Directional) blockData).getFacing().getOppositeFace(); - offsetX = facing.getModX(); - offsetZ = facing.getModZ(); - } else if (blockData instanceof Sign) { - //Get offset the block beneath the sign - offsetY = -1; - } else { - return; - } - parent = this.makeRelativeBlockLocation(offsetX, offsetY, offsetZ); - } - @Override @NotNull public String toString() { diff --git a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java index 922bd60..96f158b 100644 --- a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java @@ -8,11 +8,11 @@ import net.knarcraft.stargate.config.formatting.Message; import net.knarcraft.stargate.config.formatting.SGFormatBuilder; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.PortalActivator; -import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.portal.teleporter.PlayerTeleporter; import net.knarcraft.stargate.utility.BungeeHelper; import net.knarcraft.stargate.utility.MaterialHelper; import net.knarcraft.stargate.utility.PermissionHelper; +import net.knarcraft.stargate.utility.PortalUtil; import net.knarcraft.stargate.utility.UUIDMigrationHelper; import net.md_5.bungee.api.ChatColor; import org.bukkit.Bukkit; @@ -70,7 +70,7 @@ public class PlayerEventListener implements Listener { return; } - Portal portal = PortalHandler.getBungeePortal(destination); + Portal portal = PortalUtil.getBungeePortal(destination); if (portal == null) { Stargate.debug("PlayerJoin", "Error fetching destination portal: " + destination); return; @@ -115,7 +115,7 @@ public class PlayerEventListener implements Listener { */ private void handleSignClick(@NotNull PlayerInteractEvent event, @NotNull Player player, @NotNull Block block, boolean leftClick) { - Portal portal = PortalHandler.getByBlock(block); + Portal portal = PortalUtil.getByBlock(block); if (portal == null) { return; } @@ -225,7 +225,7 @@ public class PlayerEventListener implements Listener { } if (MaterialHelper.isButtonCompatible(block.getType())) { - Portal portal = PortalHandler.getByBlock(block); + Portal portal = PortalUtil.getByBlock(block); if (portal == null) { return; } @@ -261,7 +261,7 @@ public class PlayerEventListener implements Listener { * @param playerThe player that clicked the block
*/ private void displayPortalInfo(@NotNull Block block, @NotNull Player player) { - Portal portal = PortalHandler.getByBlock(block); + Portal portal = PortalUtil.getByBlock(block); if (portal == null) { return; } diff --git a/src/main/java/net/knarcraft/stargate/listener/StargateBreakListener.java b/src/main/java/net/knarcraft/stargate/listener/StargateBreakListener.java index 2002474..2acdb2d 100644 --- a/src/main/java/net/knarcraft/stargate/listener/StargateBreakListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/StargateBreakListener.java @@ -2,8 +2,8 @@ package net.knarcraft.stargate.listener; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.portal.Portal; -import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.portal.PortalRegistry; +import net.knarcraft.stargate.utility.PortalUtil; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; @@ -56,7 +56,7 @@ public class StargateBreakListener implements Listener { return; } Block block = event.getBlock(); - Portal portal = PortalHandler.getByEntrance(block); + Portal portal = PortalUtil.getByEntrance(block); if (portal != null) { //Prevent blocks from being placed in the entrance, if protectEntrance is enabled, as breaking the block // would destroy the portal @@ -75,7 +75,7 @@ public class StargateBreakListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onEntityExplode(@NotNull EntityExplodeEvent event) { for (Block block : event.blockList()) { - Portal portal = PortalHandler.getByBlock(block); + Portal portal = PortalUtil.getByBlock(block); if (portal == null) { continue; } @@ -92,7 +92,7 @@ public class StargateBreakListener implements Listener { public void onCreatureSpawn(@NotNull CreatureSpawnEvent event) { //Prevent Zombified Piglins and other creatures form spawning at stargates if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.NETHER_PORTAL && - PortalHandler.getByEntrance(event.getLocation()) != null) { + PortalUtil.getByEntrance(event.getLocation()) != null) { event.setCancelled(true); Stargate.debug("EntitySpawnListener", "Prevented creature from spawning at Stargate"); } @@ -213,8 +213,8 @@ public class StargateBreakListener implements Listener { * @param blockThe block in question
*/ private boolean cancelIfAtEntrance(@NotNull Cancellable event, @NotNull Block block) { - if (PortalHandler.getByEntrance(block) != null || PortalHandler.getByControl(block) != null || - PortalHandler.getByBlock(block) != null) { + if (PortalUtil.getByEntrance(block) != null || PortalUtil.getByControl(block) != null || + PortalUtil.getByBlock(block) != null) { event.setCancelled(true); return true; } else { diff --git a/src/main/java/net/knarcraft/stargate/listener/StargateCreateDestroyListener.java b/src/main/java/net/knarcraft/stargate/listener/StargateCreateDestroyListener.java index efeaece..94984a4 100644 --- a/src/main/java/net/knarcraft/stargate/listener/StargateCreateDestroyListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/StargateCreateDestroyListener.java @@ -8,11 +8,11 @@ import net.knarcraft.stargate.container.BlockLocation; import net.knarcraft.stargate.event.StargateDestroyEvent; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.PortalCreator; -import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.portal.PortalRegistry; import net.knarcraft.stargate.utility.EconomyHelper; import net.knarcraft.stargate.utility.PermissionHelper; import net.knarcraft.stargate.utility.PortalFileHelper; +import net.knarcraft.stargate.utility.PortalUtil; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.data.type.WallSign; @@ -51,8 +51,8 @@ public class StargateCreateDestroyListener implements Listener { //Remove the sign if the no sign option is enabled if (portal.getOptions().hasNoSign()) { - Material replaceMaterial = PortalFileHelper.decideRemovalMaterial(portal.getSignLocation(), portal); - BlockChangeRequest request = new BlockChangeRequest(portal.getSignLocation(), replaceMaterial, null); + Material replaceMaterial = PortalFileHelper.decideRemovalMaterial(portal.getLocation().getSignBlock(), portal); + BlockChangeRequest request = new BlockChangeRequest(portal.getLocation().getSignBlock(), replaceMaterial, null); Stargate.addControlBlockUpdateRequest(request); } @@ -73,9 +73,9 @@ public class StargateCreateDestroyListener implements Listener { Player player = event.getPlayer(); //Decide if a portal is broken - Portal portal = PortalHandler.getByBlock(block); + Portal portal = PortalUtil.getByBlock(block); if (portal == null && Stargate.getGateConfig().protectEntrance()) { - portal = PortalHandler.getByEntrance(block); + portal = PortalUtil.getByEntrance(block); } if (portal == null) { return; diff --git a/src/main/java/net/knarcraft/stargate/listener/StargateTeleportListener.java b/src/main/java/net/knarcraft/stargate/listener/StargateTeleportListener.java index d58a3b8..c7f838f 100644 --- a/src/main/java/net/knarcraft/stargate/listener/StargateTeleportListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/StargateTeleportListener.java @@ -6,7 +6,6 @@ import net.knarcraft.stargate.config.formatting.SGFormatBuilder; import net.knarcraft.stargate.container.BlockLocation; import net.knarcraft.stargate.container.FromTheEndTeleportation; import net.knarcraft.stargate.portal.Portal; -import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.portal.teleporter.PlayerTeleporter; import net.knarcraft.stargate.portal.teleporter.VehicleTeleporter; import net.knarcraft.stargate.utility.BungeeHelper; @@ -14,6 +13,7 @@ import net.knarcraft.stargate.utility.EconomyHelper; import net.knarcraft.stargate.utility.EntityHelper; import net.knarcraft.stargate.utility.MaterialHelper; import net.knarcraft.stargate.utility.PermissionHelper; +import net.knarcraft.stargate.utility.PortalUtil; import net.knarcraft.stargate.utility.TeleportHelper; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -71,7 +71,7 @@ public class StargateTeleportListener implements Listener { if (!event.isCancelled() && (cause == PlayerTeleportEvent.TeleportCause.NETHER_PORTAL || cause == PlayerTeleportEvent.TeleportCause.END_GATEWAY || cause == PlayerTeleportEvent.TeleportCause.END_PORTAL) - && PortalHandler.getByAdjacentEntrance(event.getFrom()) != null) { + && PortalUtil.getByAdjacentEntrance(event.getFrom()) != null) { event.setCancelled(true); } } @@ -89,7 +89,7 @@ public class StargateTeleportListener implements Listener { Entity entity = event.getEntity(); //Cancel normal portal event is near a stargate - if (PortalHandler.getByAdjacentEntrance(event.getFrom(), EntityHelper.getEntityMaxSizeInt(entity)) != null) { + if (PortalUtil.getByAdjacentEntrance(event.getFrom(), EntityHelper.getEntityMaxSizeInt(entity)) != null) { event.setCancelled(true); } } @@ -110,9 +110,9 @@ public class StargateTeleportListener implements Listener { Portal entrancePortal; int entitySize = EntityHelper.getEntityMaxSizeInt(vehicle); if (EntityHelper.getEntityMaxSize(vehicle) > 1) { - entrancePortal = PortalHandler.getByAdjacentEntrance(event.getTo(), entitySize - 1); + entrancePortal = PortalUtil.getByAdjacentEntrance(event.getTo(), entitySize - 1); } else { - entrancePortal = PortalHandler.getByEntrance(event.getTo()); + entrancePortal = PortalUtil.getByEntrance(event.getTo()); } //Return if the portal cannot be teleported through @@ -142,10 +142,10 @@ public class StargateTeleportListener implements Listener { if (!isRelevantMoveEvent(event, player, fromLocation, toLocation)) { return; } - Portal entrancePortal = PortalHandler.getByEntrance(toLocation); + Portal entrancePortal = PortalUtil.getByEntrance(toLocation); //Check an additional block away in case the portal is a bungee portal using END_PORTAL if (entrancePortal == null) { - entrancePortal = PortalHandler.getByAdjacentEntrance(toLocation); + entrancePortal = PortalUtil.getByAdjacentEntrance(toLocation); // This should never realistically be null if (entrancePortal == null) { return; @@ -183,7 +183,7 @@ public class StargateTeleportListener implements Listener { return; } - Portal portal = PortalHandler.getByAdjacentEntrance(location); + Portal portal = PortalUtil.getByAdjacentEntrance(location); if (portal == null) { return; } @@ -258,8 +258,8 @@ public class StargateTeleportListener implements Listener { Stargate.debug(route, "Unable to find portal destination"); return; } - Stargate.debug("vehicleTeleport", destinationPortal.getWorld() + " " + - destinationPortal.getSignLocation()); + Stargate.debug("vehicleTeleport", destinationPortal.getLocation().getWorld() + " " + + destinationPortal.getLocation().getSignBlock()); new VehicleTeleporter(destinationPortal, vehicle).teleportEntity(entrancePortal); } } @@ -461,14 +461,14 @@ public class StargateTeleportListener implements Listener { * @returnThe portal the player entered, or null if no portal was entered
*/ private Portal getEnteredPortal(@NotNull BlockLocation toLocation, @NotNull Player player) { - Portal entrancePortal = PortalHandler.getByEntrance(toLocation); + Portal entrancePortal = PortalUtil.getByEntrance(toLocation); // Return if in an entrance if (entrancePortal != null) { return entrancePortal; } //Check an additional block away for special cases like BungeeCord portals using END_PORTAL as its material - entrancePortal = PortalHandler.getByAdjacentEntrance(toLocation); + entrancePortal = PortalUtil.getByAdjacentEntrance(toLocation); if (entrancePortal == null) { return null; } diff --git a/src/main/java/net/knarcraft/stargate/portal/Portal.java b/src/main/java/net/knarcraft/stargate/portal/Portal.java index 3a8e2ba..a510f47 100644 --- a/src/main/java/net/knarcraft/stargate/portal/Portal.java +++ b/src/main/java/net/knarcraft/stargate/portal/Portal.java @@ -1,7 +1,5 @@ package net.knarcraft.stargate.portal; -import net.knarcraft.stargate.container.BlockLocation; -import net.knarcraft.stargate.container.RelativeBlockVector; import net.knarcraft.stargate.portal.property.PortalLocation; import net.knarcraft.stargate.portal.property.PortalOption; import net.knarcraft.stargate.portal.property.PortalOptions; @@ -9,10 +7,8 @@ import net.knarcraft.stargate.portal.property.PortalOwner; import net.knarcraft.stargate.portal.property.PortalStrings; import net.knarcraft.stargate.portal.property.PortalStructure; import net.knarcraft.stargate.portal.property.gate.Gate; -import net.knarcraft.stargate.transformation.SimpleVectorOperation; -import net.knarcraft.stargate.utility.DirectionHelper; import net.md_5.bungee.api.ChatColor; -import org.bukkit.World; +import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -28,7 +24,6 @@ public class Portal { private final String cleanName; private final String network; private final String cleanNetwork; - private final SimpleVectorOperation vectorOperation; private final PortalOwner portalOwner; private boolean isRegistered; @@ -50,7 +45,7 @@ public class Portal { * @param portalOwnerThe portal's owner
* @param optionsA map containing all possible portal options, with true for the ones enabled
*/ - public Portal(@NotNull PortalLocation portalLocation, @Nullable BlockLocation button, + public Portal(@NotNull PortalLocation portalLocation, @Nullable Block button, @NotNull PortalStrings portalStrings, @NotNull Gate gate, @NotNull PortalOwner portalOwner, @NotNull MapThe world this portal belongs to
- */ - @Nullable - public World getWorld() { - return location.getWorld(); - } - - /** - * Gets the location of this portal's sign - * - * @returnThe location of this portal's sign
- */ - @NotNull - public BlockLocation getSignLocation() { - return this.location.getSignLocation(); - } - - /** - * Gets the rotation (yaw) of this portal - * - *The yaw is used to calculate all kinds of directions. See DirectionHelper to see how the yaw is used to - * calculate to/from other direction types.
- * - * @returnThe rotation (yaw) of this portal
- */ - public float getYaw() { - return this.location.getYaw(); - } - - /** - * Gets the location of the top-left block of the portal - * - * @returnThe location of the top-left portal block
- */ - @NotNull - public BlockLocation getTopLeft() { - return this.location.getTopLeft(); - } - - /** - * Gets the block at the given location relative to this portal's top-left block - * - * @param vectorThe relative block vector explaining the position of the block
- * @returnThe block at the given relative position
- */ - @NotNull - public BlockLocation getBlockAt(@NotNull RelativeBlockVector vector) { - return (BlockLocation) getTopLeft().clone().add(vectorOperation.performToRealSpaceOperation(vector.toVector())); - } - /** * Cleans a string by removing color codes, lower-casing and replacing spaces with underscores * @@ -328,13 +269,14 @@ public class Portal { */ @NotNull public static String cleanString(@NotNull String string) { + // TODO: Replace special characters such as : and . return ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', string)).toLowerCase(); } @Override @NotNull public String toString() { - return String.format("Portal [id=%s, network=%s name=%s, type=%s]", getSignLocation(), network, name, + return String.format("Portal [id=%s, network=%s name=%s, type=%s]", this.location.getSignBlock(), network, name, structure.getGate().getFilename()); } diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalActivator.java b/src/main/java/net/knarcraft/stargate/portal/PortalActivator.java index 97fcb96..5b980e2 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalActivator.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalActivator.java @@ -6,6 +6,7 @@ import net.knarcraft.stargate.config.formatting.SGFormatBuilder; import net.knarcraft.stargate.event.StargateActivateEvent; import net.knarcraft.stargate.event.StargateDeactivateEvent; import net.knarcraft.stargate.utility.ListHelper; +import net.knarcraft.stargate.utility.PortalUtil; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -74,16 +75,16 @@ public class PortalActivator { String portalNetwork = portal.getCleanNetwork(); if (portal.getOptions().isRandom()) { //Find possible destinations - ListThe gate type of the new portal
- * @param topLeftThe top-left block of the new portal
- * @param yawThe yaw when looking directly outwards from the portal
- * @param playerThe player creating the new portal
+ * @param gateThe gate type of the new portal
+ * @param portalLocationThe location of the portal to check
+ * @param playerThe player creating the new portal
* @returnTrue if a conflict was found. False otherwise
*/ - private static boolean conflictsWithExistingPortal(@NotNull Gate gate, @NotNull BlockLocation topLeft, double yaw, + private static boolean conflictsWithExistingPortal(@NotNull Gate gate, @NotNull PortalLocation portalLocation, @NotNull Player player) { - for (RelativeBlockVector borderVector : gate.getLayout().getBorder()) { - BlockLocation borderBlockLocation = topLeft.getRelativeLocation(borderVector, yaw); - if (PortalHandler.getByBlock(borderBlockLocation.getBlock()) != null) { + for (BlockVector borderVector : gate.getLayout().getBorder()) { + Block borderBlockLocation = portalLocation.getRelative(borderVector); + if (PortalUtil.getByBlock(borderBlockLocation) != null) { Stargate.debug("PortalCreator::conflictsWithExistingPortal", "Gate conflicts with existing gate"); new SGFormatBuilder(Message.CREATION_CONFLICT).error(player); diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalOpener.java b/src/main/java/net/knarcraft/stargate/portal/PortalOpener.java index 1ff461d..b210f65 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalOpener.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalOpener.java @@ -3,7 +3,6 @@ package net.knarcraft.stargate.portal; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.config.material.BukkitTagSpecifier; import net.knarcraft.stargate.container.BlockChangeRequest; -import net.knarcraft.stargate.container.BlockLocation; import net.knarcraft.stargate.event.StargateCloseEvent; import net.knarcraft.stargate.event.StargateOpenEvent; import net.knarcraft.stargate.portal.property.PortalOptions; @@ -12,6 +11,7 @@ import net.knarcraft.stargate.utility.MaterialHelper; import org.bukkit.Axis; import org.bukkit.Material; import org.bukkit.Tag; +import org.bukkit.block.Block; import org.bukkit.block.data.Orientable; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -98,10 +98,10 @@ public class PortalOpener { Material openType = ListHelper.getRandom(possibleMaterials); //Adjust orientation if applicable - Axis axis = (openType.createBlockData() instanceof Orientable) ? portal.getLocation().getRotationAxis() : null; + Axis axis = (openType.createBlockData() instanceof Orientable) ? portal.getLocation().getVectorOperation().getNormalAxis() : null; //Change the entrance blocks to the correct type - for (BlockLocation inside : portal.getStructure().getEntrances()) { + for (Block inside : portal.getStructure().getEntrances()) { Stargate.addControlBlockUpdateRequest(new BlockChangeRequest(inside, openType, axis)); } @@ -147,7 +147,7 @@ public class PortalOpener { destination.getPortalActivator().setDestination(portal); //Update the destination's sign if it exists - if (new BukkitTagSpecifier(Tag.WALL_SIGNS).asMaterials().contains(destination.getLocation().getSignLocation().getType())) { + if (new BukkitTagSpecifier(Tag.WALL_SIGNS).asMaterials().contains(destination.getLocation().getSignBlock().getType())) { destination.drawSign(); } } @@ -182,9 +182,9 @@ public class PortalOpener { Material closedType = ListHelper.getRandom(possibleMaterials); //Adjust orientation if applicable - Axis axis = (closedType.createBlockData() instanceof Orientable) ? portal.getLocation().getRotationAxis() : null; + Axis axis = (closedType.createBlockData() instanceof Orientable) ? portal.getLocation().getVectorOperation().getNormalAxis() : null; - for (BlockLocation entrance : portal.getStructure().getEntrances()) { + for (Block entrance : portal.getStructure().getEntrances()) { Stargate.addControlBlockUpdateRequest(new BlockChangeRequest(entrance, closedType, axis)); } diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java b/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java index e800829..5151cf2 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java @@ -3,10 +3,11 @@ package net.knarcraft.stargate.portal; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.config.addons.DynmapManager; import net.knarcraft.stargate.config.material.BukkitTagSpecifier; -import net.knarcraft.stargate.container.BlockLocation; import net.knarcraft.stargate.utility.PortalFileHelper; +import net.knarcraft.stargate.utility.PortalUtil; import org.bukkit.Tag; import org.bukkit.World; +import org.bukkit.block.Block; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -20,9 +21,9 @@ import java.util.Map; */ public class PortalRegistry { - private static final MapThe portal the frame block belongs to, or null
*/ @Nullable - public static Portal getPortalFromFrame(@NotNull BlockLocation blockLocation) { + public static Portal getPortalFromFrame(@NotNull Block blockLocation) { return lookupBlocks.get(blockLocation); } @@ -114,7 +115,7 @@ public class PortalRegistry { * @returnThe portal the control block belongs to, or null
*/ @Nullable - public static Portal getPortalFromControl(@NotNull BlockLocation blockLocation) { + public static Portal getPortalFromControl(@NotNull Block blockLocation) { return lookupControls.get(blockLocation); } @@ -141,7 +142,7 @@ public class PortalRegistry { * @returnA portal, or null
*/ @Nullable - public static Portal getPortalFromEntrance(@NotNull BlockLocation blockLocation) { + public static Portal getPortalFromEntrance(@NotNull Block blockLocation) { return lookupEntrances.get(blockLocation); } @@ -203,9 +204,9 @@ public class PortalRegistry { //Update all portals in the same network with this portal as its destination for (String originName : allPortalNetworks.get(networkName)) { - Portal origin = PortalHandler.getByName(originName, portal.getCleanNetwork()); + Portal origin = PortalUtil.getByName(originName, portal.getCleanNetwork()); if (origin == null || !origin.getDestinationName().equalsIgnoreCase(portalName) || - !new BukkitTagSpecifier(Tag.WALL_SIGNS).asMaterials().contains(origin.getLocation().getSignLocation().getType())) { + !new BukkitTagSpecifier(Tag.WALL_SIGNS).asMaterials().contains(origin.getLocation().getSignBlock().getType())) { continue; } //Update the portal's sign @@ -222,9 +223,7 @@ public class PortalRegistry { //Mark the portal's sign as unregistered new PortalSignDrawer(portal).drawUnregisteredSign(); - if (portal.getWorld() != null) { - PortalFileHelper.saveAllPortals(portal.getWorld()); - } + PortalFileHelper.saveAllPortals(portal.getLocation().getWorld()); portal.setRegistered(false); DynmapManager.removePortalMarker(portal); } @@ -237,22 +236,22 @@ public class PortalRegistry { */ private static void clearLookupMaps(@NotNull Portal portal, boolean removeAll) { //Remove portal from lookup blocks - for (BlockLocation block : portal.getStructure().getFrame()) { + for (Block block : portal.getStructure().getFrame()) { lookupBlocks.remove(block); } //Remove registered info about the lookup controls and blocks - lookupBlocks.remove(portal.getSignLocation()); - lookupControls.remove(portal.getSignLocation()); + lookupBlocks.remove(portal.getLocation().getSignBlock()); + lookupControls.remove(portal.getLocation().getSignBlock()); - BlockLocation button = portal.getStructure().getButton(); + Block button = portal.getStructure().getButton(); if (button != null) { lookupBlocks.remove(button); lookupControls.remove(button); } //Remove entrances - for (BlockLocation entrance : portal.getStructure().getEntrances()) { + for (Block entrance : portal.getStructure().getEntrances()) { lookupEntrances.remove(entrance); } @@ -303,23 +302,23 @@ public class PortalRegistry { } //Register all frame blocks to the lookup list - for (BlockLocation block : portal.getStructure().getFrame()) { + for (Block block : portal.getStructure().getFrame()) { lookupBlocks.put(block, portal); } //Register the sign and button to the lookup lists if (!portal.getOptions().hasNoSign()) { - lookupBlocks.put(portal.getSignLocation(), portal); - lookupControls.put(portal.getSignLocation(), portal); + lookupBlocks.put(portal.getLocation().getSignBlock(), portal); + lookupControls.put(portal.getLocation().getSignBlock(), portal); } - BlockLocation button = portal.getStructure().getButton(); + Block button = portal.getStructure().getButton(); if (button != null) { lookupBlocks.put(button, portal); lookupControls.put(button, portal); } //Register entrances to the lookup list - for (BlockLocation entrance : portal.getStructure().getEntrances()) { + for (Block entrance : portal.getStructure().getEntrances()) { lookupEntrances.put(entrance, portal); } diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java b/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java index c520a80..010ebd7 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java @@ -8,6 +8,7 @@ import net.knarcraft.stargate.config.formatting.SGFormatBuilder; import net.knarcraft.stargate.container.SignData; import net.knarcraft.stargate.portal.property.PortalLocation; import net.knarcraft.stargate.utility.PermissionHelper; +import net.knarcraft.stargate.utility.PortalUtil; import net.knarcraft.stargate.utility.SignHelper; import net.md_5.bungee.api.ChatColor; import org.bukkit.Material; @@ -130,7 +131,7 @@ public class PortalSignDrawer { */ @Nullable private Sign getSign() { - Block signBlock = portal.getSignLocation().getBlock(); + Block signBlock = portal.getLocation().getSignBlock(); BlockState state = signBlock.getState(); if (!(state instanceof Sign sign)) { if (!portal.getOptions().hasNoSign()) { @@ -272,7 +273,7 @@ public class PortalSignDrawer { ChatColor highlightColor = signData.getHighlightSignColor(); ChatColor mainColor = signData.getMainSignColor(); if (freeGatesColored) { - Portal destination = PortalHandler.getByName(portal.getDestinationName(), portal.getNetwork()); + Portal destination = PortalUtil.getByName(portal.getDestinationName(), portal.getNetwork()); boolean free = PermissionHelper.isFree(Objects.requireNonNull(portal.getActivePlayer()), portal, destination); ChatColor nameColor = (free ? freeColor : highlightColor); setLine(signData, signLineIndex, nameColor + ">" + (free ? freeColor : mainColor) + @@ -311,7 +312,7 @@ public class PortalSignDrawer { PortalActivator destinations = portal.getPortalActivator(); String destinationName = destinations.getDestinations().get(destinationIndex); if (freeGatesColored) { - Portal destination = PortalHandler.getByName(destinationName, portal.getNetwork()); + Portal destination = PortalUtil.getByName(destinationName, portal.getNetwork()); boolean free = PermissionHelper.isFree(Objects.requireNonNull(portal.getActivePlayer()), portal, destination); setLine(signData, signLineIndex, (free ? freeColor : mainColor) + translateAllColorCodes(destinationName), output); } else { @@ -365,7 +366,7 @@ public class PortalSignDrawer { private void drawFixedSign(@NotNull SignData signData, @NotNull String[] output) { ChatColor highlightColor = signData.getHighlightSignColor(); ChatColor mainColor = signData.getMainSignColor(); - Portal destinationPortal = PortalHandler.getByName(portal.getDestinationName(), portal.getCleanNetwork()); + Portal destinationPortal = PortalUtil.getByName(portal.getDestinationName(), portal.getCleanNetwork()); String destinationName = portal.getOptions().isRandom() ? new SGFormatBuilder(Message.SIGN_RANDOM).toString() : (destinationPortal != null ? destinationPortal.getName() : portal.getDestinationName()); setLine(signData, 1, highlightColor + ">" + mainColor + translateAllColorCodes(destinationName) + @@ -377,7 +378,7 @@ public class PortalSignDrawer { setLine(signData, 2, highlightColor + "(" + mainColor + translateAllColorCodes(portal.getNetwork()) + highlightColor + ")", output); } - Portal destination = PortalHandler.getByName(portal.getDestinationName(), portal.getNetwork()); + Portal destination = PortalUtil.getByName(portal.getDestinationName(), portal.getNetwork()); if (destination == null && !portal.getOptions().isRandom()) { setLine(signData, 3, errorColor + new SGFormatBuilder(Message.SIGN_DISCONNECTED).toString(), output); } else { @@ -394,7 +395,7 @@ public class PortalSignDrawer { */ public static void markPortalWithInvalidGate(@NotNull PortalLocation portalLocation, @NotNull String gateName, int lineIndex) { - BlockState blockState = portalLocation.getSignLocation().getBlock().getState(); + BlockState blockState = portalLocation.getSignBlock().getState(); if (!(blockState instanceof Sign sign)) { return; } diff --git a/src/main/java/net/knarcraft/stargate/portal/property/PortalLocation.java b/src/main/java/net/knarcraft/stargate/portal/property/PortalLocation.java index 10fa189..4f39f49 100644 --- a/src/main/java/net/knarcraft/stargate/portal/property/PortalLocation.java +++ b/src/main/java/net/knarcraft/stargate/portal/property/PortalLocation.java @@ -1,10 +1,10 @@ package net.knarcraft.stargate.portal.property; -import net.knarcraft.stargate.container.BlockLocation; -import net.knarcraft.stargate.container.RelativeBlockVector; -import org.bukkit.Axis; +import net.knarcraft.stargate.transformation.SimpleVectorOperation; import org.bukkit.World; +import org.bukkit.block.Block; import org.bukkit.block.BlockFace; +import org.bukkit.util.BlockVector; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -14,29 +14,61 @@ import org.jetbrains.annotations.Nullable; @SuppressWarnings("UnusedReturnValue") public class PortalLocation { - private BlockLocation topLeft; - private float yaw; - private BlockLocation signLocation; - private RelativeBlockVector buttonVector; - private BlockFace buttonFacing; + private @Nullable Block topLeft; + private @NotNull BlockFace facing; + private @NotNull Block signBlock; + private @Nullable Block buttonBlock; + private final @NotNull World world; + private final SimpleVectorOperation vectorOperation; + + /** + * Instantiates a new portal location + * + * @param topLeftThe top-left block of the portal
+ * @param facingThe direction the portal is facing
+ * @param signBlockThe block the sign is attached to
+ * @param buttonBlockThe block of the portal, or null if the portal doesn't need a button
+ */ + public PortalLocation(@NotNull Block topLeft, @NotNull BlockFace facing, @NotNull Block signBlock, + @Nullable Block buttonBlock) { + this.world = topLeft.getWorld(); + this.topLeft = topLeft; + this.facing = facing; + this.signBlock = signBlock; + this.buttonBlock = buttonBlock; + this.vectorOperation = new SimpleVectorOperation(facing); + } + + /** + * Instantiates a new portal location + * + * @param signBlockThe block the sign is attached to
+ * @param facingThe direction the portal is facing
+ */ + public PortalLocation(@NotNull Block signBlock, @NotNull BlockFace facing) { + this.world = signBlock.getWorld(); + this.signBlock = signBlock; + this.facing = facing; + this.vectorOperation = new SimpleVectorOperation(facing); + } /** * Gets the top-left block of the portal * * @returnThe top-left block of the portal
*/ - @NotNull - public BlockLocation getTopLeft() { + @Nullable + public Block getTopLeft() { return topLeft; } /** - * Gets the yaw for looking outwards from the portal + * Gets the block face the portal is facing towards * - * @returnThe portal's yaw
+ * @returnThe portal's facing direction
*/ - public float getYaw() { - return yaw; + public BlockFace getFacing() { + return facing; } /** @@ -45,8 +77,8 @@ public class PortalLocation { * @returnThe location of the portal's sign
*/ @NotNull - public BlockLocation getSignLocation() { - return signLocation; + public Block getSignBlock() { + return signBlock; } /** @@ -55,30 +87,8 @@ public class PortalLocation { * @returnThe relative location of the portal's button
*/ @Nullable - public RelativeBlockVector getButtonVector() { - return buttonVector; - } - - /** - * Gets the block face determining the button's direction - * - * @returnThe button's block face
- */ - @NotNull - public BlockFace getButtonFacing() { - return buttonFacing; - } - - /** - * Gets the rotation axis, which is the axis along which the gate is placed - * - *The portal's rotation axis is the cross axis of the button's axis
- * - * @returnThe portal's rotation axis
- */ - @NotNull - public Axis getRotationAxis() { - return getYaw() == 0.0F || getYaw() == 180.0F ? Axis.X : Axis.Z; + public Block getButtonBlock() { + return buttonBlock; } /** @@ -86,9 +96,19 @@ public class PortalLocation { * * @returnThe world this portal resides in
*/ - @Nullable + @NotNull public World getWorld() { - return topLeft.getWorld(); + return this.world; + } + + /** + * Gets the vector operation to use in order to convert this portal's relative vectors to the real space + * + * @returnThe vector operation
+ */ + @NotNull + public SimpleVectorOperation getVectorOperation() { + return this.vectorOperation; } /** @@ -101,59 +121,62 @@ public class PortalLocation { * @returnThe portal location Object
*/ @NotNull - public PortalLocation setTopLeft(@NotNull BlockLocation topLeft) { + public PortalLocation setTopLeft(@NotNull Block topLeft) { this.topLeft = topLeft; return this; } /** - * Sets the portal's yaw + * Sets the portal's facing direction * - *The portal's yaw is the yaw a player would get when looking directly out from the portal
+ *The portal's facing direction is the direction a player would get when looking directly out from the portal
* - * @param yawThe portal's new yaw
+ * @param facingThe portal's new facing direction
* @returnThe portal location Object
*/ @NotNull - public PortalLocation setYaw(float yaw) { - this.yaw = yaw; + public PortalLocation setFacing(@NotNull BlockFace facing) { + this.facing = facing; return this; } /** * Sets the location of the portal's sign * - * @param signLocationThe new sign location
+ * @param signBlockThe new sign location
* @returnThe portal location Object
*/ @NotNull - public PortalLocation setSignLocation(@NotNull BlockLocation signLocation) { - this.signLocation = signLocation; + public PortalLocation setSignBlock(@NotNull Block signBlock) { + this.signBlock = signBlock; return this; } /** * Sets the relative location of the portal's button * - * @param buttonVectorThe new relative button location
+ * @param buttonBlockThe new relative button location
* @returnThe portal location Object
*/ @NotNull - public PortalLocation setButtonVector(@Nullable RelativeBlockVector buttonVector) { - this.buttonVector = buttonVector; + public PortalLocation setButtonBlock(@Nullable Block buttonBlock) { + this.buttonBlock = buttonBlock; return this; } /** - * Sets the block face for the direction the portal button is facing + * Gets a block relative to this portal's top-left block * - * @param buttonFacingThe new block face of the portal's button
- * @returnThe portal location Object
+ * @param vectorThe relative vector pointing at the block
+ * @returnThe block
+ * @throws IllegalStateExceptionIf the top-left location is not set
*/ @NotNull - public PortalLocation setButtonFacing(@NotNull BlockFace buttonFacing) { - this.buttonFacing = buttonFacing; - return this; + public Block getRelative(@NotNull BlockVector vector) throws IllegalStateException { + if (this.topLeft == null) { + throw new IllegalStateException("Top-left is not set"); + } + return this.vectorOperation.getRealLocationBlock(this.topLeft, vector); } } diff --git a/src/main/java/net/knarcraft/stargate/portal/property/PortalOptions.java b/src/main/java/net/knarcraft/stargate/portal/property/PortalOptions.java index 341b528..a20216f 100644 --- a/src/main/java/net/knarcraft/stargate/portal/property/PortalOptions.java +++ b/src/main/java/net/knarcraft/stargate/portal/property/PortalOptions.java @@ -194,4 +194,14 @@ public class PortalOptions { return this.options.get(PortalOption.INVISIBLE); } + /** + * Checks whether the given portal option is true + * + * @param optionThe option to check
+ * @returnTrue if enabled for the portal
+ */ + public boolean checkOption(@NotNull PortalOption option) { + return this.options.get(option); + } + } diff --git a/src/main/java/net/knarcraft/stargate/portal/property/PortalStructure.java b/src/main/java/net/knarcraft/stargate/portal/property/PortalStructure.java index 4308888..1b40bc9 100644 --- a/src/main/java/net/knarcraft/stargate/portal/property/PortalStructure.java +++ b/src/main/java/net/knarcraft/stargate/portal/property/PortalStructure.java @@ -1,10 +1,10 @@ package net.knarcraft.stargate.portal.property; import net.knarcraft.stargate.Stargate; -import net.knarcraft.stargate.container.BlockLocation; -import net.knarcraft.stargate.container.RelativeBlockVector; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.property.gate.Gate; +import org.bukkit.block.Block; +import org.bukkit.util.BlockVector; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -18,9 +18,9 @@ public class PortalStructure { private final Portal portal; private final Gate gate; - private BlockLocation button; - private BlockLocation[] frame; - private BlockLocation[] entrances; + private Block button; + private Block[] frame; + private Block[] entrances; /** * Instantiates a new portal structure @@ -29,7 +29,7 @@ public class PortalStructure { * @param gateThe gate type used by this portal structure
* @param buttonThe real location of the portal's button
*/ - public PortalStructure(@NotNull Portal portal, @NotNull Gate gate, @Nullable BlockLocation button) { + public PortalStructure(@NotNull Portal portal, @NotNull Gate gate, @Nullable Block button) { this.portal = portal; this.gate = gate; this.button = button; @@ -51,7 +51,7 @@ public class PortalStructure { * @returnThe location of this portal's button
*/ @Nullable - public BlockLocation getButton() { + public Block getButton() { return button; } @@ -60,7 +60,7 @@ public class PortalStructure { * * @param buttonThe location of this portal's button
*/ - public void setButton(@NotNull BlockLocation button) { + public void setButton(@NotNull Block button) { this.button = button; } @@ -71,7 +71,7 @@ public class PortalStructure { */ public boolean checkIntegrity() { if (Stargate.getGateConfig().verifyPortals()) { - return gate.matches(portal.getTopLeft(), portal.getYaw()); + return gate.matches(portal.getLocation().getTopLeft(), portal.getLocation().getFacing()); } else { return true; } @@ -87,10 +87,10 @@ public class PortalStructure { * @returnA list of block locations
*/ @NotNull - private BlockLocation[] relativeBlockVectorsToBlockLocations(@NotNull RelativeBlockVector[] vectors) { - BlockLocation[] locations = new BlockLocation[vectors.length]; + private Block[] relativeBlockVectorsToBlockLocations(@NotNull BlockVector[] vectors) { + Block[] locations = new Block[vectors.length]; for (int i = 0; i < vectors.length; i++) { - locations[i] = portal.getBlockAt(vectors[i]); + locations[i] = portal.getLocation().getRelative(vectors[i]); } return locations; } @@ -101,7 +101,7 @@ public class PortalStructure { * @returnThe locations of this portal's entrances
*/ @NotNull - public BlockLocation[] getEntrances() { + public Block[] getEntrances() { if (entrances == null) { //Get the locations of the entrances once, and only if necessary as it's an expensive operation entrances = relativeBlockVectorsToBlockLocations(gate.getLayout().getEntrances()); @@ -115,7 +115,7 @@ public class PortalStructure { * @returnThe locations of this portal's frame
*/ @NotNull - public BlockLocation[] getFrame() { + public Block[] getFrame() { if (frame == null) { //Get the locations of the frame blocks once, and only if necessary as it's an expensive operation frame = relativeBlockVectorsToBlockLocations(gate.getLayout().getBorder()); diff --git a/src/main/java/net/knarcraft/stargate/portal/property/gate/Gate.java b/src/main/java/net/knarcraft/stargate/portal/property/gate/Gate.java index 7fe235c..b67dfde 100644 --- a/src/main/java/net/knarcraft/stargate/portal/property/gate/Gate.java +++ b/src/main/java/net/knarcraft/stargate/portal/property/gate/Gate.java @@ -3,10 +3,12 @@ package net.knarcraft.stargate.portal.property.gate; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.config.material.BukkitMaterialSpecifier; import net.knarcraft.stargate.config.material.MaterialSpecifier; -import net.knarcraft.stargate.container.BlockLocation; -import net.knarcraft.stargate.container.RelativeBlockVector; +import net.knarcraft.stargate.transformation.SimpleVectorOperation; import net.knarcraft.stargate.utility.MaterialHelper; import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.util.BlockVector; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -181,11 +183,11 @@ public class Gate { * Checks whether a portal's gate matches this gate type * * @param topLeftThe top-left block of the portal's gate
- * @param yawThe yaw when looking directly outwards
+ * @param facingThe direction the portal is facing
* @returnTrue if this gate matches the portal
*/ - public boolean matches(@NotNull BlockLocation topLeft, double yaw) { - return matches(topLeft, yaw, false); + public boolean matches(@NotNull Block topLeft, @NotNull BlockFace facing) { + return matches(topLeft, facing, false); } /** @@ -196,34 +198,34 @@ public class Gate { * containing AIR or WATER will cause the gate to not match. * * @param topLeftThe top-left block of the portal's gate
- * @param yawThe yaw when looking directly outwards
+ * @param facingThe direction the portal is facing
* @param onCreateWhether this is used in the context of creating a new gate
* @returnTrue if this gate matches the portal
*/ - public boolean matches(@NotNull BlockLocation topLeft, double yaw, boolean onCreate) { - return verifyGateEntrancesMatch(topLeft, yaw, onCreate) && verifyGateBorderMatches(topLeft, yaw); + public boolean matches(@NotNull Block topLeft, @NotNull BlockFace facing, boolean onCreate) { + return verifyGateEntrancesMatch(topLeft, facing, onCreate) && verifyGateBorderMatches(topLeft, facing); } /** * Verifies that all border blocks of a portal matches this gate type * - * @param topLeftThe top-left block of the portal
- * @param yawThe yaw when looking directly outwards from the portal
+ * @param topLeftThe top-left block of the portal's gate
+ * @param facingThe direction the portal is facing
* @returnTrue if all border blocks of the gate match the layout
*/ - private boolean verifyGateBorderMatches(@NotNull BlockLocation topLeft, double yaw) { - for (RelativeBlockVector borderVector : layout.getBorder()) { - int rowIndex = borderVector.right(); - int lineIndex = borderVector.down(); + private boolean verifyGateBorderMatches(@NotNull Block topLeft, @NotNull BlockFace facing) { + for (BlockVector borderVector : layout.getBorder()) { + int rowIndex = borderVector.getBlockX(); + int lineIndex = borderVector.getBlockY(); Character key = layout.getLayout()[lineIndex][rowIndex]; ListThe top-left block of this portal
- * @param yawThe yaw when looking directly outwards
+ * @param topLeftThe top-left block of the portal's gate
+ * @param facingThe direction the portal is facing
* @param onCreateWhether this is used in the context of creating a new gate
* @returnWhether this is used in the context of creating a new gate
*/ - private boolean verifyGateEntrancesMatch(@NotNull BlockLocation topLeft, double yaw, boolean onCreate) { + private boolean verifyGateEntrancesMatch(@NotNull Block topLeft, @NotNull BlockFace facing, boolean onCreate) { Stargate.debug("verifyGateEntrancesMatch", String.valueOf(topLeft)); - for (RelativeBlockVector entranceVector : layout.getEntrances()) { + for (BlockVector entranceVector : layout.getEntrances()) { Stargate.debug("verifyGateEntrancesMatch", String.valueOf(entranceVector)); - Material type = topLeft.getRelativeLocation(entranceVector, yaw).getType(); + Material type = new SimpleVectorOperation(facing).getRealLocationBlock(topLeft, entranceVector).getType(); //Ignore entrance if it's air or water, and we're creating a new gate if (onCreate && (type.isAir() || type == Material.WATER)) { diff --git a/src/main/java/net/knarcraft/stargate/portal/property/gate/GateLayout.java b/src/main/java/net/knarcraft/stargate/portal/property/gate/GateLayout.java index 1940471..61935b6 100644 --- a/src/main/java/net/knarcraft/stargate/portal/property/gate/GateLayout.java +++ b/src/main/java/net/knarcraft/stargate/portal/property/gate/GateLayout.java @@ -1,6 +1,6 @@ package net.knarcraft.stargate.portal.property.gate; -import net.knarcraft.stargate.container.RelativeBlockVector; +import org.bukkit.util.BlockVector; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -19,11 +19,11 @@ import java.util.List; public class GateLayout { private final Character[][] layout; - private final ListThe locations of entrances for this gate
*/ @NotNull - public RelativeBlockVector[] getEntrances() { + public BlockVector[] getEntrances() { return entrances; } @@ -66,7 +66,7 @@ public class GateLayout { * @returnThe locations of border blocks for this gate
*/ @NotNull - public RelativeBlockVector[] getBorder() { + public BlockVector[] getBorder() { return border; } @@ -76,7 +76,7 @@ public class GateLayout { * @returnThe exit block defined in the layout
*/ @Nullable - public RelativeBlockVector getExit() { + public BlockVector getExit() { return exitBlock; } @@ -89,7 +89,7 @@ public class GateLayout { * @returnAll possible exits
*/ @NotNull - public ListThe locations of the control blocks for this gate
*/ @NotNull - public RelativeBlockVector[] getControls() { + public BlockVector[] getControls() { return controls; } @@ -127,9 +127,9 @@ public class GateLayout { *This methods reads the layout and stores exits, entrances, border blocks and control blocks.
*/ private void readLayout() { - ListThe list of entrances to save to
* @param borderListThe list of border blocks to save to
*/ - private void readLayout(@NotNull ListThe list of border blocks to save to
*/ private void parseLayoutCharacter(@NotNull Character key, int columnIndex, int rowIndex, int[] exitDepths, - @NotNull ListWhether to flip the z-axis
+ * @param flipXAxisWhether to flip the X-axis
*/ - public void setFlipZAxis(boolean flipZAxis) { - this.flipZAxis = flipZAxis; + public void setFlipXAxis(boolean flipXAxis) { + this.flipXAxis = flipXAxis; } /** @@ -83,8 +84,8 @@ public class SimpleVectorOperation { public Vector performToAbstractSpaceOperation(@NotNull Vector vector) { Vector clone = vector.clone(); clone.rotateAroundAxis(rotationAxes.get(facing), rotationAngles.get(facing)); - if (flipZAxis) { - clone.setZ(-clone.getZ()); + if (flipXAxis) { + clone.setX(-clone.getX()); } return clone; } @@ -98,8 +99,8 @@ public class SimpleVectorOperation { @NotNull public Vector performToRealSpaceOperation(@NotNull Vector vector) { Vector clone = vector.clone(); - if (flipZAxis) { - clone.setZ(-clone.getZ()); + if (flipXAxis) { + clone.setX(-clone.getX()); } return clone.rotateAroundAxis(rotationAxes.get(facing), -rotationAngles.get(facing)); } @@ -115,6 +116,28 @@ public class SimpleVectorOperation { return performToRealSpaceOperation((Vector) vector).toBlockVector(); } + /** + * Gets the block vector pointing to the real location of the relative block vector + * + * @param topLeftThe vector of the top-left portal block
+ * @param relativeThe relative vector of a portal block
+ * @returnThe vector for the true location of the block in the real space
+ */ + public BlockVector getRealLocationVector(@NotNull BlockVector topLeft, @NotNull BlockVector relative) { + return topLeft.clone().add(this.performToRealSpaceOperation(relative)).toBlockVector(); + } + + /** + * Gets the block pointing to the real location of the relative block vector + * + * @param topLeftThe top-left portal block
+ * @param relativeThe relative vector of a portal block
+ * @returnThe corresponding block
+ */ + public Block getRealLocationBlock(@NotNull Block topLeft, @NotNull BlockVector relative) { + return topLeft.getLocation().clone().add(this.performToRealSpaceOperation(relative)).getBlock(); + } + /** * Initializes the operations used for rotating to each block-face */ diff --git a/src/main/java/net/knarcraft/stargate/utility/BStatsHelper.java b/src/main/java/net/knarcraft/stargate/utility/BStatsHelper.java index 134419b..ee9f4b0 100644 --- a/src/main/java/net/knarcraft/stargate/utility/BStatsHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/BStatsHelper.java @@ -2,7 +2,6 @@ package net.knarcraft.stargate.utility; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.config.ConfigOption; -import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.portal.property.gate.GateHandler; import org.bstats.bukkit.Metrics; import org.bstats.charts.SimplePie; @@ -40,7 +39,7 @@ public final class BStatsHelper { Metrics metrics = new Metrics(plugin, pluginId); MapThe block to get the direction of
+ * @returnThe direction the block is facing, or null if the block is not directional
+ */ + @Nullable + public static BlockFace getFacing(@NotNull Block block) { + BlockData blockData = block.getBlockData(); + if (blockData instanceof Directional directional) { + return directional.getFacing(); + } else { + return null; + } + } + /** * Gets a yaw by comparing two locations * @@ -39,6 +60,32 @@ public final class DirectionHelper { throw new IllegalArgumentException("Locations given are equal or at the same x and y axis"); } + /** + * Gets a block face by comparing two locations + * + *The block face here is the direction an observer at the from location has to look to face the to location. + * The block face is only meant to be calculated for locations where both have either the same x value or the same + * z value. Equal locations, or locations with equal x and equal z will throw an exception.
+ * + * @param fromLocationThe origin location
+ * @param toLocationThe target location, which the block face will point towards
+ * @returnThe yaw pointing from the first location to the second location
+ * @throws IllegalArgumentExceptionIf the locations are the same, or equal except y
+ */ + public static BlockFace getBlockFaceFromLocationDifference(@NotNull Location fromLocation, @NotNull Location toLocation) throws IllegalArgumentException { + Location difference = fromLocation.clone().subtract(toLocation.clone()); + if (difference.getX() > 0) { + return BlockFace.WEST; + } else if (difference.getX() < 0) { + return BlockFace.EAST; + } else if (difference.getZ() > 0) { + return BlockFace.NORTH; + } else if (difference.getZ() < 0) { + return BlockFace.SOUTH; + } + throw new IllegalArgumentException("Locations given are equal or at the same x and y axis"); + } + /** * Gets a block face given a yaw value * @@ -66,6 +113,23 @@ public final class DirectionHelper { } } + /** + * Gets a yaw from the specified block face + * + * @param blockFaceThe block face to convert into a yaw
+ * @returnThe yaw of the block face
+ * @throws IllegalArgumentExceptionIf the block face is not pointing to one of the four primary directions
+ */ + public static double getYawFromBlockFace(@NotNull BlockFace blockFace) throws IllegalArgumentException { + return switch (blockFace) { + case SOUTH -> 0; + case WEST -> 90; + case NORTH -> 180; + case EAST -> 270; + default -> throw new IllegalArgumentException("Invalid block face given. It must be one of N,S,W,E"); + }; + } + /** * Gets a direction vector given a yaw * @@ -142,6 +206,36 @@ public final class DirectionHelper { } } + /** + * Gets this block location's parent block + * + *The parent block is the block the item at this block location is attached to. Usually this is the block a + * sign or wall sign is attached to.
+ * + * @returnThis block location's parent block
+ */ + @Nullable + public static Block getParent(@NotNull Block block) { + int offsetX = 0; + int offsetY = 0; + int offsetZ = 0; + + BlockData blockData = block.getBlockData(); + if (blockData instanceof Directional) { + //Get the offset of the block "behind" this block + BlockFace facing = ((Directional) blockData).getFacing().getOppositeFace(); + offsetX = facing.getModX(); + offsetZ = facing.getModZ(); + } else if (blockData instanceof Sign) { + //Get offset the block beneath the sign + offsetY = -1; + } else { + return null; + } + + return block.getRelative(offsetX, offsetY, offsetZ); + } + /** * Normalizes a yaw to make it positive and no larger than 360 degrees * diff --git a/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java b/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java index 9e8573c..6bc2335 100644 --- a/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java @@ -145,8 +145,7 @@ public final class PermissionHelper { //If there is no destination, deny Stargate.debug(route, "Portal has no destination"); deny = true; - } else if (destination.getWorld() != null && - PermissionHelper.cannotAccessWorld(player, destination.getWorld().getName())) { + } else if (PermissionHelper.cannotAccessWorld(player, destination.getLocation().getWorld().getName())) { //If the player does not have access to the portal's world, deny Stargate.debug(route, "Cannot access world"); deny = true; diff --git a/src/main/java/net/knarcraft/stargate/utility/PortalFileHelper.java b/src/main/java/net/knarcraft/stargate/utility/PortalFileHelper.java index 4e4f14b..112836d 100644 --- a/src/main/java/net/knarcraft/stargate/utility/PortalFileHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/PortalFileHelper.java @@ -2,11 +2,8 @@ package net.knarcraft.stargate.utility; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.container.BlockChangeRequest; -import net.knarcraft.stargate.container.BlockLocation; import net.knarcraft.stargate.container.ControlBlockUpdateRequest; -import net.knarcraft.stargate.container.RelativeBlockVector; import net.knarcraft.stargate.portal.Portal; -import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.portal.PortalRegistry; import net.knarcraft.stargate.portal.property.PortalLocation; import net.knarcraft.stargate.portal.property.PortalOptions; @@ -14,16 +11,19 @@ import net.knarcraft.stargate.portal.property.PortalOwner; import net.knarcraft.stargate.portal.property.PortalStrings; import net.knarcraft.stargate.portal.property.gate.Gate; import net.knarcraft.stargate.portal.property.gate.GateHandler; +import net.knarcraft.stargate.transformation.SimpleVectorOperation; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Directional; import org.bukkit.block.data.Waterlogged; +import org.bukkit.util.BlockVector; +import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.io.BufferedWriter; import java.io.File; @@ -57,17 +57,12 @@ public final class PortalFileHelper { for (Portal portal : PortalRegistry.getAllPortals()) { //Skip portals in other worlds - if (portal.getWorld() == null) { - Stargate.logSevere(String.format("Could not save portal %s because its world is null", - portal.getName())); - } else { - String worldName = portal.getWorld().getName(); - if (!worldName.equalsIgnoreCase(world.getName())) { - continue; - } - //Save the portal - savePortal(bufferedWriter, portal); + String worldName = portal.getLocation().getWorld().getName(); + if (!worldName.equalsIgnoreCase(world.getName())) { + continue; } + //Save the portal + savePortal(bufferedWriter, portal); } bufferedWriter.close(); @@ -85,19 +80,19 @@ public final class PortalFileHelper { */ private static void savePortal(@NotNull BufferedWriter bufferedWriter, @NotNull Portal portal) throws IOException { StringBuilder builder = new StringBuilder(); - BlockLocation button = portal.getStructure().getButton(); + Block button = portal.getStructure().getButton(); //WARNING: Because of the primitive save format, any change in order will break everything! builder.append(portal.getName()).append(':'); - builder.append(portal.getSignLocation()).append(':'); + builder.append(portal.getLocation().getSignBlock()).append(':'); builder.append((button != null) ? button.toString() : "").append(':'); //Add removes config values to keep indices consistent builder.append(0).append(':'); builder.append(0).append(':'); - builder.append(portal.getYaw()).append(':'); - builder.append(portal.getTopLeft()).append(':'); + builder.append(DirectionHelper.getYawFromBlockFace(portal.getLocation().getFacing())).append(':'); + builder.append(portal.getLocation().getTopLeft()).append(':'); builder.append(portal.getGate().getFilename()).append(':'); //Only save the destination name if the gate is fixed as it doesn't matter otherwise @@ -127,11 +122,7 @@ public final class PortalFileHelper { builder.append(options.isHidden()).append(':'); builder.append(options.isAlwaysOn()).append(':'); builder.append(options.isPrivate()).append(':'); - if (portal.getWorld() != null) { - builder.append(portal.getWorld().getName()).append(':'); - } else { - builder.append(':'); - } + builder.append(portal.getLocation().getWorld().getName()).append(':'); builder.append(options.isFree()).append(':'); builder.append(options.isBackwards()).append(':'); builder.append(options.isShown()).append(':'); @@ -229,9 +220,9 @@ public final class PortalFileHelper { */ private static void doPostLoadTasks(@NotNull World world, boolean needsToSaveDatabase) { //Open any always-on portals. Do this here as it should be more efficient than in the loop. - PortalHandler.verifyAllPortals(); + PortalUtil.verifyAllPortals(); int portalCount = PortalRegistry.getAllPortals().size(); - int openCount = PortalHandler.openAlwaysOpenPortals(); + int openCount = PortalUtil.openAlwaysOpenPortals(); //Print info about loaded stargates so that admins can see if all stargates loaded Stargate.logInfo(String.format("{%s} Loaded %d stargates with %d set as always-on", world.getName(), @@ -242,8 +233,8 @@ public final class PortalFileHelper { Stargate.debug("PortalFileHelper::doPostLoadTasks::update", String.format("Queueing portal sign/button updates for %s", world)); for (Portal portal : PortalRegistry.getAllPortals()) { - if (portal.isRegistered() && portal.getWorld() != null && portal.getWorld().equals(world) && - world.getWorldBorder().isInside(portal.getSignLocation())) { + if (portal.isRegistered() && portal.getLocation().getWorld().equals(world) && + world.getWorldBorder().isInside(portal.getLocation().getSignBlock().getLocation())) { Stargate.addControlBlockUpdateRequest(new ControlBlockUpdateRequest(portal)); Stargate.debug("UpdateSignsButtons", String.format("Queued sign and button updates for portal %s", portal.getName())); @@ -267,13 +258,13 @@ public final class PortalFileHelper { private static boolean loadPortal(@NotNull String[] portalData, @NotNull World world, int lineIndex) { //Load min. required portal data String name = portalData[0]; - BlockLocation button = (!portalData[2].isEmpty()) ? new BlockLocation(world, portalData[2]) : null; + Block button = (!portalData[2].isEmpty()) ? getBlock(world, portalData[2]) : null; //Load the portal's location - PortalLocation portalLocation = new PortalLocation(); - portalLocation.setSignLocation(new BlockLocation(world, portalData[1])); - portalLocation.setYaw(Float.parseFloat(portalData[5])); - portalLocation.setTopLeft(new BlockLocation(world, portalData[6])); + + + PortalLocation portalLocation = new PortalLocation(getBlock(world, portalData[6]), + DirectionHelper.getBlockFaceFromYaw(Float.parseFloat(portalData[5])), getBlock(world, portalData[1]), button); //Check if the portal's gate type exists and is loaded Gate gate = GateHandler.getGateByName(portalData[7]); @@ -295,11 +286,11 @@ public final class PortalFileHelper { //Create the new portal PortalStrings portalStrings = new PortalStrings(name, network, destination); Portal portal = new Portal(portalLocation, button, portalStrings, gate, owner, - PortalHandler.getPortalOptions(portalData)); + PortalUtil.getPortalOptions(portalData)); //Register the portal, and close it in case it wasn't properly closed when the server stopped boolean buttonLocationChanged = updateButtonVector(portal); - PortalHandler.registerPortal(portal); + PortalUtil.registerPortal(portal); portal.getPortalOpener().closePortal(true); return buttonLocationChanged; } @@ -307,15 +298,16 @@ public final class PortalFileHelper { /** * Decides the material to use for removing a portal's button/sign * - * @param locationThe location of the button/sign to replace
- * @param portalThe portal the button/sign belongs to
+ * @param blockThe location of the button/sign to replace
+ * @param portalThe portal the button/sign belongs to
* @returnThe material to use for removing the button/sign
*/ @NotNull - public static Material decideRemovalMaterial(@NotNull BlockLocation location, @NotNull Portal portal) { + public static Material decideRemovalMaterial(@NotNull Block block, @NotNull Portal portal) { //Get the blocks to each side of the location - Location leftLocation = location.getRelativeLocation(-1, 0, 0, portal.getYaw()); - Location rightLocation = location.getRelativeLocation(1, 0, 0, portal.getYaw()); + SimpleVectorOperation vectorOperation = portal.getLocation().getVectorOperation(); + Location leftLocation = block.getLocation().clone().add(vectorOperation.performToRealSpaceOperation(new Vector(-1, 0, 0))); + Location rightLocation = block.getLocation().clone().add(vectorOperation.performToRealSpaceOperation(new Vector(1, 0, 0))); //If the block is water or is waterlogged, assume the portal is underwater if (isUnderwater(leftLocation) || isUnderwater(rightLocation)) { @@ -349,15 +341,12 @@ public final class PortalFileHelper { * @returnTrue if the calculated button location is not the same as the one in the portal file
*/ private static boolean updateButtonVector(@NotNull Portal portal) { - for (RelativeBlockVector control : portal.getGate().getLayout().getControls()) { - BlockLocation controlLocation = portal.getLocation().getTopLeft().getRelativeLocation(control, - portal.getYaw()); - BlockLocation buttonLocation = controlLocation.getRelativeLocation( - new RelativeBlockVector(0, 0, 1), portal.getYaw()); - if (!buttonLocation.equals(portal.getLocation().getSignLocation())) { - portal.getLocation().setButtonVector(control); + for (BlockVector control : portal.getGate().getLayout().getControls()) { + Block buttonLocation = portal.getLocation().getRelative(control.clone().add(new Vector(0, 0, 1)).toBlockVector()); + if (!buttonLocation.equals(portal.getLocation().getSignBlock())) { + portal.getLocation().setButtonBlock(buttonLocation); - BlockLocation oldButtonLocation = portal.getStructure().getButton(); + Block oldButtonLocation = portal.getStructure().getButton(); if (oldButtonLocation != null && !oldButtonLocation.equals(buttonLocation)) { Stargate.addControlBlockUpdateRequest(new BlockChangeRequest(oldButtonLocation, Material.AIR, null)); portal.getStructure().setButton(buttonLocation); @@ -376,7 +365,7 @@ public final class PortalFileHelper { */ public static void generatePortalButton(@NotNull Portal portal, @NotNull BlockFace buttonFacing) { //Go one block outwards to find the button's location rather than the control block's location - BlockLocation button = getButtonLocation(portal); + Block button = portal.getLocation().getButtonBlock(); // If the button location is null here, it is assumed that the button generation wasn't necessary if (button == null) { @@ -390,27 +379,24 @@ public final class PortalFileHelper { Directional buttonData = (Directional) Bukkit.createBlockData(buttonType); buttonData.setFacing(buttonFacing); - button.getBlock().setBlockData(buttonData); + button.setBlockData(buttonData); } portal.getStructure().setButton(button); } /** - * Gets the location of a portal's button + * Gets the block specified in the input * - * @param portalThe portal to find the button for
- * @returnThe location of the portal's button
+ * @param worldThe world the block belongs to
+ * @param stringComma-separated coordinate string
+ * @returnThe specified block
+ * @throws NumberFormatExceptionIf non-numeric values are encountered
*/ - @Nullable - public static BlockLocation getButtonLocation(@NotNull Portal portal) { - BlockLocation topLeft = portal.getTopLeft(); - RelativeBlockVector buttonVector = portal.getLocation().getButtonVector(); - - if (buttonVector == null) { - return null; - } - - return topLeft.getRelativeLocation(buttonVector.addOut(1), portal.getYaw()); + @NotNull + private static Block getBlock(@NotNull World world, @NotNull String string) throws NumberFormatException { + String[] parts = string.split(","); + return new Location(world, Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), + Integer.parseInt(parts[2])).getBlock(); } } diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java b/src/main/java/net/knarcraft/stargate/utility/PortalUtil.java similarity index 85% rename from src/main/java/net/knarcraft/stargate/portal/PortalHandler.java rename to src/main/java/net/knarcraft/stargate/utility/PortalUtil.java index e5bebf9..5d35755 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java +++ b/src/main/java/net/knarcraft/stargate/utility/PortalUtil.java @@ -1,24 +1,23 @@ -package net.knarcraft.stargate.portal; +package net.knarcraft.stargate.utility; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.config.Permission; import net.knarcraft.stargate.config.formatting.Message; import net.knarcraft.stargate.config.formatting.SGFormatBuilder; import net.knarcraft.stargate.config.material.BukkitTagSpecifier; -import net.knarcraft.stargate.container.BlockLocation; -import net.knarcraft.stargate.container.RelativeBlockVector; +import net.knarcraft.stargate.portal.Portal; +import net.knarcraft.stargate.portal.PortalRegistry; import net.knarcraft.stargate.portal.property.PortalLocation; import net.knarcraft.stargate.portal.property.PortalOption; import net.knarcraft.stargate.portal.property.PortalStructure; import net.knarcraft.stargate.portal.property.gate.Gate; import net.knarcraft.stargate.portal.property.gate.GateHandler; -import net.knarcraft.stargate.utility.MaterialHelper; -import net.knarcraft.stargate.utility.PermissionHelper; import org.bukkit.Location; import org.bukkit.Tag; -import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.Player; +import org.bukkit.util.BlockVector; +import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -30,9 +29,9 @@ import java.util.Map; /** * Keeps track of all loaded portals, and handles portal creation */ -public class PortalHandler { +public final class PortalUtil { - private PortalHandler() { + private PortalUtil() { } @@ -118,8 +117,7 @@ public class PortalHandler { return true; } //Check if this player can access the destination world - if (destinationPortal.getWorld() != null && PermissionHelper.cannotAccessWorld(player, - destinationPortal.getWorld().getName())) { + if (PermissionHelper.cannotAccessWorld(player, destinationPortal.getLocation().getWorld().getName())) { return false; } //The portal is visible to the player @@ -166,36 +164,33 @@ public class PortalHandler { * Tries to find a gate matching the portal the user is trying to create * * @param portalLocationThe location data for the new portal
- * @param worldThe world the player is located in
* @returnThe matching gate type, or null if no such gate could be found
*/ @Nullable - public static Gate findMatchingGate(@NotNull PortalLocation portalLocation, @NotNull World world) { - Block signParent = portalLocation.getSignLocation().getParent(); + public static Gate findMatchingGate(@NotNull PortalLocation portalLocation) { + Block signParent = DirectionHelper.getParent(portalLocation.getSignBlock()); if (signParent == null) { return null; } - BlockLocation parent = new BlockLocation(world, signParent.getX(), signParent.getY(), - signParent.getZ()); //Get all gates with the used type of control blocks ListIf unable to read or write a portal file
+ */ + public void savePortals() throws IOException { + MapThe portal to save
+ */ + public void savePortal(@NotNull Portal portal, @NotNull FileConfiguration configuration) { + + String root = portal.getNetwork() + "." + portal.getName(); + ConfigurationSection portalSection = configuration.createSection(root); + + portalSection.set("name", portal.getName()); + portalSection.set("network", portal.getNetwork()); + portalSection.set("destination", portal.getOptions().isFixed() ? portal.getDestinationName() : null); + portalSection.set("owner", portal.getOwner().getIdentifier()); + portalSection.set("gate", portal.getGate().getFilename()); + portalSection.set("topLeft", portal.getLocation().getTopLeft()); + portalSection.set("signLocation", portal.getLocation().getSignBlock()); + portalSection.set("buttonLocation", portal.getLocation().getButtonBlock()); + portalSection.set("facing", portal.getLocation().getFacing()); + + for (PortalOption option : PortalOption.values()) { + portalSection.set("options." + option.getCharacterRepresentation(), portal.getOptions().checkOption(option)); + } + } + + @Nullable + public Portal loadPortal(@NotNull ConfigurationSection portalSection) { + + /* PortalLocation location = new PortalLocation(); + BlockLocation signLocation = (BlockLocation) portalSection.get("signLocation"); + BlockLocation topLeft = (BlockLocation) portalSection.get("topLeft"); + if (signLocation == null || topLeft == null) { + return null; + } + BlockFace facing = (BlockFace) Objects.requireNonNull(portalSection.get("facing")); + location.setSignLocation(signLocation).setButtonFacing(facing).setYaw(BlockFace.); + + BlockLocation button; + PortalStrings portalStrings = new PortalStrings(); + Gate gate = GateHandler.getGateByName(); + PortalOwner owner; + Map