Makes stargates' buttons update when portals are loaded #14
This commit is contained in:
parent
f3292cff99
commit
80ff241d4b
@ -4,20 +4,18 @@ import net.knarcraft.stargate.Stargate;
|
|||||||
import net.knarcraft.stargate.container.BlockLocation;
|
import net.knarcraft.stargate.container.BlockLocation;
|
||||||
import net.knarcraft.stargate.container.RelativeBlockVector;
|
import net.knarcraft.stargate.container.RelativeBlockVector;
|
||||||
import net.knarcraft.stargate.event.StargateCreateEvent;
|
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.PortalLocation;
|
||||||
import net.knarcraft.stargate.portal.property.PortalOption;
|
import net.knarcraft.stargate.portal.property.PortalOption;
|
||||||
import net.knarcraft.stargate.portal.property.PortalOptions;
|
import net.knarcraft.stargate.portal.property.PortalOptions;
|
||||||
import net.knarcraft.stargate.portal.property.PortalOwner;
|
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.DirectionHelper;
|
||||||
import net.knarcraft.stargate.utility.EconomyHelper;
|
import net.knarcraft.stargate.utility.EconomyHelper;
|
||||||
import net.knarcraft.stargate.utility.PermissionHelper;
|
import net.knarcraft.stargate.utility.PermissionHelper;
|
||||||
import net.knarcraft.stargate.utility.PortalFileHelper;
|
import net.knarcraft.stargate.utility.PortalFileHelper;
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.BlockFace;
|
import org.bukkit.block.BlockFace;
|
||||||
import org.bukkit.block.data.Directional;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.block.SignChangeEvent;
|
import org.bukkit.event.block.SignChangeEvent;
|
||||||
|
|
||||||
@ -51,16 +49,16 @@ public class PortalCreator {
|
|||||||
*/
|
*/
|
||||||
public Portal createPortal() {
|
public Portal createPortal() {
|
||||||
BlockLocation signLocation = new BlockLocation(event.getBlock());
|
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
|
//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");
|
Stargate.debug("createPortal", "Control block not registered");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//The control block is already part of another portal
|
//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");
|
Stargate.debug("createPortal", "idParent belongs to existing stargate");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -75,7 +73,8 @@ public class PortalCreator {
|
|||||||
Map<PortalOption, Boolean> portalOptions = PortalHandler.getPortalOptions(player, destinationName, options);
|
Map<PortalOption, Boolean> portalOptions = PortalHandler.getPortalOptions(player, destinationName, options);
|
||||||
|
|
||||||
//Get the yaw
|
//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
|
//Get the direction the button should be facing
|
||||||
BlockFace buttonFacing = DirectionHelper.getBlockFaceFromYaw(yaw);
|
BlockFace buttonFacing = DirectionHelper.getBlockFaceFromYaw(yaw);
|
||||||
@ -86,7 +85,7 @@ public class PortalCreator {
|
|||||||
Stargate.debug("createPortal", "Finished getting all portal info");
|
Stargate.debug("createPortal", "Finished getting all portal info");
|
||||||
|
|
||||||
//Try and find a gate matching the new portal
|
//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)) {
|
if ((gate == null) || (portalLocation.getButtonVector() == null)) {
|
||||||
Stargate.debug("createPortal", "Could not find matching gate layout");
|
Stargate.debug("createPortal", "Could not find matching gate layout");
|
||||||
return null;
|
return null;
|
||||||
@ -202,9 +201,8 @@ public class PortalCreator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Add button if the portal is not always on
|
//Add button if the portal is not always on
|
||||||
if (!portalOptions.isAlwaysOn() && !portalOptions.isBungee()) {
|
if (!portalOptions.isAlwaysOn()) {
|
||||||
generatePortalButton(portalLocation.getTopLeft(), portalLocation.getButtonVector(),
|
PortalFileHelper.generatePortalButton(portal, portalLocation.getButtonFacing());
|
||||||
portalLocation.getButtonFacing());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Register the new portal
|
//Register the new portal
|
||||||
@ -273,25 +271,6 @@ public class PortalCreator {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Generates a button for a portal
|
|
||||||
*
|
|
||||||
* @param topLeft <p>The top-left block of the portal</p>
|
|
||||||
* @param buttonVector <p>A relative vector pointing at the button</p>
|
|
||||||
* @param buttonFacing <p>The direction the button should be facing</p>
|
|
||||||
*/
|
|
||||||
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
|
* Updates the open state of the newly created portal
|
||||||
*
|
*
|
||||||
|
@ -1,16 +1,23 @@
|
|||||||
package net.knarcraft.stargate.utility;
|
package net.knarcraft.stargate.utility;
|
||||||
|
|
||||||
import net.knarcraft.stargate.Stargate;
|
import net.knarcraft.stargate.Stargate;
|
||||||
|
import net.knarcraft.stargate.container.BlockChangeRequest;
|
||||||
import net.knarcraft.stargate.container.BlockLocation;
|
import net.knarcraft.stargate.container.BlockLocation;
|
||||||
|
import net.knarcraft.stargate.container.RelativeBlockVector;
|
||||||
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.portal.PortalRegistry;
|
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.PortalLocation;
|
||||||
import net.knarcraft.stargate.portal.property.PortalOptions;
|
import net.knarcraft.stargate.portal.property.PortalOptions;
|
||||||
import net.knarcraft.stargate.portal.property.PortalOwner;
|
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.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.BufferedWriter;
|
||||||
import java.io.File;
|
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
|
//Register the portal, and close it in case it wasn't properly closed when the server stopped
|
||||||
PortalHandler.registerPortal(portal);
|
PortalHandler.registerPortal(portal);
|
||||||
portal.getPortalOpener().closePortal(true);
|
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 <p>The portal update the button of</p>
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
*
|
||||||
|
* <p>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.</p>
|
||||||
|
*
|
||||||
|
* @param portal <p>The portal to set button vector for</p>
|
||||||
|
*/
|
||||||
|
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 <p>The portal to generate button for</p>
|
||||||
|
* @param buttonFacing <p>The direction the button should be facing</p>
|
||||||
|
*/
|
||||||
|
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 <p>The portal to find the button for</p>
|
||||||
|
* @return <p>The location of the portal's button</p>
|
||||||
|
*/
|
||||||
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user