Adds broken code for yaml storage
Some checks failed
EpicKnarvik97/Stargate/pipeline/head There was a failure building this commit

This commit is contained in:
2025-12-17 14:28:14 +01:00
parent c5a964337a
commit 0edb800cd3
35 changed files with 652 additions and 500 deletions

View File

@@ -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 location <p>The location of the button/sign to replace</p>
* @param portal <p>The portal the button/sign belongs to</p>
* @param block <p>The location of the button/sign to replace</p>
* @param portal <p>The portal the button/sign belongs to</p>
* @return <p>The material to use for removing the button/sign</p>
*/
@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 {
* @return <p>True if the calculated button location is not the same as the one in the portal file</p>
*/
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 portal <p>The portal to find the button for</p>
* @return <p>The location of the portal's button</p>
* @param world <p>The world the block belongs to</p>
* @param string <p>Comma-separated coordinate string</p>
* @return <p>The specified block</p>
* @throws NumberFormatException <p>If non-numeric values are encountered</p>
*/
@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();
}
}