Skip to content

Commit

Permalink
Metadata refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorinwasher committed Jan 8, 2024
1 parent 5fe4d3a commit 0d844c5
Show file tree
Hide file tree
Showing 17 changed files with 173 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,13 +82,14 @@ public void registerPlacement(RegistryAPI registry, Location location, List<Real
}
for (RealPortal portal : portals) {
for (BlockHandlerInterface blockHandlerInterface : blockHandlerMap.get(material)) {
MetaData metaData = new MetaData("");
Metadata metaData = new Metadata("");
if (portal.hasFlag(blockHandlerInterface.getFlag()) && blockHandlerInterface.registerBlock(location, player, portal, metaData)) {
PortalPosition portalPosition = registry.savePortalPosition(portal, location, blockHandlerInterface.getInterfaceType(), blockHandlerInterface.getPlugin());
portalPosition.assignPortal(portal);
registry.registerPortalPosition(portalPosition, location, portal);
blockBlockHandlerMap.put(new BlockLocation(location), blockHandlerInterface);
if (!metaData.getMetaDataString().isEmpty()) {
portalPosition.setMetaData(portal, metaData.getMetaDataString());
if (metaData.getMetadata() != null && !metaData.getMetadata().isEmpty()) {
portalPosition.setMetadata(metaData.getMetadata());
}
return;
}
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/org/sgrewritten/stargate/api/MetadataHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.sgrewritten.stargate.api;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface MetadataHolder {

/**
* Set metadata for this portal. This sets all metadata for this entity, it's advised to use {@link MetadataHolder#setMetadata(JsonElement, String)}
*
* @param data <p> The meta data to set </p>
*/
@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 <p> The meta data of this portal </p>
*/
@ApiStatus.Internal
@Nullable
String getMetadata();

/**
* Set the metadata of this instance
* @param data <p>The data to set</p>
* @param plugin <p>The name of the plugin this relates to</p>
*/
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 <p>The name of the plugin this relates to</p>
* @return <p>The metadata of this instance</p>
*/
@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;
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/sgrewritten/stargate/api/gate/GateAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <p>The portal to assign to this gate</p>
*/
@ApiStatus.Internal
void assignPortal(RealPortal realPortal);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -66,37 +72,12 @@ public BlockVector getRelativePositionLocation() {
return this.relativePositionLocation;
}

/**
* @param portal <p> The portal which this position belongs to </p>
* @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 <p>The owner of this portal position</p>
* @param data <p>The new metadata</p>
*/
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);
}

/**
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -84,20 +85,6 @@ public interface RealPortal extends Portal {
*/
List<Location> getPortalPosition(PositionType type);

/**
* Set metadata for this portal
*
* @param data <p> The meta data to set </p>
*/
void setMetaData(String data);

/**
* Get metadata for this portal
*
* @return <p> The meta data of this portal </p>
*/
String getMetaData();

/**
* Get the facing entities exit from this portal.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/sgrewritten/stargate/gate/Gate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,6 +65,7 @@ public class Gate implements GateAPI {
private boolean isOpen = false;
private boolean flipped;
private final @NotNull RegistryAPI registry;
private RealPortal portal;


/**
Expand Down Expand Up @@ -529,4 +531,15 @@ public PortalPosition addPortalPosition(BlockVector relativeBlockVector, Positio
public void addPortalPositions(List<PortalPosition> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
Loading

0 comments on commit 0d844c5

Please sign in to comment.