From 23fec8bb84ccd0994e9533db0f5ac1200ee6aa0e Mon Sep 17 00:00:00 2001 From: Thorinwasher Date: Thu, 29 Feb 2024 20:35:24 +0100 Subject: [PATCH] Introduce LineData instead of SignLine before formatting --- .../api/event/StargateMessageEvent.java | 2 +- .../portal/StargateSignFormatPortalEvent.java | 2 +- .../AsyncStargateSendMessagePortalEvent.java | 2 +- .../StargateSendMessagePortalEvent.java | 2 +- .../SyncStargateSendMessagePortalEvent.java | 2 +- .../stargate/api/gate/GateAPI.java | 14 ++- .../stargate/api/network/PortalBuilder.java | 4 +- .../api/network/portal/PortalPosition.java | 15 +++ .../portal/PortalPositionAttachment.java | 11 ++ .../api/network/portal/RealPortal.java | 8 +- .../AbstractSignLine.java | 4 +- .../portal/formatting/LineFormatter.java | 29 +++++ .../{format => formatting}/NetworkLine.java | 2 +- .../{format => formatting}/PortalLine.java | 2 +- .../{format => formatting}/SignLine.java | 2 +- .../{format => formatting}/SignLineType.java | 2 +- .../StargateComponent.java | 4 +- .../StargateComponentDeserialiser.java | 2 +- .../{format => formatting}/TextLine.java | 2 +- .../portal/formatting/data/LineData.java | 23 ++++ .../formatting/data/NetworkLineData.java | 30 +++++ .../formatting/data/PortalLineData.java | 40 ++++++ .../portal/formatting/data/TextLineData.java | 43 +++++++ .../org/sgrewritten/stargate/gate/Gate.java | 106 ++++++++-------- .../listener/PlayerEventListener.java | 2 +- .../network/portal/AbstractPortal.java | 114 ++++++++---------- .../stargate/network/portal/BungeePortal.java | 27 ++--- .../stargate/network/portal/FixedPortal.java | 32 +++-- .../network/portal/NetworkedPortal.java | 34 +++--- .../stargate/network/portal/RandomPortal.java | 30 ++--- .../formatting/LegacyLineColorFormatter.java | 52 +++++--- .../portal/formatting/LineColorFormatter.java | 61 ++++++---- .../portal/formatting/LineFormatter.java | 58 --------- .../formatting/NoLineColorFormatter.java | 63 +++++----- .../stargate/util/MessageUtils.java | 2 +- .../StargateComponentTest.java | 4 +- .../sgrewritten/stargate/gate/GateMock.java | 10 +- .../stargate/util/portal/PortalMock.java | 8 +- 38 files changed, 502 insertions(+), 348 deletions(-) create mode 100644 src/main/java/org/sgrewritten/stargate/api/network/portal/PortalPositionAttachment.java rename src/main/java/org/sgrewritten/stargate/api/network/portal/{format => formatting}/AbstractSignLine.java (79%) create mode 100644 src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/LineFormatter.java rename src/main/java/org/sgrewritten/stargate/api/network/portal/{format => formatting}/NetworkLine.java (94%) rename src/main/java/org/sgrewritten/stargate/api/network/portal/{format => formatting}/PortalLine.java (96%) rename src/main/java/org/sgrewritten/stargate/api/network/portal/{format => formatting}/SignLine.java (81%) rename src/main/java/org/sgrewritten/stargate/api/network/portal/{format => formatting}/SignLineType.java (87%) rename src/main/java/org/sgrewritten/stargate/api/network/portal/{format => formatting}/StargateComponent.java (91%) rename src/main/java/org/sgrewritten/stargate/api/network/portal/{format => formatting}/StargateComponentDeserialiser.java (95%) rename src/main/java/org/sgrewritten/stargate/api/network/portal/{format => formatting}/TextLine.java (96%) create mode 100644 src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/LineData.java create mode 100644 src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/NetworkLineData.java create mode 100644 src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/PortalLineData.java create mode 100644 src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/TextLineData.java delete mode 100644 src/main/java/org/sgrewritten/stargate/network/portal/formatting/LineFormatter.java rename src/test/java/org/sgrewritten/stargate/api/network/portal/{format => formatting}/StargateComponentTest.java (95%) diff --git a/src/main/java/org/sgrewritten/stargate/api/event/StargateMessageEvent.java b/src/main/java/org/sgrewritten/stargate/api/event/StargateMessageEvent.java index 194b971f..de06fe1c 100644 --- a/src/main/java/org/sgrewritten/stargate/api/event/StargateMessageEvent.java +++ b/src/main/java/org/sgrewritten/stargate/api/event/StargateMessageEvent.java @@ -4,7 +4,7 @@ import org.bukkit.event.Event; import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; -import org.sgrewritten.stargate.api.network.portal.format.StargateComponent; +import org.sgrewritten.stargate.api.network.portal.formatting.StargateComponent; public class StargateMessageEvent extends Event implements Cancellable { diff --git a/src/main/java/org/sgrewritten/stargate/api/event/portal/StargateSignFormatPortalEvent.java b/src/main/java/org/sgrewritten/stargate/api/event/portal/StargateSignFormatPortalEvent.java index 03edd7ed..80aaed01 100644 --- a/src/main/java/org/sgrewritten/stargate/api/event/portal/StargateSignFormatPortalEvent.java +++ b/src/main/java/org/sgrewritten/stargate/api/event/portal/StargateSignFormatPortalEvent.java @@ -7,7 +7,7 @@ import org.jetbrains.annotations.NotNull; import org.sgrewritten.stargate.api.network.portal.PortalPosition; import org.sgrewritten.stargate.api.network.portal.RealPortal; -import org.sgrewritten.stargate.api.network.portal.format.SignLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLine; public class StargateSignFormatPortalEvent extends StargatePortalEvent { private static final HandlerList handlers = new HandlerList(); diff --git a/src/main/java/org/sgrewritten/stargate/api/event/portal/message/AsyncStargateSendMessagePortalEvent.java b/src/main/java/org/sgrewritten/stargate/api/event/portal/message/AsyncStargateSendMessagePortalEvent.java index 40e021ff..3e53aba1 100644 --- a/src/main/java/org/sgrewritten/stargate/api/event/portal/message/AsyncStargateSendMessagePortalEvent.java +++ b/src/main/java/org/sgrewritten/stargate/api/event/portal/message/AsyncStargateSendMessagePortalEvent.java @@ -4,7 +4,7 @@ import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; import org.sgrewritten.stargate.api.network.portal.Portal; -import org.sgrewritten.stargate.api.network.portal.format.StargateComponent; +import org.sgrewritten.stargate.api.network.portal.formatting.StargateComponent; public class AsyncStargateSendMessagePortalEvent extends StargateSendMessagePortalEvent { private static final HandlerList handlers = new HandlerList(); diff --git a/src/main/java/org/sgrewritten/stargate/api/event/portal/message/StargateSendMessagePortalEvent.java b/src/main/java/org/sgrewritten/stargate/api/event/portal/message/StargateSendMessagePortalEvent.java index 6d458b66..30e712a6 100644 --- a/src/main/java/org/sgrewritten/stargate/api/event/portal/message/StargateSendMessagePortalEvent.java +++ b/src/main/java/org/sgrewritten/stargate/api/event/portal/message/StargateSendMessagePortalEvent.java @@ -3,7 +3,7 @@ import org.bukkit.entity.Entity; import org.sgrewritten.stargate.api.event.portal.CancellableStargatePortalEvent; import org.sgrewritten.stargate.api.network.portal.Portal; -import org.sgrewritten.stargate.api.network.portal.format.StargateComponent; +import org.sgrewritten.stargate.api.network.portal.formatting.StargateComponent; public abstract class StargateSendMessagePortalEvent extends CancellableStargatePortalEvent { private final MessageType type; diff --git a/src/main/java/org/sgrewritten/stargate/api/event/portal/message/SyncStargateSendMessagePortalEvent.java b/src/main/java/org/sgrewritten/stargate/api/event/portal/message/SyncStargateSendMessagePortalEvent.java index 3d4d797d..addcac7a 100644 --- a/src/main/java/org/sgrewritten/stargate/api/event/portal/message/SyncStargateSendMessagePortalEvent.java +++ b/src/main/java/org/sgrewritten/stargate/api/event/portal/message/SyncStargateSendMessagePortalEvent.java @@ -4,7 +4,7 @@ import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; import org.sgrewritten.stargate.api.network.portal.Portal; -import org.sgrewritten.stargate.api.network.portal.format.StargateComponent; +import org.sgrewritten.stargate.api.network.portal.formatting.StargateComponent; public class SyncStargateSendMessagePortalEvent extends StargateSendMessagePortalEvent { private static final HandlerList handlers = new HandlerList(); 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 b97d6f2c..568822b6 100644 --- a/src/main/java/org/sgrewritten/stargate/api/gate/GateAPI.java +++ b/src/main/java/org/sgrewritten/stargate/api/gate/GateAPI.java @@ -10,7 +10,8 @@ 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.formatting.data.LineData; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLine; import org.sgrewritten.stargate.exception.GateConflictException; import org.sgrewritten.stargate.exception.InvalidStructureException; import org.sgrewritten.stargate.network.StorageType; @@ -25,11 +26,14 @@ public interface GateAPI { /** * Set button and draw sign - * - * @param signLines

an array with 4 elements, representing each line of a sign

- * @param drawButton

whether or not include a button.

*/ - void drawControlMechanisms(SignLine[] signLines, boolean drawButton); + void drawControlMechanisms(LineData[] lines); + + /** + * Update the state on the portal position according to its type + * @param portalPosition

The portal position to update

+ */ + void redrawPosition(PortalPosition portalPosition, @Nullable LineData[] lines); /** * Gets a copy of this gate's portal positions 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 573f996c..2c4b8907 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/PortalBuilder.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/PortalBuilder.java @@ -22,6 +22,7 @@ import org.sgrewritten.stargate.api.gate.GateStructureType; import org.sgrewritten.stargate.api.network.portal.BlockLocation; import org.sgrewritten.stargate.api.network.portal.PortalFlag; +import org.sgrewritten.stargate.api.network.portal.PositionType; import org.sgrewritten.stargate.api.network.portal.RealPortal; import org.sgrewritten.stargate.api.permission.BypassPermission; import org.sgrewritten.stargate.api.permission.PermissionManager; @@ -301,7 +302,8 @@ public RealPortal build() throws TranslatableException, GateConflictException, N getLocationsAdjacentToPortal(gateAPI).forEach(position -> stargateAPI.getMaterialHandlerResolver().registerPlacement(stargateAPI.getRegistry(), position, List.of(portal), position.getBlock().getType(), eventTarget)); //Save the portal and inform the user stargateAPI.getNetworkManager().savePortal(portal, network); - portal.setSignColor(ColorRegistry.DEFAULT_DYE_COLOR); + gateAPI.getPortalPositions().stream().filter(portalPosition -> portalPosition.getPositionType() == PositionType.SIGN) + .forEach(portalPosition -> portal.setSignColor(ColorRegistry.DEFAULT_DYE_COLOR, portalPosition)); Stargate.log(Level.FINE, "Successfully created a new portal"); String msg; if (flags.contains(PortalFlag.PERSONAL_NETWORK)) { 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 dbfb456f..4ddc0f80 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 @@ -22,6 +22,7 @@ public class PortalPosition implements MetadataHolder { private boolean active; private @Nullable String metaData = null; private RealPortal portal; + private PortalPositionAttachment attachment; /** * Instantiates a new active portal position @@ -142,4 +143,18 @@ public String getMetadata() { public RealPortal getPortal() { return this.portal; } + + @ApiStatus.Internal + public @Nullable PortalPositionAttachment getAttachment(){ + return this.attachment; + } + + @ApiStatus.Internal + public void setAttachment(@NotNull PortalPositionAttachment attachment){ + Objects.requireNonNull(attachment); + if(this.attachment != null && this.attachment.getType() != attachment.getType()){ + throw new IllegalArgumentException("Can't change attachment type"); + } + this.attachment = attachment; + } } diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/PortalPositionAttachment.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/PortalPositionAttachment.java new file mode 100644 index 00000000..a9ccc687 --- /dev/null +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/PortalPositionAttachment.java @@ -0,0 +1,11 @@ +package org.sgrewritten.stargate.api.network.portal; + +public interface PortalPositionAttachment { + + + Type getType(); + + enum Type { + LINE_FORMATTER + } +} 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 672646e8..728cce9a 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 @@ -7,7 +7,8 @@ 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; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLine; +import org.sgrewritten.stargate.api.network.portal.formatting.data.LineData; import java.util.List; @@ -19,10 +20,9 @@ public interface RealPortal extends Portal, MetadataHolder { /** * Gets the line to be drawn for the signs - * * @return

Lines to be drawn for the gate owned by this portal

*/ - SignLine[] getDrawnControlLines(); + LineData[] getDrawnControlLines(); /** * Updates the color of this portal's sign @@ -32,7 +32,7 @@ public interface RealPortal extends Portal, MetadataHolder { * * @param color

Color to change the sign text to. If null, then the default color will be used

*/ - void setSignColor(DyeColor color); + void setSignColor(DyeColor color, PortalPosition portalPosition); /** * The action to be run when this portal's button is clicked diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/AbstractSignLine.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/AbstractSignLine.java similarity index 79% rename from src/main/java/org/sgrewritten/stargate/api/network/portal/format/AbstractSignLine.java rename to src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/AbstractSignLine.java index 3d2bc923..eb5e4d6c 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/AbstractSignLine.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/AbstractSignLine.java @@ -1,6 +1,4 @@ -package org.sgrewritten.stargate.api.network.portal.format; - -import org.sgrewritten.stargate.property.NonLegacyClass; +package org.sgrewritten.stargate.api.network.portal.formatting; import java.util.List; diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/LineFormatter.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/LineFormatter.java new file mode 100644 index 00000000..a4af836f --- /dev/null +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/LineFormatter.java @@ -0,0 +1,29 @@ +package org.sgrewritten.stargate.api.network.portal.formatting; + +import org.sgrewritten.stargate.api.network.portal.PortalPositionAttachment; +import org.sgrewritten.stargate.api.network.portal.formatting.data.LineData; + +/** + * A formatter for formatting a line on a sign + */ +public interface LineFormatter extends PortalPositionAttachment { + + /** + * @param lineData

The line data to format

+ * @return

Formatted sign lines

+ */ + default SignLine[] formatLineData(LineData[] lineData){ + SignLine[] output = new SignLine[lineData.length]; + for (int i = 0; i < lineData.length; i++) { + output[i] = convertToSignLine(lineData[i]); + } + return output; + } + + SignLine convertToSignLine(LineData lineData); + + @Override + default Type getType() { + return Type.LINE_FORMATTER; + } +} \ No newline at end of file diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/NetworkLine.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/NetworkLine.java similarity index 94% rename from src/main/java/org/sgrewritten/stargate/api/network/portal/format/NetworkLine.java rename to src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/NetworkLine.java index f6709eae..a7b11840 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/NetworkLine.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/NetworkLine.java @@ -1,4 +1,4 @@ -package org.sgrewritten.stargate.api.network.portal.format; +package org.sgrewritten.stargate.api.network.portal.formatting; import net.md_5.bungee.api.ChatColor; import org.sgrewritten.stargate.api.network.Network; diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/PortalLine.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/PortalLine.java similarity index 96% rename from src/main/java/org/sgrewritten/stargate/api/network/portal/format/PortalLine.java rename to src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/PortalLine.java index 0cd231c3..f47d48ed 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/PortalLine.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/PortalLine.java @@ -1,4 +1,4 @@ -package org.sgrewritten.stargate.api.network.portal.format; +package org.sgrewritten.stargate.api.network.portal.formatting; import com.google.common.base.Preconditions; import net.md_5.bungee.api.ChatColor; diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/SignLine.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/SignLine.java similarity index 81% rename from src/main/java/org/sgrewritten/stargate/api/network/portal/format/SignLine.java rename to src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/SignLine.java index 37432ede..18ef84be 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/SignLine.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/SignLine.java @@ -1,4 +1,4 @@ -package org.sgrewritten.stargate.api.network.portal.format; +package org.sgrewritten.stargate.api.network.portal.formatting; import java.util.List; diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/SignLineType.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/SignLineType.java similarity index 87% rename from src/main/java/org/sgrewritten/stargate/api/network/portal/format/SignLineType.java rename to src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/SignLineType.java index 8add1ae1..482db760 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/SignLineType.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/SignLineType.java @@ -1,4 +1,4 @@ -package org.sgrewritten.stargate.api.network.portal.format; +package org.sgrewritten.stargate.api.network.portal.formatting; public enum SignLineType { diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/StargateComponent.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/StargateComponent.java similarity index 91% rename from src/main/java/org/sgrewritten/stargate/api/network/portal/format/StargateComponent.java rename to src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/StargateComponent.java index 9e7c44d3..11ca5032 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/StargateComponent.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/StargateComponent.java @@ -1,12 +1,10 @@ -package org.sgrewritten.stargate.api.network.portal.format; +package org.sgrewritten.stargate.api.network.portal.formatting; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.md_5.bungee.api.ChatColor; import org.jetbrains.annotations.Nullable; -import org.sgrewritten.stargate.property.NonLegacyClass; /** * A wrapper class to be able to store both legacy text and {@link Component} text without causing failures when adventure diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/StargateComponentDeserialiser.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/StargateComponentDeserialiser.java similarity index 95% rename from src/main/java/org/sgrewritten/stargate/api/network/portal/format/StargateComponentDeserialiser.java rename to src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/StargateComponentDeserialiser.java index 2c686d4e..7a530236 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/StargateComponentDeserialiser.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/StargateComponentDeserialiser.java @@ -1,4 +1,4 @@ -package org.sgrewritten.stargate.api.network.portal.format; +package org.sgrewritten.stargate.api.network.portal.formatting; import net.kyori.adventure.text.Component; import org.jetbrains.annotations.Nullable; diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/TextLine.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/TextLine.java similarity index 96% rename from src/main/java/org/sgrewritten/stargate/api/network/portal/format/TextLine.java rename to src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/TextLine.java index 355e35dd..0fdf72f6 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/portal/format/TextLine.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/TextLine.java @@ -1,4 +1,4 @@ -package org.sgrewritten.stargate.api.network.portal.format; +package org.sgrewritten.stargate.api.network.portal.formatting; import net.md_5.bungee.api.ChatColor; import org.sgrewritten.stargate.network.portal.formatting.HighlightingStyle; diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/LineData.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/LineData.java new file mode 100644 index 00000000..9b248c0f --- /dev/null +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/LineData.java @@ -0,0 +1,23 @@ +package org.sgrewritten.stargate.api.network.portal.formatting.data; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.sgrewritten.stargate.api.network.portal.formatting.LineFormatter; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLineType; + +/** + * Used as input arguments for {@link LineFormatter} + */ +public interface LineData{ + + /** + * @return

The line type of this line data

+ */ + @NotNull SignLineType getType(); + + /** + * + * @return

The unformatted text for this line data

+ */ + @NotNull String getText(); +} diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/NetworkLineData.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/NetworkLineData.java new file mode 100644 index 00000000..8b099bc2 --- /dev/null +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/NetworkLineData.java @@ -0,0 +1,30 @@ +package org.sgrewritten.stargate.api.network.portal.formatting.data; + +import org.jetbrains.annotations.NotNull; +import org.sgrewritten.stargate.api.network.Network; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLineType; + +import java.util.Objects; + +public class NetworkLineData implements LineData { + + private final Network network; + + public NetworkLineData(@NotNull Network network) { + this.network = Objects.requireNonNull(network); + } + + public @NotNull Network getNetwork() { + return network; + } + + @Override + public @NotNull SignLineType getType() { + return SignLineType.NETWORK; + } + + @Override + public @NotNull String getText() { + return network.getName(); + } +} diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/PortalLineData.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/PortalLineData.java new file mode 100644 index 00000000..8b03f868 --- /dev/null +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/PortalLineData.java @@ -0,0 +1,40 @@ +package org.sgrewritten.stargate.api.network.portal.formatting.data; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.sgrewritten.stargate.api.network.portal.Portal; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLineType; + +import java.util.Objects; + +public class PortalLineData implements LineData { + + private Portal portal; + private final SignLineType portalType; + private final String portalName; + + public PortalLineData(@NotNull Portal portal, SignLineType portalType){ + this.portal = Objects.requireNonNull(portal); + this.portalName = portal.getName(); + this.portalType = Objects.requireNonNull(portalType); + } + + public PortalLineData(@NotNull String portalName, SignLineType portalType){ + this.portalName = Objects.requireNonNull(portalName); + this.portalType = Objects.requireNonNull(portalType); + } + + public @Nullable Portal getPortal() { + return this.portal; + } + + @Override + public @NotNull SignLineType getType() { + return portalType; + } + + @Override + public @NotNull String getText() { + return portalName; + } +} diff --git a/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/TextLineData.java b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/TextLineData.java new file mode 100644 index 00000000..82330d24 --- /dev/null +++ b/src/main/java/org/sgrewritten/stargate/api/network/portal/formatting/data/TextLineData.java @@ -0,0 +1,43 @@ +package org.sgrewritten.stargate.api.network.portal.formatting.data; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLineType; +import org.sgrewritten.stargate.network.portal.formatting.HighlightingStyle; + +public class TextLineData implements LineData { + + private final String text; + private final SignLineType type; + private HighlightingStyle highlightingStyle = null; + + public TextLineData(){ + this.text = ""; + this.type = SignLineType.TEXT; + } + + public TextLineData(String text, SignLineType type){ + this.text = text; + this.type = type; + } + + public TextLineData(String text, HighlightingStyle highlightingStyle) { + this.text = text; + this.type = SignLineType.TEXT; + this.highlightingStyle = highlightingStyle; + } + + @Override + public @NotNull SignLineType getType() { + return this.type; + } + + @Override + public @NotNull String getText() { + return this.text; + } + + public @Nullable HighlightingStyle getHighlightingStyle(){ + return this.highlightingStyle; + } +} diff --git a/src/main/java/org/sgrewritten/stargate/gate/Gate.java b/src/main/java/org/sgrewritten/stargate/gate/Gate.java index 2f6a6542..104496a9 100644 --- a/src/main/java/org/sgrewritten/stargate/gate/Gate.java +++ b/src/main/java/org/sgrewritten/stargate/gate/Gate.java @@ -31,8 +31,10 @@ 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.network.portal.formatting.LineFormatter; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLine; +import org.sgrewritten.stargate.api.network.portal.formatting.StargateComponentDeserialiser; +import org.sgrewritten.stargate.api.network.portal.formatting.data.LineData; import org.sgrewritten.stargate.api.vectorlogic.MatrixVectorOperation; import org.sgrewritten.stargate.api.vectorlogic.VectorOperation; import org.sgrewritten.stargate.exception.GateConflictException; @@ -118,10 +120,16 @@ public Gate(GateData gateData, @NotNull RegistryAPI registry) throws InvalidStru } @Override - public void drawControlMechanisms(SignLine[] signLines, boolean drawButton) { - drawSigns(signLines); - if (drawButton) { - drawButtons(); + public void drawControlMechanisms(LineData[] lines) { + portalPositions.forEach(portalPosition -> this.redrawPosition(portalPosition, lines)); + } + + @Override + public void redrawPosition(PortalPosition portalPosition, @Nullable LineData[] lines) { + if (portalPosition.getPositionType() == PositionType.SIGN) { + drawSign(portalPosition, Objects.requireNonNull(lines)); + } else if (portalPosition.getPositionType() == PositionType.BUTTON) { + drawButton(portalPosition); } } @@ -133,35 +141,30 @@ public List getPortalPositions() { /** * Draws this gate's signs * - * @param signLines

The lines to draw on the sign

+ * @param lineData

The lines to draw on the sign

*/ - private void drawSigns(final SignLine[] signLines) { - StringBuilder builder = new StringBuilder("Drawing signs with lines:"); - for (SignLine line : signLines) { - builder.append("\n"); - builder.append(StargateComponentDeserialiser.getLegacyText(line)); + private void drawSign(PortalPosition portalPosition, final LineData[] lineData) { + if(!(portalPosition.getAttachment() instanceof LineFormatter lineFormatter)){ + throw new IllegalArgumentException("Expected attachment to be instance of line formatter"); } - Stargate.log(Level.FINEST, builder.toString()); - List activePortalPositions = getActivePortalPositions(PositionType.SIGN); - for (PortalPosition portalPosition : activePortalPositions) { - Location signLocation = getLocation(portalPosition.getRelativePositionLocation()); - new StargateRegionTask(signLocation){ - @Override - public void run() { - Stargate.log(Level.FINER, "Drawing sign at location " + signLocation); - BlockState signState = signLocation.getBlock().getState(); - if (!(signState instanceof Sign sign)) { - Stargate.log(Level.FINE, "Could not find sign at position " + signLocation); - return; - } - StargateSignFormatPortalEvent event = new StargateSignFormatPortalEvent(portal, signLines, portalPosition, signLocation); - Bukkit.getPluginManager().callEvent(event); - SignLine[] newSignLines = event.getLines(); - setSignLines(sign, newSignLines); - sign.update(); + SignLine[] signLines = lineFormatter.formatLineData(lineData); + Location signLocation = getLocation(portalPosition.getRelativePositionLocation()); + new StargateRegionTask(signLocation) { + @Override + public void run() { + Stargate.log(Level.FINER, "Drawing sign at location " + signLocation); + BlockState signState = signLocation.getBlock().getState(); + if (!(signState instanceof Sign sign)) { + Stargate.log(Level.FINE, "Could not find sign at position " + signLocation); + return; } - }.runNow(); - } + StargateSignFormatPortalEvent event = new StargateSignFormatPortalEvent(portal, signLines, portalPosition, signLocation); + Bukkit.getPluginManager().callEvent(event); + SignLine[] newSignLines = event.getLines(); + setSignLines(sign, newSignLines); + sign.update(); + } + }.runNow(); } private void setSignLines(Sign sign, SignLine[] signLines) { @@ -179,27 +182,24 @@ private void setSignLines(Sign sign, SignLine[] signLines) { /** * Draws this gate's button */ - private void drawButtons() { - for (PortalPosition portalPosition : getActivePortalPositions(PositionType.BUTTON)) { - Location buttonLocation = getLocation(portalPosition.getRelativePositionLocation()); - new StargateRegionTask(buttonLocation) { - @Override - public void run() { - Material blockType = buttonLocation.getBlock().getType(); - if (ButtonHelper.isButton(blockType)) { - return; - } - Material buttonMaterial = ButtonHelper.getButtonMaterial(buttonLocation); - Stargate.log(Level.FINEST, "buttonMaterial: " + buttonMaterial); - Directional buttonData = (Directional) Bukkit.createBlockData(buttonMaterial); - buttonData.setFacing(facing); - - buttonLocation.getBlock().setBlockData(buttonData); - BlockDropManager.disableBlockDrops(buttonLocation.getBlock()); + private void drawButton(PortalPosition portalPosition) { + Location buttonLocation = getLocation(portalPosition.getRelativePositionLocation()); + new StargateRegionTask(buttonLocation) { + @Override + public void run() { + Material blockType = buttonLocation.getBlock().getType(); + if (ButtonHelper.isButton(blockType)) { + return; } - }.runNow(); + Material buttonMaterial = ButtonHelper.getButtonMaterial(buttonLocation); + Stargate.log(Level.FINEST, "buttonMaterial: " + buttonMaterial); + Directional buttonData = (Directional) Bukkit.createBlockData(buttonMaterial); + buttonData.setFacing(facing); - } + buttonLocation.getBlock().setBlockData(buttonData); + BlockDropManager.disableBlockDrops(buttonLocation.getBlock()); + } + }.runNow(); } /** @@ -322,7 +322,7 @@ public void run() { @Override public Location getLocation(@NotNull Vector vector) { - return VectorUtils.getLocation(topLeft,converter,vector); + return VectorUtils.getLocation(topLeft, converter, vector); } /** @@ -403,7 +403,7 @@ public void calculatePortalPositions(boolean alwaysOn) throws InvalidStructureEx } } - if(!hasRegisteredAButton){ + if (!hasRegisteredAButton) { throw new InvalidStructureException("Could not find a button position"); } } diff --git a/src/main/java/org/sgrewritten/stargate/listener/PlayerEventListener.java b/src/main/java/org/sgrewritten/stargate/listener/PlayerEventListener.java index 9d37e04f..37a0dbcc 100644 --- a/src/main/java/org/sgrewritten/stargate/listener/PlayerEventListener.java +++ b/src/main/java/org/sgrewritten/stargate/listener/PlayerEventListener.java @@ -102,7 +102,7 @@ private void handleRelevantClickEvent(Block block, PortalPosition portalPosition if (Tag.WALL_SIGNS.isTagged(blockMaterial)) { if (event.getAction() == Action.RIGHT_CLICK_BLOCK && dyePortalSignText(event, portal)) { - portal.setSignColor(ColorConverter.getDyeColorFromMaterial(event.getMaterial())); + portal.setSignColor(ColorConverter.getDyeColorFromMaterial(event.getMaterial()), registry.getPortalPosition(block.getLocation())); event.setUseInteractedBlock(Event.Result.ALLOW); return; } 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 14037174..4b17c4b3 100644 --- a/src/main/java/org/sgrewritten/stargate/network/portal/AbstractPortal.java +++ b/src/main/java/org/sgrewritten/stargate/network/portal/AbstractPortal.java @@ -4,7 +4,6 @@ import org.bukkit.DyeColor; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.Tag; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.Sign; @@ -14,6 +13,7 @@ import org.bukkit.event.Event.Result; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.util.Vector; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.sgrewritten.stargate.Stargate; import org.sgrewritten.stargate.api.config.ConfigurationOption; @@ -32,9 +32,12 @@ 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.SignLineType; -import org.sgrewritten.stargate.api.network.portal.format.TextLine; +import org.sgrewritten.stargate.api.network.portal.formatting.LineFormatter; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLineType; +import org.sgrewritten.stargate.api.network.portal.formatting.TextLine; +import org.sgrewritten.stargate.api.network.portal.formatting.data.LineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.TextLineData; import org.sgrewritten.stargate.api.permission.BypassPermission; import org.sgrewritten.stargate.api.permission.PermissionManager; import org.sgrewritten.stargate.colors.ColorRegistry; @@ -49,7 +52,6 @@ import org.sgrewritten.stargate.network.StorageType; import org.sgrewritten.stargate.network.portal.formatting.LegacyLineColorFormatter; import org.sgrewritten.stargate.network.portal.formatting.LineColorFormatter; -import org.sgrewritten.stargate.network.portal.formatting.LineFormatter; import org.sgrewritten.stargate.network.portal.formatting.NoLineColorFormatter; import org.sgrewritten.stargate.property.NonLegacyClass; import org.sgrewritten.stargate.property.StargateConstant; @@ -91,8 +93,6 @@ public abstract class AbstractPortal implements RealPortal { protected UUID openFor; protected Portal destination = null; protected Portal overriddenDestination = null; - protected LineFormatter colorDrawer; - private long openTime = -1; private UUID ownerUUID; private final GateAPI gate; @@ -134,8 +134,8 @@ public abstract class AbstractPortal implements RealPortal { if (NameHelper.isInvalidName(name)) { throw new NameLengthException("Invalid length of name '" + name + "' , name length must be above 0 and under " + StargateConstant.MAX_TEXT_LENGTH); } - - colorDrawer = new NoLineColorFormatter(); + gate.getPortalPositions().stream().filter(portalPosition -> portalPosition.getPositionType() == PositionType.SIGN) + .forEach(portalPosition -> portalPosition.setAttachment(new NoLineColorFormatter())); if (gate.getFormat().isIronDoorBlockable()) { flags.add(PortalFlag.IRON_DOOR); @@ -166,7 +166,8 @@ public List getPortalPosition(PositionType type) { @Override public void updateState() { - setSignColor(null); + gate.getPortalPositions().stream().filter(portalPosition -> portalPosition.getPositionType() == PositionType.SIGN) + .forEach(portalPosition -> this.setSignColor(null, portalPosition)); if (getCurrentDestination() == null || this instanceof FixedPortal || hasFlag(PortalFlag.ALWAYS_ON)) { this.destination = getDestination(); } @@ -178,8 +179,7 @@ public void updateState() { if (isOpen() && currentDestination == null) { close(true); } - SignLine[] lines = getDrawnControlLines(); - this.getGate().drawControlMechanisms(lines, !this.hasFlag(PortalFlag.ALWAYS_ON)); + this.getGate().drawControlMechanisms(getDrawnControlLines()); } @Override @@ -189,7 +189,7 @@ public boolean isOpen() { @Override public void open(Player actor) { - if(hasFlag(PortalFlag.ALWAYS_ON) && getCurrentDestination() == null){ + if (hasFlag(PortalFlag.ALWAYS_ON) && getCurrentDestination() == null) { return; } getGate().open(); @@ -224,8 +224,7 @@ public void close(boolean forceClose) { Stargate.log(Level.FINE, "Closing the portal"); getGate().close(); - SignLine[] lines = getDrawnControlLines(); - gate.drawControlMechanisms(lines, this.hasFlag(PortalFlag.ALWAYS_ON)); + gate.drawControlMechanisms(getDrawnControlLines()); openFor = null; } @@ -252,7 +251,7 @@ public void setNetwork(Network targetNetwork) throws NameConflictException { throw new NameConflictException(String.format("Portal of name %s already exists in network %s", this.name, targetNetwork.getId()), false); } this.network = targetNetwork; - this.getDrawnControlLines(); + updateState(); } @Override @@ -424,50 +423,36 @@ public void onButtonClick(PlayerInteractEvent event) { } @Override - public void setSignColor(@Nullable DyeColor changedColor) { + public void setSignColor(@Nullable DyeColor changedColor, @NotNull PortalPosition portalPosition) { + if (!(portalPosition.getAttachment() instanceof LineFormatter lineFormatter)) { + throw new IllegalArgumentException("Could not find line formatter"); + } /* NoLineColorFormatter should only be used during startup, this means * that if it has already been changed, and if there's no color to change to, * then the line formatter does not need to be reinstated again * * Just avoids some unnecessary minute lag */ - if (!(colorDrawer instanceof NoLineColorFormatter) && changedColor == null) { + if (!(lineFormatter instanceof NoLineColorFormatter) && changedColor == null) { return; } - for (PortalPosition portalPosition : gate.getPortalPositions()) { - if (portalPosition.getPositionType() != PositionType.SIGN) { - continue; - } - Location location = gate.getLocation(portalPosition.getRelativePositionLocation()); - new StargateRegionTask(location) { - @Override - public void run() { - updateColorDrawer(location, changedColor, portalPosition); + Location positionLocation = gate.getLocation(portalPosition.getRelativePositionLocation()); + new StargateRegionTask(positionLocation) { + @Override + public void run() { + updateColorDrawer(positionLocation, changedColor, portalPosition); + Block signBlock = positionLocation.getBlock(); + if (signBlock.getState() instanceof Sign sign) { + sign.setColor(ColorRegistry.DEFAULT_DYE_COLOR); + sign.update(); } - }.runNow(); - } - // Has to be done one tick later to avoid a bukkit bug - if (changedColor == null) { - gate.getPortalPositions().stream().filter(portalPosition -> portalPosition.getPositionType() == PositionType.SIGN).forEach(portalPosition -> { - final Block signBlock = gate.getLocation(portalPosition.getRelativePositionLocation()).getBlock(); - new StargateRegionTask(signBlock.getLocation()) { - @Override - public void run() { - if (Tag.WALL_SIGNS.isTagged(signBlock.getType())) { - Sign sign = (Sign) signBlock.getState(); - sign.setColor(ColorRegistry.DEFAULT_DYE_COLOR); - sign.update(); - } - } - }.runNow(); - }); - } - + } + }.runNow(); + LineData[] lineData = getDrawnControlLines(); new StargateGlobalTask() { @Override public void run() { - SignLine[] lines = getDrawnControlLines(); - getGate().drawControlMechanisms(lines, !hasFlag(PortalFlag.ALWAYS_ON)); + getGate().redrawPosition(portalPosition, lineData); } }.runDelayed(2); } @@ -487,12 +472,13 @@ private void updateColorDrawer(Location location, DyeColor changedColor, PortalP Bukkit.getPluginManager().callEvent(event); color = changedColor; } - + LineFormatter lineFormatter; if (NonLegacyClass.CHAT_COLOR.isImplemented()) { - colorDrawer = new LineColorFormatter(color, sign.getType()); + lineFormatter = new LineColorFormatter(color, sign.getType()); } else { - colorDrawer = new LegacyLineColorFormatter(); + lineFormatter = new LegacyLineColorFormatter(); } + portalPosition.setAttachment(lineFormatter); } @Override @@ -504,8 +490,13 @@ public GateAPI getGate() { public void destroy() { this.isDestroyed = true; // drawing the sign first is necessary, as the portal positions gets unregistered from the gate later on - SignLine[] lines = new SignLine[]{new TextLine(getName(), SignLineType.TEXT), new TextLine(), new TextLine(), new TextLine()}; - getGate().drawControlMechanisms(lines, false); + LineData[] lineData = new LineData[] { + new TextLineData(this.getName(), SignLineType.TEXT), + new TextLineData(), + new TextLineData(), + new TextLineData() + }; + getGate().drawControlMechanisms(lineData); this.close(true); } @@ -555,7 +546,6 @@ public void onSignClick(PlayerInteractEvent event) { } if (!event.getPlayer().isSneaking()) { - this.getDrawnControlLines(); return; } PermissionManager permissionManager = new StargatePermissionManager(event.getPlayer(), languageManager); @@ -567,17 +557,16 @@ public void onSignClick(PlayerInteractEvent event) { return; } - SignLine[] lines = new SignLine[]{ - new TextLine(this.colorDrawer.formatLine(languageManager.getString(TranslatableMessage.PREFIX).trim())), - new TextLine(this.colorDrawer - .formatLine(languageManager.getString(TranslatableMessage.GATE_OWNED_BY))), - new TextLine(this.colorDrawer.formatLine(Bukkit.getOfflinePlayer(ownerUUID).getName())), - new TextLine(this.colorDrawer.formatLine(INTERNAL_FLAG.matcher(getAllFlagsString()).replaceAll(""))) + LineData[] lineData = new LineData[]{ + new TextLineData(languageManager.getString(TranslatableMessage.PREFIX).trim(), SignLineType.TEXT), + new TextLineData(languageManager.getString(TranslatableMessage.GATE_OWNED_BY).trim(), SignLineType.TEXT), + new TextLineData(Bukkit.getOfflinePlayer(ownerUUID).getName(), SignLineType.TEXT), + new TextLineData(INTERNAL_FLAG.matcher(getAllFlagsString()).replaceAll(""), SignLineType.TEXT) }; new StargateGlobalTask() { @Override public void run() { - gate.drawControlMechanisms(lines, false); + gate.drawControlMechanisms(lineData); } }.runNow(); activate(event.getPlayer()); @@ -627,8 +616,7 @@ protected void deactivate() { Bukkit.getPluginManager().callEvent(event); this.activator = null; - SignLine[] lines = getDrawnControlLines(); - gate.drawControlMechanisms(lines, !flags.contains(PortalFlag.ALWAYS_ON)); + gate.drawControlMechanisms(getDrawnControlLines()); } @Override diff --git a/src/main/java/org/sgrewritten/stargate/network/portal/BungeePortal.java b/src/main/java/org/sgrewritten/stargate/network/portal/BungeePortal.java index aeb5971a..a68efb54 100644 --- a/src/main/java/org/sgrewritten/stargate/network/portal/BungeePortal.java +++ b/src/main/java/org/sgrewritten/stargate/network/portal/BungeePortal.java @@ -1,18 +1,19 @@ package org.sgrewritten.stargate.network.portal; import org.sgrewritten.stargate.Stargate; -import org.sgrewritten.stargate.api.config.ConfigurationOption; import org.sgrewritten.stargate.api.formatting.LanguageManager; import org.sgrewritten.stargate.api.formatting.TranslatableMessage; import org.sgrewritten.stargate.api.gate.GateAPI; import org.sgrewritten.stargate.api.network.Network; import org.sgrewritten.stargate.api.network.portal.Portal; import org.sgrewritten.stargate.api.network.portal.PortalFlag; -import org.sgrewritten.stargate.api.network.portal.format.PortalLine; -import org.sgrewritten.stargate.api.network.portal.format.SignLine; -import org.sgrewritten.stargate.api.network.portal.format.SignLineType; -import org.sgrewritten.stargate.api.network.portal.format.TextLine; -import org.sgrewritten.stargate.config.ConfigurationHelper; +import org.sgrewritten.stargate.api.network.portal.formatting.PortalLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLineType; +import org.sgrewritten.stargate.api.network.portal.formatting.TextLine; +import org.sgrewritten.stargate.api.network.portal.formatting.data.LineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.PortalLineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.TextLineData; import org.sgrewritten.stargate.economy.StargateEconomyAPI; import org.sgrewritten.stargate.exception.UnimplementedFlagException; import org.sgrewritten.stargate.exception.name.BungeeNameException; @@ -90,14 +91,12 @@ public BungeePortal(Network network, String name, String destination, String des } @Override - public SignLine[] getDrawnControlLines() { - Stargate.log(Level.FINEST, "serverDestination = " + serverDestination); - - return new SignLine[]{ - new PortalLine(super.colorDrawer.formatPortalName(this, HighlightingStyle.MINUS_SIGN), this, SignLineType.THIS_PORTAL), - new PortalLine(super.colorDrawer.formatPortalName(getDestination(), HighlightingStyle.LESSER_GREATER_THAN), getDestination(), SignLineType.DESTINATION_PORTAL), - new TextLine(super.colorDrawer.formatStringWithHighlighting(serverDestination, HighlightingStyle.SQUARE_BRACKETS), SignLineType.TEXT), - new TextLine(bungeeString) + public LineData[] getDrawnControlLines() { + return new LineData[] { + new PortalLineData(this, SignLineType.THIS_PORTAL), + new PortalLineData(getDestination(), SignLineType.DESTINATION_PORTAL), + new TextLineData(serverDestination, HighlightingStyle.SQUARE_BRACKETS), + new TextLineData(bungeeString, SignLineType.TEXT) }; } diff --git a/src/main/java/org/sgrewritten/stargate/network/portal/FixedPortal.java b/src/main/java/org/sgrewritten/stargate/network/portal/FixedPortal.java index fd761f0d..41d9766c 100644 --- a/src/main/java/org/sgrewritten/stargate/network/portal/FixedPortal.java +++ b/src/main/java/org/sgrewritten/stargate/network/portal/FixedPortal.java @@ -6,14 +6,13 @@ import org.sgrewritten.stargate.api.network.Network; import org.sgrewritten.stargate.api.network.portal.Portal; import org.sgrewritten.stargate.api.network.portal.PortalFlag; -import org.sgrewritten.stargate.api.network.portal.format.NetworkLine; -import org.sgrewritten.stargate.api.network.portal.format.PortalLine; -import org.sgrewritten.stargate.api.network.portal.format.SignLine; -import org.sgrewritten.stargate.api.network.portal.format.SignLineType; -import org.sgrewritten.stargate.api.network.portal.format.TextLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLineType; +import org.sgrewritten.stargate.api.network.portal.formatting.data.LineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.NetworkLineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.PortalLineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.TextLineData; import org.sgrewritten.stargate.economy.StargateEconomyAPI; import org.sgrewritten.stargate.exception.name.NameLengthException; -import org.sgrewritten.stargate.network.portal.formatting.HighlightingStyle; import java.util.Set; import java.util.UUID; @@ -36,26 +35,23 @@ public class FixedPortal extends AbstractPortal { * @param ownerUUID

The UUID of the portal's owner

* @throws NameLengthException */ - public FixedPortal(Network network, String name, String destinationName, Set flags, Set unrecognisedFlags, GateAPI gate, - UUID ownerUUID, LanguageManager languageManager, StargateEconomyAPI economyAPI, String metaData) throws NameLengthException { + public FixedPortal(Network network, String name, String destinationName, Set flags, Set unrecognisedFlags, GateAPI gate, UUID ownerUUID, LanguageManager languageManager, StargateEconomyAPI economyAPI, String metaData) throws NameLengthException { super(network, name, flags, unrecognisedFlags, gate, ownerUUID, languageManager, economyAPI, metaData); this.destinationName = destinationName; this.destination = network.getPortal(destinationName); } @Override - public SignLine[] getDrawnControlLines() { - SignLine[] lines = new SignLine[4]; - lines[0] = new PortalLine(super.colorDrawer.formatPortalName(this, HighlightingStyle.MINUS_SIGN), this, SignLineType.THIS_PORTAL); - lines[2] = new NetworkLine(super.colorDrawer.formatNetworkName(network, network.getHighlightingStyle()), network); - Portal destination = getDestination(); + public LineData[] getDrawnControlLines() { + LineData[] lines = new LineData[4]; + lines[0] = new PortalLineData(this, SignLineType.THIS_PORTAL); + lines[2] = new NetworkLineData(network); if (destination != null) { - lines[1] = new PortalLine(super.colorDrawer.formatPortalName(destination, HighlightingStyle.LESSER_GREATER_THAN), getDestination(), SignLineType.DESTINATION_PORTAL); - lines[3] = new TextLine(); + lines[1] = new PortalLineData(destination, SignLineType.DESTINATION_PORTAL); + lines[3] = new TextLineData(); } else { - lines[1] = new TextLine(super.colorDrawer.formatLine(destinationName), SignLineType.DESTINATION_PORTAL); - lines[3] = new TextLine(super.colorDrawer.formatErrorLine(super.languageManager.getString( - TranslatableMessage.DISCONNECTED), HighlightingStyle.SQUARE_BRACKETS), SignLineType.ERROR); + lines[1] = new PortalLineData(destinationName, SignLineType.DESTINATION_PORTAL); + lines[3] = new TextLineData(super.languageManager.getString(TranslatableMessage.DISCONNECTED), SignLineType.ERROR); } return lines; } diff --git a/src/main/java/org/sgrewritten/stargate/network/portal/NetworkedPortal.java b/src/main/java/org/sgrewritten/stargate/network/portal/NetworkedPortal.java index 3bc36231..8ac6f298 100644 --- a/src/main/java/org/sgrewritten/stargate/network/portal/NetworkedPortal.java +++ b/src/main/java/org/sgrewritten/stargate/network/portal/NetworkedPortal.java @@ -18,11 +18,15 @@ import org.sgrewritten.stargate.api.network.Network; import org.sgrewritten.stargate.api.network.portal.Portal; import org.sgrewritten.stargate.api.network.portal.PortalFlag; -import org.sgrewritten.stargate.api.network.portal.format.NetworkLine; -import org.sgrewritten.stargate.api.network.portal.format.PortalLine; -import org.sgrewritten.stargate.api.network.portal.format.SignLine; -import org.sgrewritten.stargate.api.network.portal.format.SignLineType; -import org.sgrewritten.stargate.api.network.portal.format.TextLine; +import org.sgrewritten.stargate.api.network.portal.formatting.NetworkLine; +import org.sgrewritten.stargate.api.network.portal.formatting.PortalLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLineType; +import org.sgrewritten.stargate.api.network.portal.formatting.TextLine; +import org.sgrewritten.stargate.api.network.portal.formatting.data.LineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.NetworkLineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.PortalLineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.TextLineData; import org.sgrewritten.stargate.config.ConfigurationHelper; import org.sgrewritten.stargate.economy.StargateEconomyAPI; import org.sgrewritten.stargate.exception.name.NameLengthException; @@ -200,13 +204,13 @@ public void close(boolean force) { } @Override - public SignLine[] getDrawnControlLines() { - SignLine[] lines = new SignLine[4]; - lines[0] = new PortalLine(super.colorDrawer.formatPortalName(this, HighlightingStyle.MINUS_SIGN), this, SignLineType.THIS_PORTAL); + public LineData[] getDrawnControlLines() { + LineData[] lines = new LineData[4]; + lines[0] = new PortalLineData(this, SignLineType.THIS_PORTAL); if (!this.isActive || this.getSelectedDestination() == NO_DESTINATION_SELECTED) { - lines[1] = new TextLine(super.colorDrawer.formatLine(super.languageManager.getString(TranslatableMessage.RIGHT_CLICK))); - lines[2] = new TextLine(super.colorDrawer.formatLine(super.languageManager.getString(TranslatableMessage.TO_USE))); - lines[3] = new NetworkLine(super.colorDrawer.formatNetworkName(network, network.getHighlightingStyle()), getNetwork()); + lines[1] = new TextLineData(super.languageManager.getString(TranslatableMessage.RIGHT_CLICK), SignLineType.TEXT); + lines[2] = new TextLineData(super.languageManager.getString(TranslatableMessage.TO_USE), SignLineType.TEXT); + lines[3] = new NetworkLineData(getNetwork()); } else { drawActiveSign(lines); } @@ -226,14 +230,14 @@ public Portal getDestination() { * * @param lines

The sign lines to update

*/ - private void drawActiveSign(SignLine[] lines) { + private void drawActiveSign(LineData[] lines) { int destinationIndex = getSelectedDestination() % 3; int firstDestination = getSelectedDestination() - destinationIndex; int maxLength = destinations.size(); for (int lineIndex = 0; lineIndex < 3; lineIndex++) { int destination = lineIndex + firstDestination; if (destination >= maxLength) { - lines[lineIndex + 1] = new TextLine(); + lines[lineIndex + 1] = new TextLineData(); continue; } drawDestination(lineIndex, destination, destinationIndex, lines); @@ -248,12 +252,12 @@ private void drawActiveSign(SignLine[] lines) { * @param destinationIndex

The modulo of the selected destination

* @param lines

The sign lines to update

*/ - private void drawDestination(int lineIndex, int destination, int destinationIndex, SignLine[] lines) { + private void drawDestination(int lineIndex, int destination, int destinationIndex, LineData[] lines) { boolean isSelectedPortal = (destinationIndex == lineIndex); HighlightingStyle highlightingStyle = isSelectedPortal ? HighlightingStyle.LESSER_GREATER_THAN : HighlightingStyle.NOTHING; Portal destinationPortal = destinations.get(destination); - lines[lineIndex + 1] = new PortalLine(super.colorDrawer.formatPortalName(destinationPortal, highlightingStyle), destinationPortal, isSelectedPortal ? SignLineType.DESTINATION_PORTAL : SignLineType.PORTAL); + lines[lineIndex + 1] = new PortalLineData(destinationPortal, isSelectedPortal ? SignLineType.DESTINATION_PORTAL : SignLineType.PORTAL); } /** diff --git a/src/main/java/org/sgrewritten/stargate/network/portal/RandomPortal.java b/src/main/java/org/sgrewritten/stargate/network/portal/RandomPortal.java index 5a6f0c07..1dce677b 100644 --- a/src/main/java/org/sgrewritten/stargate/network/portal/RandomPortal.java +++ b/src/main/java/org/sgrewritten/stargate/network/portal/RandomPortal.java @@ -8,11 +8,14 @@ import org.sgrewritten.stargate.api.network.Network; import org.sgrewritten.stargate.api.network.portal.Portal; import org.sgrewritten.stargate.api.network.portal.PortalFlag; -import org.sgrewritten.stargate.api.network.portal.format.NetworkLine; -import org.sgrewritten.stargate.api.network.portal.format.PortalLine; -import org.sgrewritten.stargate.api.network.portal.format.SignLine; -import org.sgrewritten.stargate.api.network.portal.format.SignLineType; -import org.sgrewritten.stargate.api.network.portal.format.TextLine; +import org.sgrewritten.stargate.api.network.portal.formatting.NetworkLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLineType; +import org.sgrewritten.stargate.api.network.portal.formatting.TextLine; +import org.sgrewritten.stargate.api.network.portal.formatting.data.LineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.NetworkLineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.PortalLineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.TextLineData; import org.sgrewritten.stargate.economy.StargateEconomyAPI; import org.sgrewritten.stargate.exception.name.NameLengthException; import org.sgrewritten.stargate.network.portal.formatting.HighlightingStyle; @@ -39,20 +42,17 @@ public class RandomPortal extends AbstractPortal { * @param ownerUUID

The UUID of the portal's owner

* @throws NameLengthException */ - public RandomPortal(Network network, String name, Set flags, Set unrecognisedFlags, GateAPI gate, UUID ownerUUID, LanguageManager languageManager, StargateEconomyAPI economyAPI, String metaData) - throws NameLengthException { + public RandomPortal(Network network, String name, Set flags, Set unrecognisedFlags, GateAPI gate, UUID ownerUUID, LanguageManager languageManager, StargateEconomyAPI economyAPI, String metaData) throws NameLengthException { super(network, name, flags, unrecognisedFlags, gate, ownerUUID, languageManager, economyAPI, metaData); } @Override - public SignLine[] getDrawnControlLines() { - return new SignLine[]{ - new PortalLine(super.colorDrawer.formatPortalName(this, HighlightingStyle.MINUS_SIGN), this, SignLineType.THIS_PORTAL), - new TextLine(super.colorDrawer.formatLine(HighlightingStyle.LESSER_GREATER_THAN.getHighlightedName( - super.languageManager.getString(TranslatableMessage.RANDOM)))), - new NetworkLine(super.colorDrawer.formatNetworkName(getNetwork(), getNetwork().getHighlightingStyle()), getNetwork()), - new TextLine() - }; + public LineData[] getDrawnControlLines() { + return new LineData[]{ + new PortalLineData(this, SignLineType.THIS_PORTAL), + new TextLineData(super.languageManager.getString(TranslatableMessage.RANDOM),HighlightingStyle.LESSER_GREATER_THAN), + new NetworkLineData(getNetwork()), + new TextLineData()}; } @Override diff --git a/src/main/java/org/sgrewritten/stargate/network/portal/formatting/LegacyLineColorFormatter.java b/src/main/java/org/sgrewritten/stargate/network/portal/formatting/LegacyLineColorFormatter.java index c55ce743..2996b06b 100644 --- a/src/main/java/org/sgrewritten/stargate/network/portal/formatting/LegacyLineColorFormatter.java +++ b/src/main/java/org/sgrewritten/stargate/network/portal/formatting/LegacyLineColorFormatter.java @@ -3,32 +3,51 @@ import org.bukkit.ChatColor; import org.sgrewritten.stargate.api.network.Network; import org.sgrewritten.stargate.api.network.portal.Portal; -import org.sgrewritten.stargate.api.network.portal.format.StargateComponent; +import org.sgrewritten.stargate.api.network.portal.formatting.LineFormatter; +import org.sgrewritten.stargate.api.network.portal.formatting.NetworkLine; +import org.sgrewritten.stargate.api.network.portal.formatting.PortalLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLineType; +import org.sgrewritten.stargate.api.network.portal.formatting.StargateComponent; +import org.sgrewritten.stargate.api.network.portal.formatting.TextLine; +import org.sgrewritten.stargate.api.network.portal.formatting.data.LineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.NetworkLineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.PortalLineData; import org.sgrewritten.stargate.colors.ColorRegistry; import java.util.ArrayList; import java.util.List; public class LegacyLineColorFormatter implements LineFormatter { - @Override - public List formatPortalName(Portal portal, HighlightingStyle highlightingStyle) { - return new ArrayList<>(List.of( + public SignLine convertToSignLine(LineData lineData){ + return switch (lineData.getType()){ + case ERROR -> new TextLine(formatErrorLine(lineData.getText(), HighlightingStyle.SQUARE_BRACKETS), SignLineType.ERROR); + case TEXT -> new TextLine(formatLine(lineData.getText())); + case NETWORK -> formatNetworkName((NetworkLineData) lineData); + case DESTINATION_PORTAL -> formatPortalName((PortalLineData) lineData, HighlightingStyle.LESSER_GREATER_THAN); + case THIS_PORTAL -> formatPortalName((PortalLineData) lineData,HighlightingStyle.MINUS_SIGN); + case PORTAL -> formatPortalName((PortalLineData) lineData,HighlightingStyle.NOTHING); + }; + } + + private SignLine formatPortalName(PortalLineData lineData, HighlightingStyle highlightingStyle) { + Portal portal = lineData.getPortal(); + List components = new ArrayList<>(List.of( new StargateComponent(getColor() + highlightingStyle.getPrefix()), - new StargateComponent(getColor() + (portal != null ? portal.getName() : "null")), + new StargateComponent(getColor() + (portal != null ? portal.getName() : lineData.getText())), new StargateComponent(getColor() + highlightingStyle.getSuffix()) )); + return new PortalLine(components, portal, lineData.getType()); } - @Override - public List formatLine(String line) { + private List formatLine(String line) { List output = new ArrayList<>(); output.add(new StargateComponent(getColor() + line)); return output; } - @Override - public List formatErrorLine(String error, HighlightingStyle highlightingStyle) { + private List formatErrorLine(String error, HighlightingStyle highlightingStyle) { return new ArrayList<>(List.of( new StargateComponent(getColor() + highlightingStyle.getPrefix()), new StargateComponent(getColor() + error), @@ -40,22 +59,21 @@ private ChatColor getColor() { return ColorRegistry.LEGACY_SIGN_COLOR; } - @Override - public List formatNetworkName(Network network, HighlightingStyle highlightingStyle) { - return new ArrayList<>(List.of( - new StargateComponent(getColor() + highlightingStyle.getPrefix()), + private SignLine formatNetworkName(NetworkLineData lineData) { + Network network = lineData.getNetwork(); + List components = new ArrayList<>(List.of( + new StargateComponent(getColor() + network.getHighlightingStyle().getPrefix()), new StargateComponent(getColor() + (network == null ? "null" : network.getName())), - new StargateComponent(getColor() + highlightingStyle.getSuffix()) + new StargateComponent(getColor() + network.getHighlightingStyle().getSuffix()) )); + return new NetworkLine(components, network); } - @Override - public List formatStringWithHighlighting(String aString, HighlightingStyle highlightingStyle) { + private List formatStringWithHighlighting(String aString, HighlightingStyle highlightingStyle) { return new ArrayList<>(List.of( new StargateComponent(getColor() + highlightingStyle.getPrefix()), new StargateComponent(getColor() + aString), new StargateComponent(getColor() + highlightingStyle.getSuffix()) )); } - } diff --git a/src/main/java/org/sgrewritten/stargate/network/portal/formatting/LineColorFormatter.java b/src/main/java/org/sgrewritten/stargate/network/portal/formatting/LineColorFormatter.java index ead9fb6f..1a7eea94 100644 --- a/src/main/java/org/sgrewritten/stargate/network/portal/formatting/LineColorFormatter.java +++ b/src/main/java/org/sgrewritten/stargate/network/portal/formatting/LineColorFormatter.java @@ -8,7 +8,16 @@ import org.sgrewritten.stargate.api.network.Network; import org.sgrewritten.stargate.api.network.portal.Portal; import org.sgrewritten.stargate.api.network.portal.PortalFlag; -import org.sgrewritten.stargate.api.network.portal.format.StargateComponent; +import org.sgrewritten.stargate.api.network.portal.formatting.LineFormatter; +import org.sgrewritten.stargate.api.network.portal.formatting.NetworkLine; +import org.sgrewritten.stargate.api.network.portal.formatting.PortalLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLineType; +import org.sgrewritten.stargate.api.network.portal.formatting.StargateComponent; +import org.sgrewritten.stargate.api.network.portal.formatting.TextLine; +import org.sgrewritten.stargate.api.network.portal.formatting.data.LineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.NetworkLineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.PortalLineData; import org.sgrewritten.stargate.colors.ColorConverter; import org.sgrewritten.stargate.colors.ColorRegistry; import org.sgrewritten.stargate.colors.ColorSelector; @@ -40,40 +49,50 @@ public LineColorFormatter(DyeColor dyeColor, Material signMaterial) { pointerColor = this.getPointerColor(); } + @Override - public List formatPortalName(Portal portal, HighlightingStyle highlightingStyle) { + public SignLine convertToSignLine(LineData lineData) { + return switch (lineData.getType()) { + case ERROR -> + new TextLine(formatErrorLine(lineData.getText(), HighlightingStyle.SQUARE_BRACKETS), SignLineType.ERROR); + case TEXT -> new TextLine(formatLine(lineData.getText())); + case NETWORK -> formatNetworkName((NetworkLineData) lineData); + case DESTINATION_PORTAL -> + formatPortalName((PortalLineData) lineData, HighlightingStyle.LESSER_GREATER_THAN); + case THIS_PORTAL -> formatPortalName((PortalLineData) lineData, HighlightingStyle.MINUS_SIGN); + case PORTAL -> formatPortalName((PortalLineData) lineData, HighlightingStyle.NOTHING); + }; + } + + private SignLine formatPortalName(PortalLineData lineData, HighlightingStyle highlightingStyle) { ChatColor pointerColorTemp = this.pointerColor; + Portal portal = lineData.getPortal(); if (ConfigurationHelper.getInteger(ConfigurationOption.POINTER_BEHAVIOR) == 2 && getFlagColor(portal) != null) { pointerColorTemp = getFlagColor(portal); } - String portalName = (portal != null) ? portal.getName() : "null"; - return new ArrayList<>(List.of( + String portalName = (portal != null) ? portal.getName() : lineData.getText(); + List components = new ArrayList<>(List.of( new StargateComponent(pointerColorTemp + highlightingStyle.getPrefix()), new StargateComponent(color + portalName), new StargateComponent(pointerColorTemp + highlightingStyle.getSuffix()) )); + return new PortalLine(components, portal, lineData.getType()); } - @Override - public List formatNetworkName(Network network, HighlightingStyle highlightingStyle) { - String networkName; - String bold; - if (network == null) { - networkName = "null"; - bold = ""; - } else { - networkName = network.getName(); - bold = (network.getStorageType() == StorageType.INTER_SERVER) ? ChatColor.BOLD.toString() : ""; - } - return new ArrayList<>(List.of( + private SignLine formatNetworkName(NetworkLineData networkLineData) { + Network network = networkLineData.getNetwork(); + HighlightingStyle highlightingStyle = network.getHighlightingStyle(); + String networkName = network.getName(); + String bold = (network.getStorageType() == StorageType.INTER_SERVER) ? ChatColor.BOLD.toString() : ""; + List components = new ArrayList<>(List.of( new StargateComponent(pointerColor + bold + highlightingStyle.getPrefix()), new StargateComponent(color + bold + networkName), new StargateComponent(pointerColor + bold + highlightingStyle.getSuffix()) )); + return new NetworkLine(components, network); } - @Override - public List formatStringWithHighlighting(String aString, HighlightingStyle highlightingStyle) { + private List formatStringWithHighlighting(String aString, HighlightingStyle highlightingStyle) { return new ArrayList<>(List.of( new StargateComponent(pointerColor + highlightingStyle.getPrefix()), new StargateComponent(color + aString), @@ -81,13 +100,11 @@ public List formatStringWithHighlighting(String aString, High )); } - @Override - public List formatLine(String line) { + private List formatLine(String line) { return new ArrayList<>(List.of(new StargateComponent(color + line))); } - @Override - public List formatErrorLine(String error, HighlightingStyle highlightingStyle) { + private List formatErrorLine(String error, HighlightingStyle highlightingStyle) { return new ArrayList<>(List.of( new StargateComponent(ERROR_COLOR + highlightingStyle.getPrefix()), new StargateComponent(ERROR_COLOR + error), diff --git a/src/main/java/org/sgrewritten/stargate/network/portal/formatting/LineFormatter.java b/src/main/java/org/sgrewritten/stargate/network/portal/formatting/LineFormatter.java deleted file mode 100644 index ecffea6a..00000000 --- a/src/main/java/org/sgrewritten/stargate/network/portal/formatting/LineFormatter.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.sgrewritten.stargate.network.portal.formatting; - -import org.sgrewritten.stargate.api.network.Network; -import org.sgrewritten.stargate.api.network.portal.Portal; -import org.sgrewritten.stargate.api.network.portal.format.StargateComponent; - -import java.util.List; - -/** - * A formatter for formatting a line on a sign - */ -public interface LineFormatter { - - /** - * Formats the name of a portal - * - * @param portal

The portal whose name is to be formatted

- * @param highlightingStyle

The highlighting style to use when formatting

- * @return

The formatted name

- */ - List formatPortalName(Portal portal, HighlightingStyle highlightingStyle); - - /** - * Formats the name of a portal - * - * @param network

The network whose name is to be formatted

- * @param highlightingStyle

The highlighting style to use when formatting

- * @return

The formatted name

- */ - List formatNetworkName(Network network, HighlightingStyle highlightingStyle); - - /** - * Formats the name of a portal - * - * @param aString

Any type of string

- * @param highlightingStyle

The highlighting style to use when formatting

- * @return

The formatted string

- */ - List formatStringWithHighlighting(String aString, HighlightingStyle highlightingStyle); - - /** - * Formats a line using the default behaviour - * - * @param line

The line to format

- * @return

The formatted line

- */ - List formatLine(String line); - - /** - * Formats an error line to display on a sign - * - * @param error

The error message to format

- * @param highlightingStyle

The highlighting style to use when formatting

- * @return

The formatted error

- */ - List formatErrorLine(String error, HighlightingStyle highlightingStyle); - -} \ No newline at end of file diff --git a/src/main/java/org/sgrewritten/stargate/network/portal/formatting/NoLineColorFormatter.java b/src/main/java/org/sgrewritten/stargate/network/portal/formatting/NoLineColorFormatter.java index 822023fb..ca5e04f4 100644 --- a/src/main/java/org/sgrewritten/stargate/network/portal/formatting/NoLineColorFormatter.java +++ b/src/main/java/org/sgrewritten/stargate/network/portal/formatting/NoLineColorFormatter.java @@ -2,7 +2,16 @@ import org.sgrewritten.stargate.api.network.Network; import org.sgrewritten.stargate.api.network.portal.Portal; -import org.sgrewritten.stargate.api.network.portal.format.StargateComponent; +import org.sgrewritten.stargate.api.network.portal.formatting.LineFormatter; +import org.sgrewritten.stargate.api.network.portal.formatting.NetworkLine; +import org.sgrewritten.stargate.api.network.portal.formatting.PortalLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLine; +import org.sgrewritten.stargate.api.network.portal.formatting.SignLineType; +import org.sgrewritten.stargate.api.network.portal.formatting.StargateComponent; +import org.sgrewritten.stargate.api.network.portal.formatting.TextLine; +import org.sgrewritten.stargate.api.network.portal.formatting.data.LineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.NetworkLineData; +import org.sgrewritten.stargate.api.network.portal.formatting.data.PortalLineData; import java.util.ArrayList; import java.util.List; @@ -15,44 +24,26 @@ public class NoLineColorFormatter implements LineFormatter { @Override - public List formatPortalName(Portal portal, HighlightingStyle highlightingStyle) { - return new ArrayList<>(List.of( - new StargateComponent(highlightingStyle.getPrefix()), - new StargateComponent((portal != null) ? portal.getName() : "null"), - new StargateComponent(highlightingStyle.getSuffix()) - )); + public SignLine convertToSignLine(LineData lineData) { + return switch (lineData.getType()){ + case ERROR -> new TextLine(HighlightingStyle.SQUARE_BRACKETS.getHighlightedName(lineData.getText()), SignLineType.ERROR); + case TEXT -> new TextLine(lineData.getText()); + case NETWORK ->getNetworkSignLine((NetworkLineData) lineData); + case DESTINATION_PORTAL -> getPortalSignLine((PortalLineData) lineData, HighlightingStyle.LESSER_GREATER_THAN); + case THIS_PORTAL -> getPortalSignLine((PortalLineData) lineData, HighlightingStyle.MINUS_SIGN); + case PORTAL -> getPortalSignLine((PortalLineData) lineData, HighlightingStyle.NOTHING); + }; } - @Override - public List formatLine(String line) { - return new ArrayList<>(List.of(new StargateComponent(line))); + private SignLine getNetworkSignLine(NetworkLineData lineData){ + Network network = lineData.getNetwork(); + HighlightingStyle highlightingStyle = network.getHighlightingStyle(); + return new NetworkLine(List.of(new StargateComponent(highlightingStyle.getHighlightedName(network.getName()))),network); } - @Override - public List formatErrorLine(String error, HighlightingStyle highlightingStyle) { - return new ArrayList<>(List.of( - new StargateComponent(highlightingStyle.getPrefix()), - new StargateComponent(error), - new StargateComponent(highlightingStyle.getSuffix()) - )); - } - - @Override - public List formatNetworkName(Network network, HighlightingStyle highlightingStyle) { - return new ArrayList<>(List.of( - new StargateComponent(highlightingStyle.getPrefix()), - new StargateComponent((network != null) ? network.getName() : "null"), - new StargateComponent(highlightingStyle.getSuffix()) - )); + private SignLine getPortalSignLine(PortalLineData lineData, HighlightingStyle highlightingStyle){ + Portal portal = lineData.getPortal(); + String portalName = portal == null ? lineData.getText() : portal.getName(); + return new PortalLine(List.of(new StargateComponent(highlightingStyle.getHighlightedName(portalName))),portal,lineData.getType()); } - - @Override - public List formatStringWithHighlighting(String aString, HighlightingStyle highlightingStyle) { - return new ArrayList<>(List.of( - new StargateComponent(highlightingStyle.getPrefix()), - new StargateComponent(aString), - new StargateComponent(highlightingStyle.getSuffix()) - )); - } - } diff --git a/src/main/java/org/sgrewritten/stargate/util/MessageUtils.java b/src/main/java/org/sgrewritten/stargate/util/MessageUtils.java index 0f05e862..ada57417 100644 --- a/src/main/java/org/sgrewritten/stargate/util/MessageUtils.java +++ b/src/main/java/org/sgrewritten/stargate/util/MessageUtils.java @@ -9,7 +9,7 @@ import org.sgrewritten.stargate.api.event.portal.message.StargateSendMessagePortalEvent; import org.sgrewritten.stargate.api.event.portal.message.SyncStargateSendMessagePortalEvent; import org.sgrewritten.stargate.api.network.portal.Portal; -import org.sgrewritten.stargate.api.network.portal.format.StargateComponent; +import org.sgrewritten.stargate.api.network.portal.formatting.StargateComponent; import org.sgrewritten.stargate.property.NonLegacyClass; public class MessageUtils { diff --git a/src/test/java/org/sgrewritten/stargate/api/network/portal/format/StargateComponentTest.java b/src/test/java/org/sgrewritten/stargate/api/network/portal/formatting/StargateComponentTest.java similarity index 95% rename from src/test/java/org/sgrewritten/stargate/api/network/portal/format/StargateComponentTest.java rename to src/test/java/org/sgrewritten/stargate/api/network/portal/formatting/StargateComponentTest.java index 5fe08809..4976d564 100644 --- a/src/test/java/org/sgrewritten/stargate/api/network/portal/format/StargateComponentTest.java +++ b/src/test/java/org/sgrewritten/stargate/api/network/portal/formatting/StargateComponentTest.java @@ -1,11 +1,9 @@ -package org.sgrewritten.stargate.api.network.portal.format; +package org.sgrewritten.stargate.api.network.portal.formatting; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.md_5.bungee.api.ChatColor; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; diff --git a/src/test/java/org/sgrewritten/stargate/gate/GateMock.java b/src/test/java/org/sgrewritten/stargate/gate/GateMock.java index 76acf6ab..bdd0ea84 100644 --- a/src/test/java/org/sgrewritten/stargate/gate/GateMock.java +++ b/src/test/java/org/sgrewritten/stargate/gate/GateMock.java @@ -13,7 +13,8 @@ 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.formatting.SignLine; +import org.sgrewritten.stargate.api.network.portal.formatting.data.LineData; import org.sgrewritten.stargate.exception.GateConflictException; import java.util.List; @@ -22,7 +23,12 @@ public class GateMock implements GateAPI { private RealPortal portal; @Override - public void drawControlMechanisms(SignLine[] signLines, boolean drawButton) { + public void drawControlMechanisms(LineData[] lines) { + + } + + @Override + public void redrawPosition(PortalPosition portalPosition, @Nullable LineData[] lines) { } 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 3798535c..0f34438a 100644 --- a/src/test/java/org/sgrewritten/stargate/util/portal/PortalMock.java +++ b/src/test/java/org/sgrewritten/stargate/util/portal/PortalMock.java @@ -10,9 +10,11 @@ import org.sgrewritten.stargate.api.network.Network; import org.sgrewritten.stargate.api.network.portal.Portal; import org.sgrewritten.stargate.api.network.portal.PortalFlag; +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.formatting.SignLine; +import org.sgrewritten.stargate.api.network.portal.formatting.data.LineData; import org.sgrewritten.stargate.gate.GateMock; import org.sgrewritten.stargate.network.StorageType; import org.sgrewritten.stargate.network.portal.GlobalPortalId; @@ -157,12 +159,12 @@ public boolean isDestroyed() { } @Override - public SignLine[] getDrawnControlLines() { + public LineData[] getDrawnControlLines() { return null; } @Override - public void setSignColor(DyeColor color) { + public void setSignColor(DyeColor color, PortalPosition portalPosition) { }