Skip to content

Commit 6b36e56

Browse files
committed
refactor: cleanup formatters
1 parent 90908cd commit 6b36e56

File tree

11 files changed

+37
-153
lines changed

11 files changed

+37
-153
lines changed

platform/src/main/java/net/silthus/schat/platform/config/adapter/ConfigurateAdapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
import net.silthus.schat.platform.config.serializers.ChannelSerializer;
3535
import net.silthus.schat.platform.config.serializers.ColorSerializer;
3636
import net.silthus.schat.platform.config.serializers.MiniMessageComponentSerializer;
37-
import net.silthus.schat.platform.config.serializers.PointeredFormatSerializer;
37+
import net.silthus.schat.platform.config.serializers.MiniMessageFormatSerializer;
3838
import net.silthus.schat.platform.config.serializers.SettingsSerializer;
3939
import net.silthus.schat.platform.config.serializers.TextDecorationSerializer;
4040
import net.silthus.schat.pointer.Settings;
41-
import net.silthus.schat.ui.format.PointeredFormat;
41+
import net.silthus.schat.ui.format.Format;
4242
import org.spongepowered.configurate.ConfigurateException;
4343
import org.spongepowered.configurate.ConfigurationNode;
4444
import org.spongepowered.configurate.ConfigurationOptions;
@@ -54,7 +54,7 @@ public abstract class ConfigurateAdapter<T extends AbstractConfigurationLoader.B
5454
.register(ChannelConfig.class, new ChannelSerializer())
5555
.register(TextColor.class, new ColorSerializer())
5656
.register(TextDecoration.class, new TextDecorationSerializer())
57-
.register(PointeredFormat.class, new PointeredFormatSerializer())
57+
.register(Format.class, new MiniMessageFormatSerializer())
5858
.build();
5959

6060
private static final Function<ConfigurationOptions, ConfigurationOptions> DEFAULT_OPTIONS = options ->

platform/src/main/java/net/silthus/schat/platform/config/serializers/PointeredFormatSerializer.java renamed to platform/src/main/java/net/silthus/schat/platform/config/serializers/MiniMessageFormatSerializer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@
2525
package net.silthus.schat.platform.config.serializers;
2626

2727
import java.lang.reflect.Type;
28+
import net.silthus.schat.ui.format.Format;
2829
import net.silthus.schat.ui.format.MiniMessageFormat;
29-
import net.silthus.schat.ui.format.PointeredFormat;
3030
import org.checkerframework.checker.nullness.qual.Nullable;
3131
import org.spongepowered.configurate.ConfigurationNode;
3232
import org.spongepowered.configurate.serialize.SerializationException;
3333
import org.spongepowered.configurate.serialize.TypeSerializer;
3434

35-
public class PointeredFormatSerializer implements TypeSerializer<PointeredFormat> {
35+
public class MiniMessageFormatSerializer implements TypeSerializer<Format> {
3636
@Override
37-
public void serialize(Type type, @Nullable PointeredFormat obj, ConfigurationNode node) throws SerializationException {
37+
public void serialize(Type type, @Nullable Format obj, ConfigurationNode node) throws SerializationException {
3838
if (obj instanceof MiniMessageFormat format) {
3939
node.set(format.format());
4040
}
4141
}
4242

4343
@Override
44-
public PointeredFormat deserialize(Type type, ConfigurationNode node) throws SerializationException {
44+
public Format deserialize(Type type, ConfigurationNode node) throws SerializationException {
4545
return new MiniMessageFormat(node.getString());
4646
}
4747
}

platform/src/test/java/net/silthus/schat/platform/config/ConfigTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ void loads_defined_channel_settings() {
7878

7979
@Test
8080
void loads_message_format() {
81-
final Component format = getTestChannelConfig().settings().get(CHANNEL_FORMAT).get(MESSAGE_FORMAT).format(View.empty(), message("Hey").source(Identity.identity("Notch")).create());
81+
final Component format = getTestChannelConfig().settings()
82+
.get(CHANNEL_FORMAT)
83+
.get(MESSAGE_FORMAT)
84+
.format(View.empty(), message("Hey").source(Identity.identity("Notch")).create());
8285
assertThat(format).isEqualTo(text("Notch", YELLOW).append(text(": Hey", GRAY)));
8386
}
8487

ui/src/main/java/net/silthus/schat/ui/format/ChannelFormat.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

ui/src/main/java/net/silthus/schat/ui/format/ChannelFormatter.java

Lines changed: 0 additions & 79 deletions
This file was deleted.

ui/src/main/java/net/silthus/schat/ui/format/Format.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424

2525
package net.silthus.schat.ui.format;
2626

27+
import net.kyori.adventure.text.Component;
28+
import net.silthus.schat.pointer.Pointered;
2729
import net.silthus.schat.ui.view.View;
2830

2931
@FunctionalInterface
30-
public interface Format<T> {
31-
net.kyori.adventure.text.Component format(View view, T type);
32+
public interface Format {
33+
Component format(View view, Pointered entity);
3234
}

ui/src/main/java/net/silthus/schat/ui/format/MessageFormat.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

ui/src/main/java/net/silthus/schat/ui/format/MiniMessageFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
@Getter
4040
@Accessors(fluent = true)
41-
public class MiniMessageFormat<T extends Pointered> implements PointeredFormat<T> {
41+
public class MiniMessageFormat implements Format {
4242
private final MiniMessage formatter = MiniMessage.miniMessage();
4343
private final String format;
4444

ui/src/main/java/net/silthus/schat/ui/format/PointeredFormat.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

ui/src/main/java/net/silthus/schat/ui/views/TabbedChannelsView.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@
4141
import net.silthus.schat.message.Message;
4242
import net.silthus.schat.pointer.Setting;
4343
import net.silthus.schat.pointer.Settings;
44-
import net.silthus.schat.ui.format.ChannelFormat;
4544
import net.silthus.schat.ui.format.Format;
46-
import net.silthus.schat.ui.format.MessageFormat;
4745
import net.silthus.schat.ui.model.ChatterViewModel;
4846
import net.silthus.schat.ui.view.View;
4947
import org.jetbrains.annotations.NotNull;
@@ -88,40 +86,40 @@ public final class TabbedChannelsView implements View {
8886
.suffix(text(" |"))
8987
.build());
9088

91-
public static final Setting<MessageFormat> MESSAGE_FORMAT = setting(MessageFormat.class, "message", (view, msg) ->
89+
public static final Setting<Format> MESSAGE_FORMAT = setting(Format.class, "message", (view, msg) ->
9290
msg.get(Message.SOURCE)
9391
.filter(Identity.IS_NOT_NIL)
9492
.map(identity -> identity.displayName().append(text(": ")))
9593
.orElse(Component.empty())
9694
.append(msg.getOrDefault(Message.TEXT, Component.empty())));
9795

98-
public static final Setting<ChannelFormat> ACTIVE_CHANNEL_FORMAT = setting(ChannelFormat.class, "active_channel", (view, channel) ->
99-
channel.get(DISPLAY_NAME)
96+
public static final Setting<Format> ACTIVE_CHANNEL_FORMAT = setting(Format.class, "active_channel", (view, channel) ->
97+
channel.getOrDefault(DISPLAY_NAME, Component.empty())
10098
.colorIfAbsent(GREEN)
10199
.decorate(UNDERLINED)
102100
);
103101

104-
public static final Setting<ChannelFormat> INACTIVE_CHANNEL_FORMAT = setting(ChannelFormat.class, "inactive_channel", (view, channel) ->
105-
channel.getOrDefault(DISPLAY_NAME, text(channel.key()))
102+
public static final Setting<Format> INACTIVE_CHANNEL_FORMAT = setting(Format.class, "inactive_channel", (view, channel) ->
103+
channel.getOrDefault(DISPLAY_NAME, Component.empty())
106104
.colorIfAbsent(GRAY)
107105
.hoverEvent(translatable("schat.hover.join-channel")
108-
.args(channel.get(DISPLAY_NAME))
106+
.args(channel.getOrDefault(DISPLAY_NAME, Component.empty()))
109107
.color(GRAY)
110108
).clickEvent(
111-
clickEvent(RUN_COMMAND, "/channel join " + channel.key())
109+
clickEvent(RUN_COMMAND, "/channel join " + channel.getOrDefault(Channel.KEY, "unknown"))
112110
)
113111
);
114112

115113
public static final Settings.Builder DEFAULT_FORMAT_SETTINGS = Settings.settingsBuilder()
116114
.withStatic(MESSAGE_FORMAT, MESSAGE_FORMAT.defaultValue())
117-
.withStatic(ACTIVE_CHANNEL_FORMAT, (view, channel) -> ACTIVE_CHANNEL_DECORATION.apply(channel.displayName()))
118-
.withStatic(INACTIVE_CHANNEL_FORMAT, (view, channel) -> INACTIVE_CHANNEL_DECORATION.apply(channel, channel.displayName()));
115+
.withStatic(ACTIVE_CHANNEL_FORMAT, (view, channel) -> ACTIVE_CHANNEL_DECORATION.apply(channel.getOrDefault(DISPLAY_NAME, Component.empty())))
116+
.withStatic(INACTIVE_CHANNEL_FORMAT, (view, channel) -> INACTIVE_CHANNEL_DECORATION.apply((Channel) channel, channel.getOrDefault(DISPLAY_NAME, Component.empty())));
119117

120118
public static final Setting<Settings> CHANNEL_FORMAT = setting(Settings.class, "format", DEFAULT_FORMAT_SETTINGS.create());
121119
public static final Setting<Settings> PRIVATE_CHANNEL_FORMAT = setting(Settings.class, "private_channel_format", Settings.settingsBuilder()
122-
.withStatic(MESSAGE_FORMAT, (view, message) -> renderPrivateMessage(((TabbedChannelsView) view).chatter(), message))
123-
.withStatic(ACTIVE_CHANNEL_FORMAT, (view, channel) -> ACTIVE_CHANNEL_DECORATION.apply(renderPrivateChannelName(((TabbedChannelsView) view).chatter(), channel)))
124-
.withStatic(INACTIVE_CHANNEL_FORMAT, (view, channel) -> INACTIVE_CHANNEL_DECORATION.apply(channel, renderPrivateChannelName(((TabbedChannelsView) view).chatter(), channel)))
120+
.withStatic(MESSAGE_FORMAT, (view, message) -> renderPrivateMessage(((TabbedChannelsView) view).chatter(), (Message) message))
121+
.withStatic(ACTIVE_CHANNEL_FORMAT, (view, channel) -> ACTIVE_CHANNEL_DECORATION.apply(renderPrivateChannelName(((TabbedChannelsView) view).chatter(), (Channel) channel)))
122+
.withStatic(INACTIVE_CHANNEL_FORMAT, (view, channel) -> INACTIVE_CHANNEL_DECORATION.apply((Channel) channel, renderPrivateChannelName(((TabbedChannelsView) view).chatter(), (Channel) channel)))
125123
.create()
126124
);
127125

@@ -218,11 +216,11 @@ private boolean isMessageDisplayed(Message message) {
218216
public class Tab {
219217
private final Channel channel;
220218

221-
private Format<Message> messageFormat;
222-
private Format<Channel> activeFormat;
223-
private Format<Channel> inactiveFormat;
219+
private Format messageFormat;
220+
private Format activeFormat;
221+
private Format inactiveFormat;
224222

225-
protected Tab(Channel channel, Format<Message> messageFormat, Format<Channel> activeFormat, Format<Channel> inactiveFormat) {
223+
protected Tab(Channel channel, Format messageFormat, Format activeFormat, Format inactiveFormat) {
226224
this.channel = channel;
227225

228226
this.messageFormat = messageFormat;

0 commit comments

Comments
 (0)