Skip to content

Commit

Permalink
Introduce LineData instead of SignLine before formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorinwasher committed Feb 29, 2024
1 parent 67f4895 commit 23fec8b
Show file tree
Hide file tree
Showing 38 changed files with 502 additions and 348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/org/sgrewritten/stargate/api/gate/GateAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,11 +26,14 @@ public interface GateAPI {

/**
* Set button and draw sign
*
* @param signLines <p>an array with 4 elements, representing each line of a sign</p>
* @param drawButton <p>whether or not include a button.</p>
*/
void drawControlMechanisms(SignLine[] signLines, boolean drawButton);
void drawControlMechanisms(LineData[] lines);

/**
* Update the state on the portal position according to its type
* @param portalPosition <p>The portal position to update</p>
*/
void redrawPosition(PortalPosition portalPosition, @Nullable LineData[] lines);

/**
* Gets a copy of this gate's portal positions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.sgrewritten.stargate.api.network.portal;

public interface PortalPositionAttachment {


Type getType();

enum Type {
LINE_FORMATTER
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,10 +20,9 @@ public interface RealPortal extends Portal, MetadataHolder {

/**
* Gets the line to be drawn for the signs
*
* @return <p> Lines to be drawn for the gate owned by this portal</p>
*/
SignLine[] getDrawnControlLines();
LineData[] getDrawnControlLines();

/**
* Updates the color of this portal's sign
Expand All @@ -32,7 +32,7 @@ public interface RealPortal extends Portal, MetadataHolder {
*
* @param color <p>Color to change the sign text to. If null, then the default color will be used</p>
*/
void setSignColor(DyeColor color);
void setSignColor(DyeColor color, PortalPosition portalPosition);

/**
* The action to be run when this portal's button is clicked
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <p>The line data to format</p>
* @return <p>Formatted sign lines</p>
*/
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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sgrewritten.stargate.api.network.portal.format;
package org.sgrewritten.stargate.api.network.portal.formatting;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sgrewritten.stargate.api.network.portal.format;
package org.sgrewritten.stargate.api.network.portal.formatting;

public enum SignLineType {

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <p>The line type of this line data</p>
*/
@NotNull SignLineType getType();

/**
*
* @return <p>The unformatted text for this line data</p>
*/
@NotNull String getText();
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 23fec8b

Please sign in to comment.