From 80ff241d4b08098a8a4bb55455040afbd3f5ba67 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 5 Nov 2021 21:38:33 +0100 Subject: [PATCH] Makes stargates' buttons update when portals are loaded #14 --- .../stargate/portal/PortalCreator.java | 41 +++------ .../stargate/utility/PortalFileHelper.java | 83 ++++++++++++++++++- 2 files changed, 91 insertions(+), 33 deletions(-) diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java b/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java index 79e8b0b..bf41a76 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java @@ -4,20 +4,18 @@ import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.container.BlockLocation; import net.knarcraft.stargate.container.RelativeBlockVector; import net.knarcraft.stargate.event.StargateCreateEvent; -import net.knarcraft.stargate.portal.property.gate.Gate; -import net.knarcraft.stargate.portal.property.gate.GateHandler; import net.knarcraft.stargate.portal.property.PortalLocation; import net.knarcraft.stargate.portal.property.PortalOption; import net.knarcraft.stargate.portal.property.PortalOptions; import net.knarcraft.stargate.portal.property.PortalOwner; +import net.knarcraft.stargate.portal.property.gate.Gate; +import net.knarcraft.stargate.portal.property.gate.GateHandler; import net.knarcraft.stargate.utility.DirectionHelper; import net.knarcraft.stargate.utility.EconomyHelper; import net.knarcraft.stargate.utility.PermissionHelper; import net.knarcraft.stargate.utility.PortalFileHelper; -import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; -import org.bukkit.block.data.Directional; import org.bukkit.entity.Player; import org.bukkit.event.block.SignChangeEvent; @@ -51,16 +49,16 @@ public class PortalCreator { */ public Portal createPortal() { BlockLocation signLocation = new BlockLocation(event.getBlock()); - Block idParent = signLocation.getParent(); + Block signControlBlock = signLocation.getParent(); //Return early if the sign is not placed on a block, or the block is not a control block - if (idParent == null || GateHandler.getGatesByControlBlock(idParent).length == 0) { + if (signControlBlock == null || GateHandler.getGatesByControlBlock(signControlBlock).length == 0) { Stargate.debug("createPortal", "Control block not registered"); return null; } //The control block is already part of another portal - if (PortalHandler.getByBlock(idParent) != null) { + if (PortalHandler.getByBlock(signControlBlock) != null) { Stargate.debug("createPortal", "idParent belongs to existing stargate"); return null; } @@ -75,7 +73,8 @@ public class PortalCreator { Map portalOptions = PortalHandler.getPortalOptions(player, destinationName, options); //Get the yaw - float yaw = DirectionHelper.getYawFromLocationDifference(idParent.getLocation(), signLocation.getLocation()); + float yaw = DirectionHelper.getYawFromLocationDifference(signControlBlock.getLocation(), + signLocation.getLocation()); //Get the direction the button should be facing BlockFace buttonFacing = DirectionHelper.getBlockFaceFromYaw(yaw); @@ -86,7 +85,7 @@ public class PortalCreator { Stargate.debug("createPortal", "Finished getting all portal info"); //Try and find a gate matching the new portal - Gate gate = PortalHandler.findMatchingGate(portalLocation, player); + Gate gate = PortalHandler.findMatchingGate(portalLocation, player.getWorld()); if ((gate == null) || (portalLocation.getButtonVector() == null)) { Stargate.debug("createPortal", "Could not find matching gate layout"); return null; @@ -202,9 +201,8 @@ public class PortalCreator { } //Add button if the portal is not always on - if (!portalOptions.isAlwaysOn() && !portalOptions.isBungee()) { - generatePortalButton(portalLocation.getTopLeft(), portalLocation.getButtonVector(), - portalLocation.getButtonFacing()); + if (!portalOptions.isAlwaysOn()) { + PortalFileHelper.generatePortalButton(portal, portalLocation.getButtonFacing()); } //Register the new portal @@ -273,25 +271,6 @@ public class PortalCreator { return true; } - /** - * Generates a button for a portal - * - * @param topLeft

The top-left block of the portal

- * @param buttonVector

A relative vector pointing at the button

- * @param buttonFacing

The direction the button should be facing

- */ - private void generatePortalButton(BlockLocation topLeft, RelativeBlockVector buttonVector, - BlockFace buttonFacing) { - //Go one block outwards to find the button's location rather than the control block's location - BlockLocation button = topLeft.getRelativeLocation(buttonVector.addToVector( - RelativeBlockVector.Property.OUT, 1), portal.getYaw()); - - Directional buttonData = (Directional) Bukkit.createBlockData(portal.getGate().getPortalButton()); - buttonData.setFacing(buttonFacing); - button.getBlock().setBlockData(buttonData); - portal.getStructure().setButton(button); - } - /** * Updates the open state of the newly created portal * diff --git a/src/main/java/net/knarcraft/stargate/utility/PortalFileHelper.java b/src/main/java/net/knarcraft/stargate/utility/PortalFileHelper.java index cbf4a16..6e99c94 100644 --- a/src/main/java/net/knarcraft/stargate/utility/PortalFileHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/PortalFileHelper.java @@ -1,16 +1,23 @@ 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.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.gate.Gate; -import net.knarcraft.stargate.portal.property.gate.GateHandler; import net.knarcraft.stargate.portal.property.PortalLocation; import net.knarcraft.stargate.portal.property.PortalOptions; import net.knarcraft.stargate.portal.property.PortalOwner; +import net.knarcraft.stargate.portal.property.gate.Gate; +import net.knarcraft.stargate.portal.property.gate.GateHandler; +import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.block.BlockFace; +import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.Directional; import java.io.BufferedWriter; import java.io.File; @@ -255,6 +262,78 @@ public final class PortalFileHelper { //Register the portal, and close it in case it wasn't properly closed when the server stopped PortalHandler.registerPortal(portal); portal.getPortalOpener().closePortal(true); + + //Update the portal's button if it's the wrong material + updatePortalButton(portal); + } + + /** + * Updates a portal's button if it does not match the correct material + * + * @param portal

The portal update the button of

+ */ + private static void updatePortalButton(Portal portal) { + setButtonVector(portal); + BlockLocation buttonLocation = getButtonLocation(portal); + BlockData buttonData = buttonLocation.getBlock().getBlockData(); + if (portal.getOptions().isAlwaysOn()) { + //Clear button if not already air + if (buttonData.getMaterial() != Material.AIR) { + Stargate.addBlockChangeRequest(new BlockChangeRequest(buttonLocation, Material.AIR, null)); + } + } else { + //Replace button if the material does not match + if (buttonData.getMaterial() != portal.getGate().getPortalButton()) { + generatePortalButton(portal, DirectionHelper.getBlockFaceFromYaw(portal.getYaw())); + } + } + } + + /** + * Sets the button vector for the given portal + * + *

As the button vector isn't saved, it is null when the portal is loaded. This method allows it to be + * explicitly set when necessary.

+ * + * @param portal

The portal to set button vector for

+ */ + private static void setButtonVector(Portal portal) { + for (RelativeBlockVector control : portal.getGate().getLayout().getControls()) { + BlockLocation controlLocation = portal.getLocation().getTopLeft().getRelativeLocation(control, + portal.getYaw()); + if (controlLocation != portal.getLocation().getSignLocation()) { + portal.getLocation().setButtonVector(control); + } + } + } + + /** + * Generates a button for a portal + * + * @param portal

The portal to generate button for

+ * @param buttonFacing

The direction the button should be facing

+ */ + public static void generatePortalButton(Portal portal, BlockFace buttonFacing) { + //Go one block outwards to find the button's location rather than the control block's location + BlockLocation button = getButtonLocation(portal); + + Directional buttonData = (Directional) Bukkit.createBlockData(portal.getGate().getPortalButton()); + buttonData.setFacing(buttonFacing); + button.getBlock().setBlockData(buttonData); + portal.getStructure().setButton(button); + } + + /** + * Gets the location of a portal's button + * + * @param portal

The portal to find the button for

+ * @return

The location of the portal's button

+ */ + private static BlockLocation getButtonLocation(Portal portal) { + BlockLocation topLeft = portal.getTopLeft(); + RelativeBlockVector buttonVector = portal.getLocation().getButtonVector(); + return topLeft.getRelativeLocation(buttonVector.addToVector(RelativeBlockVector.Property.OUT, 1), + portal.getYaw()); } }