diff --git a/src/main/java/org/sgrewritten/stargate/api/BlockHandlerInterface.java b/src/main/java/org/sgrewritten/stargate/api/BlockHandlerInterface.java index fe64b0d9..65aca0ec 100644 --- a/src/main/java/org/sgrewritten/stargate/api/BlockHandlerInterface.java +++ b/src/main/java/org/sgrewritten/stargate/api/BlockHandlerInterface.java @@ -6,7 +6,7 @@ import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.sgrewritten.stargate.api.network.portal.MetaData; +import org.sgrewritten.stargate.api.network.portal.Metadata; import org.sgrewritten.stargate.api.network.portal.Portal; import org.sgrewritten.stargate.api.network.portal.PositionType; @@ -46,7 +46,7 @@ public interface BlockHandlerInterface { * @param portal The affected portal * @return Whether to claim the block */ - boolean registerBlock(Location blockLocation, @Nullable Player player, Portal portal, MetaData metaData); + boolean registerBlock(Location blockLocation, @Nullable Player player, Portal portal, Metadata metaData); /** * Called if a Stargate is removed, and the add-on has registered this location diff --git a/src/main/java/org/sgrewritten/stargate/api/BlockHandlerResolver.java b/src/main/java/org/sgrewritten/stargate/api/BlockHandlerResolver.java index e406b9b8..968e9aec 100644 --- a/src/main/java/org/sgrewritten/stargate/api/BlockHandlerResolver.java +++ b/src/main/java/org/sgrewritten/stargate/api/BlockHandlerResolver.java @@ -8,7 +8,7 @@ import org.sgrewritten.stargate.api.database.StorageAPI; import org.sgrewritten.stargate.api.network.RegistryAPI; import org.sgrewritten.stargate.api.network.portal.BlockLocation; -import org.sgrewritten.stargate.api.network.portal.MetaData; +import org.sgrewritten.stargate.api.network.portal.Metadata; import org.sgrewritten.stargate.api.network.portal.PortalFlag; import org.sgrewritten.stargate.api.network.portal.PortalPosition; import org.sgrewritten.stargate.api.network.portal.RealPortal; @@ -82,13 +82,14 @@ public void registerPlacement(RegistryAPI registry, Location location, List The meta data to set

+ */ + @ApiStatus.Internal + void setMetadata(@Nullable String data); + + /** + * Get metadata for this portal. This gets all metadata for this entity, it's advised to use the {@link MetadataHolder#getMetadata(String)} method + * + * @return

The meta data of this portal

+ */ + @ApiStatus.Internal + @Nullable + String getMetadata(); + + /** + * Set the metadata of this instance + * @param data

The data to set

+ * @param plugin

The name of the plugin this relates to

+ */ + default void setMetadata(@Nullable JsonElement data, String plugin){ + JsonObject metadata = loadMetadata(); + if (data == null) { + metadata.remove(plugin); + } else { + metadata.add(plugin, data); + } + this.setMetadata(metadata.toString()); + } + + /** + * Get the metadata of this instance + * @param pluginName

The name of the plugin this relates to

+ * @return

The metadata of this instance

+ */ + @Nullable + default JsonElement getMetadata(String pluginName){ + JsonObject metaData = loadMetadata(); + return metaData.get(pluginName); + } + + private @NotNull JsonObject loadMetadata(){ + String metadataString = getMetadata(); + JsonObject metadata; + if (metadataString == null) { + metadata = new JsonObject(); + } else { + metadata = JsonParser.parseString(metadataString).getAsJsonObject(); + } + return metadata; + } +} diff --git a/src/main/java/org/sgrewritten/stargate/api/gate/GateAPI.java b/src/main/java/org/sgrewritten/stargate/api/gate/GateAPI.java index 1c3c16cb..5b66ce57 100644 --- a/src/main/java/org/sgrewritten/stargate/api/gate/GateAPI.java +++ b/src/main/java/org/sgrewritten/stargate/api/gate/GateAPI.java @@ -157,4 +157,11 @@ public interface GateAPI { * Modify the world such that the gate follows its gateformat */ void forceGenerateStructure(); + + /** + * Assign a real portal to this gate + * @param realPortal

The portal to assign to this gate

+ */ + @ApiStatus.Internal + void assignPortal(RealPortal realPortal); } diff --git a/src/main/java/org/sgrewritten/stargate/api/network/PortalBuilder.java b/src/main/java/org/sgrewritten/stargate/api/network/PortalBuilder.java index 68930317..a8213e9c 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/PortalBuilder.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/PortalBuilder.java @@ -248,6 +248,7 @@ public RealPortal build() throws TranslatableException, GateConflictException, N flags.add(network.getType().getRelatedFlag()); UUID ownerUUID = network.getType() == NetworkType.PERSONAL ? UUID.fromString(network.getId()) : owner.getUniqueId(); RealPortal portal = PortalCreationHelper.createPortal(network, portalName, destinationName, serverName, flags, unrecognisedFlags, gateAPI, ownerUUID, stargateAPI, metaData); + gateAPI.assignPortal(portal); permissionAndEventHandling(portal, network); flagChecks(flags); diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/MetaData.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/MetaData.java deleted file mode 100644 index ac396f62..00000000 --- a/src/main/java/org/sgrewritten/stargate/api/network/portal/MetaData.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.sgrewritten.stargate.api.network.portal; - -import org.jetbrains.annotations.Nullable; - -/** - * Convenience class to hold one string value that can be changed without losing the reference. - */ -public class MetaData { - private String metaDataString; - - public MetaData(String metaDataString) { - this.metaDataString = metaDataString; - } - - /** - * @return

The stored metadata string

- */ - public @Nullable String getMetaDataString() { - return metaDataString; - } - - /** - * @param metaDataString

The metadata string to be stored

- */ - public void setMetaDataString(@Nullable String metaDataString) { - this.metaDataString = metaDataString; - } -} diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/Metadata.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/Metadata.java new file mode 100644 index 00000000..04a9d938 --- /dev/null +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/Metadata.java @@ -0,0 +1,26 @@ +package org.sgrewritten.stargate.api.network.portal; + +import com.google.gson.JsonElement; +import org.jetbrains.annotations.Nullable; +import org.sgrewritten.stargate.api.MetadataHolder; + +/** + * Convenience class to hold one string value that can be changed without losing the reference. + */ +public class Metadata implements MetadataHolder { + private String metaDataString; + + public Metadata(String metaDataString) { + this.metaDataString = metaDataString; + } + + @Override + public void setMetadata(@Nullable String data) { + this.metaDataString = data; + } + + @Override + public @Nullable String getMetadata() { + return this.metaDataString; + } +} diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/PortalPosition.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/PortalPosition.java index ef3e4075..0aa27023 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/portal/PortalPosition.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/PortalPosition.java @@ -1,9 +1,14 @@ package org.sgrewritten.stargate.api.network.portal; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.bukkit.util.BlockVector; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.sgrewritten.stargate.Stargate; +import org.sgrewritten.stargate.api.MetadataHolder; import org.sgrewritten.stargate.exception.database.StorageReadException; import org.sgrewritten.stargate.exception.database.StorageWriteException; @@ -12,13 +17,14 @@ /** * A position of a portal's control block */ -public class PortalPosition { +public class PortalPosition implements MetadataHolder { private final PositionType positionType; private final BlockVector relativePositionLocation; private final String pluginName; private boolean active; private @Nullable String metaData = null; + private RealPortal portal; /** * Instantiates a new active portal position @@ -66,37 +72,12 @@ public BlockVector getRelativePositionLocation() { return this.relativePositionLocation; } - /** - * @param portal

The portal which this position belongs to

- * @return - */ - @Nullable - public String getMetaData(RealPortal portal) { - if (metaData != null) { - return metaData; - } - try { - metaData = Stargate.getStorageAPIStatic().getPortalPositionMetaData(portal, this, portal.getStorageType()); - return metaData; - } catch (StorageReadException e) { - Stargate.log(e); - return null; - } - } - - /** - * Set the metadata for specified portal position - * - * @param portal

The owner of this portal position

- * @param data

The new metadata

- */ - public void setMetaData(@NotNull RealPortal portal, @NotNull String data) { - try { - this.metaData = Objects.requireNonNull(data); - Stargate.getStorageAPIStatic().setPortalPositionMetaData(portal, this, data, portal.getStorageType()); - } catch (StorageWriteException e) { - Stargate.log(e); + @ApiStatus.Internal + public void assignPortal(RealPortal portal) { + if (this.portal != null) { + throw new IllegalStateException("A portal position can only be assigned to a portal once."); } + this.portal = Objects.requireNonNull(portal); } /** @@ -134,4 +115,28 @@ public boolean isActive() { public void setActive(boolean active) { this.active = active; } + + @Override + public void setMetadata(@Nullable String data) { + try { + this.metaData = Objects.requireNonNull(data); + Stargate.getStorageAPIStatic().setPortalPositionMetaData(portal, this, data, portal.getStorageType()); + } catch (StorageWriteException e) { + Stargate.log(e); + } + } + + @Override + public String getMetadata() { + if (metaData != null) { + return metaData; + } + try { + metaData = Stargate.getStorageAPIStatic().getPortalPositionMetaData(portal, this, portal.getStorageType()); + return metaData; + } catch (StorageReadException e) { + Stargate.log(e); + return null; + } + } } diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/RealPortal.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/RealPortal.java index fae9093e..672646e8 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/portal/RealPortal.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/RealPortal.java @@ -5,6 +5,7 @@ import org.bukkit.block.BlockFace; import org.bukkit.block.Sign; import org.bukkit.event.player.PlayerInteractEvent; +import org.sgrewritten.stargate.api.MetadataHolder; import org.sgrewritten.stargate.api.gate.GateAPI; import org.sgrewritten.stargate.api.network.portal.format.SignLine; @@ -14,7 +15,7 @@ * A real portal with a physical sign that is located on this server */ @SuppressWarnings("unused") -public interface RealPortal extends Portal { +public interface RealPortal extends Portal, MetadataHolder { /** * Gets the line to be drawn for the signs @@ -84,20 +85,6 @@ public interface RealPortal extends Portal { */ List getPortalPosition(PositionType type); - /** - * Set metadata for this portal - * - * @param data

The meta data to set

- */ - void setMetaData(String data); - - /** - * Get metadata for this portal - * - * @return

The meta data of this portal

- */ - String getMetaData(); - /** * Get the facing entities exit from this portal. * diff --git a/src/main/java/org/sgrewritten/stargate/database/SQLDatabase.java b/src/main/java/org/sgrewritten/stargate/database/SQLDatabase.java index ad718ab2..aaf0aebe 100644 --- a/src/main/java/org/sgrewritten/stargate/database/SQLDatabase.java +++ b/src/main/java/org/sgrewritten/stargate/database/SQLDatabase.java @@ -263,6 +263,7 @@ private void registerPortalGate(PortalData portalData, Network network, Stargate } gate.addPortalPositions(portalPositions); RealPortal portal = PortalCreationHelper.createPortal(network, portalData, gate, stargateAPI); + gate.assignPortal(portal); network.addPortal(portal); Stargate.log(Level.FINEST, "Added as normal portal"); } diff --git a/src/main/java/org/sgrewritten/stargate/database/SQLQueryGenerator.java b/src/main/java/org/sgrewritten/stargate/database/SQLQueryGenerator.java index 78747b27..1de1daa1 100644 --- a/src/main/java/org/sgrewritten/stargate/database/SQLQueryGenerator.java +++ b/src/main/java/org/sgrewritten/stargate/database/SQLQueryGenerator.java @@ -419,9 +419,9 @@ public PreparedStatement generateAddPortalStatement(Connection connection, RealP if (isInterServer) { statement.setString(12, Stargate.getServerUUID()); - statement.setString(13, portal.getMetaData()); + statement.setString(13, portal.getMetadata()); } else { - statement.setString(12, portal.getMetaData()); + statement.setString(12, portal.getMetadata()); } Stargate.log(Level.FINEST, "sql query: " + statementMessage); diff --git a/src/main/java/org/sgrewritten/stargate/gate/Gate.java b/src/main/java/org/sgrewritten/stargate/gate/Gate.java index e2cb1709..669b04a0 100644 --- a/src/main/java/org/sgrewritten/stargate/gate/Gate.java +++ b/src/main/java/org/sgrewritten/stargate/gate/Gate.java @@ -31,6 +31,7 @@ import org.sgrewritten.stargate.api.network.portal.BlockLocation; import org.sgrewritten.stargate.api.network.portal.PortalPosition; import org.sgrewritten.stargate.api.network.portal.PositionType; +import org.sgrewritten.stargate.api.network.portal.RealPortal; import org.sgrewritten.stargate.api.network.portal.format.SignLine; import org.sgrewritten.stargate.api.network.portal.format.StargateComponentDeserialiser; import org.sgrewritten.stargate.api.vectorlogic.MatrixVectorOperation; @@ -64,6 +65,7 @@ public class Gate implements GateAPI { private boolean isOpen = false; private boolean flipped; private final @NotNull RegistryAPI registry; + private RealPortal portal; /** @@ -529,4 +531,15 @@ public PortalPosition addPortalPosition(BlockVector relativeBlockVector, Positio public void addPortalPositions(List portalPositions) { this.portalPositions.addAll(portalPositions); } + + @Override + public void assignPortal(@NotNull RealPortal realPortal){ + if(this.portal != null){ + throw new IllegalStateException("A portal position can only be assigned to a portal once."); + } + this.portal = Objects.requireNonNull(realPortal); + for(PortalPosition portalPosition : this.portalPositions){ + portalPosition.assignPortal(realPortal); + } + } } diff --git a/src/main/java/org/sgrewritten/stargate/network/portal/AbstractPortal.java b/src/main/java/org/sgrewritten/stargate/network/portal/AbstractPortal.java index fc407268..6c625a01 100644 --- a/src/main/java/org/sgrewritten/stargate/network/portal/AbstractPortal.java +++ b/src/main/java/org/sgrewritten/stargate/network/portal/AbstractPortal.java @@ -617,7 +617,7 @@ protected void deactivate() { } @Override - public void setMetaData(String data) { + public void setMetadata(String data) { try { this.metaData = data; if (this.savedToStorage) { @@ -629,7 +629,7 @@ public void setMetaData(String data) { } @Override - public String getMetaData() { + public String getMetadata() { if (this.metaData != null || !this.savedToStorage) { return this.metaData; } diff --git a/src/main/java/org/sgrewritten/stargate/util/portal/PortalCreationHelper.java b/src/main/java/org/sgrewritten/stargate/util/portal/PortalCreationHelper.java index af3b3245..313ff233 100644 --- a/src/main/java/org/sgrewritten/stargate/util/portal/PortalCreationHelper.java +++ b/src/main/java/org/sgrewritten/stargate/util/portal/PortalCreationHelper.java @@ -47,6 +47,7 @@ public static RealPortal createPortal(Network network, String name, String desti StargateAPI stargateAPI, String metaData) throws TranslatableException { name = NameHelper.getTrimmedName(name); + RealPortal portal; if (flags.contains(PortalFlag.BUNGEE)) { flags.add(PortalFlag.FIXED); Network bungeeNetwork = stargateAPI.getNetworkManager().selectNetwork(BungeePortal.getLegacyNetworkName(), NetworkType.CUSTOM, StorageType.LOCAL); diff --git a/src/test/java/org/sgrewritten/stargate/api/BlockHandlerInterfaceMock.java b/src/test/java/org/sgrewritten/stargate/api/BlockHandlerInterfaceMock.java index 4ef4a0a7..e3863569 100644 --- a/src/test/java/org/sgrewritten/stargate/api/BlockHandlerInterfaceMock.java +++ b/src/test/java/org/sgrewritten/stargate/api/BlockHandlerInterfaceMock.java @@ -7,7 +7,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.sgrewritten.stargate.api.network.portal.BlockLocation; -import org.sgrewritten.stargate.api.network.portal.MetaData; +import org.sgrewritten.stargate.api.network.portal.Metadata; import org.sgrewritten.stargate.api.network.portal.Portal; import org.sgrewritten.stargate.api.network.portal.PositionType; import org.sgrewritten.stargate.container.TwoTuple; @@ -60,7 +60,7 @@ public BlockHandlerInterfaceMock(PositionType interfaceType, Material handledMat } @Override - public boolean registerBlock(Location blockLocation, @Nullable Player player, Portal portal, MetaData data) { + public boolean registerBlock(Location blockLocation, @Nullable Player player, Portal portal, Metadata data) { if (isRegisterPlacedBlock) { registeredBlocks.put(new BlockLocation(blockLocation), new TwoTuple<>(player, portal)); } diff --git a/src/test/java/org/sgrewritten/stargate/gate/GateMock.java b/src/test/java/org/sgrewritten/stargate/gate/GateMock.java index 820819e3..b2a13d1f 100644 --- a/src/test/java/org/sgrewritten/stargate/gate/GateMock.java +++ b/src/test/java/org/sgrewritten/stargate/gate/GateMock.java @@ -12,6 +12,7 @@ import org.sgrewritten.stargate.api.network.portal.BlockLocation; import org.sgrewritten.stargate.api.network.portal.PortalPosition; import org.sgrewritten.stargate.api.network.portal.PositionType; +import org.sgrewritten.stargate.api.network.portal.RealPortal; import org.sgrewritten.stargate.api.network.portal.format.SignLine; import java.util.List; @@ -111,4 +112,9 @@ public void removePortalPosition(PortalPosition portalPosition) { public void forceGenerateStructure() { } + + @Override + public void assignPortal(RealPortal realPortal) { + + } } diff --git a/src/test/java/org/sgrewritten/stargate/util/portal/PortalMock.java b/src/test/java/org/sgrewritten/stargate/util/portal/PortalMock.java index 9da05adf..4388de46 100644 --- a/src/test/java/org/sgrewritten/stargate/util/portal/PortalMock.java +++ b/src/test/java/org/sgrewritten/stargate/util/portal/PortalMock.java @@ -197,12 +197,12 @@ public List getPortalPosition(PositionType type) { } @Override - public void setMetaData(String data) { + public void setMetadata(String data) { } @Override - public String getMetaData() { + public String getMetadata() { return null; }