Skip to content

Commit

Permalink
Added Minimessage to signs
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusTieger committed Apr 30, 2024
1 parent 4c2b965 commit 42f0afa
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 61 deletions.
1 change: 1 addition & 0 deletions ext/adventure-helper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
"compileOnly"(libs.adventureApi)
"compileOnly"(libs.adventureSerializerLegacy)
"compileOnly"(libs.bungeecordChat)
"api"(libs.adventureMinimessage)
}

configurations {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019-2024 CloudNetService team & contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eu.cloudnetservice.ext.component;

import java.util.Map;
import lombok.NonNull;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;

public class MinimessageUtils {

public static TagResolver[] tagsFromMap(@NonNull Map<String, String> placeholders) {
return placeholders.entrySet()
.stream()
.map((entry) -> Placeholder.unparsed(entry.getKey(), entry.getValue()))
.toArray((size) -> new TagResolver[size]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ public BridgeServiceHelper(
* @param service the service to use as source for the placeholder values.
* @return the String with the placeholders replaced.
* @throws NullPointerException if the given input string is null.
* @deprecated Use {@link #fillCommonPlaceholders(Map, String, ServiceInfoSnapshot)} instead
*/
@Deprecated
public static @NonNull String fillCommonPlaceholders(
@NonNull String value,
@Nullable String group,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,48 @@ public record SignConfigurationEntry(
.groupConfigurations(List.of(SignGroupConfiguration.builder()
.targetGroup("TARGET_GROUP")
.emptyLayout(SignLayoutsHolder.singleLayout(SignLayout.builder()
.lines("&7%task% &0- &7%task_id%", "&8[&7EMPTY&8]", "%online_players% / %max_players%", "%motd%")
.lines("<gray><task></gray> <black>-</black> <gray><task_id></gray>",
"<dark_gray>[</dark_gray><gray>EMPTY</gray><dark_gray>]</dark_gray>",
"<online_players> / <max_players>",
"<motd>")
.blockMaterial(onlineBlockType)
.build()))
.onlineLayout(SignLayoutsHolder.singleLayout(SignLayout.builder()
.lines("&6%task% &0- &6%task_id%", "&8[&6LOBBY&8]", "%online_players% / %max_players%", "%motd%")
.lines("<gold><task></gold> <black>-</black> <gold><task_id></gold>",
"<dark_gray>[</dark_gray><gold>LOBBY</gray><gold>]</dark_gray>",
"<online_players> / <max_players>",
"<motd>")
.blockMaterial(onlineBlockType)
.build()))
.fullLayout(SignLayoutsHolder.singleLayout(SignLayout.builder()
.lines("&5%task% &0- &5%task_id%", "&8[&5FULL&8]", "%online_players% / %max_players%", "%motd%")
.lines("<dark_purple><task></dark_purple> <black>-</black> <dark_purple><task_id></dark_purple>",
"<dark_gray>[</dark_gray><dark_purple>FULL</gray><dark_purple>]</dark_gray>",
"<online_players> / <max_players>",
"<motd>")
.blockMaterial(fullBlockType)
.build()))
.build()))
.searchingLayout(defaultLayout("Waiting", searchingBlock))
.startingLayout(defaultLayout("Starting", startingBlock))
.emptyLayout(SignLayoutsHolder.singleLayout(SignLayout.builder()
.lines("&7%task% &0- &7%task_id%", "&8[&7EMPTY&8]", "%online_players% / %max_players%", "%motd%")
.lines("<gray><task></gray> <black>-</black> <gray><task_id></gray>",
"<dark_gray>[</dark_gray><gray>EMPTY</gray><dark_gray>]</dark_gray>",
"<online_players> / <max_players>",
"<motd>")
.blockMaterial(onlineBlockType)
.build()))
.onlineLayout(SignLayoutsHolder.singleLayout(SignLayout.builder()
.lines("&6%task% &0- &6%task_id%", "&8[&6LOBBY&8]", "%online_players% / %max_players%", "%motd%")
.lines("<gold><task></gold> <black>-</black> <gold><task_id></gold>",
"<dark_gray>[</dark_gray><gold>LOBBY</gold><dark_gray>]</dark_gray>",
"<online_players> / <max_players>",
"<motd>")
.blockMaterial(onlineBlockType)
.build()))
.fullLayout(SignLayoutsHolder.singleLayout(SignLayout.builder()
.lines("&5%task% &0- &5%task_id%", "&8[&5FULL&8]", "%online_players% / %max_players%", "%motd%")
.lines("<dark_purple><task></dark_purple> <black>-</black> <dark_purple><task_id></dark_purple>",
"<dark_gray>[</dark_gray><dark_purple>FULL</dark_purple><dark_gray>]</dark_gray>",
"<online_players> / <max_players>",
"<motd>")
.blockMaterial(fullBlockType)
.build()))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,20 @@
import java.util.function.BiConsumer;
import java.util.function.Function;
import lombok.NonNull;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.Nullable;

public abstract class PlatformSign<P, C> implements Comparable<PlatformSign<P, C>> {

protected final Sign base;
protected final PlayerManager playerManager;
protected final Function<String, C> lineMapper;
protected final Function<Component, C> lineMapper;
protected volatile ServiceInfoSnapshot target;

public PlatformSign(
@NonNull Sign base,
@NonNull PlayerManager playerManager,
@NonNull Function<String, C> lineMapper
@NonNull Function<Component, C> lineMapper
) {
this.base = base;
this.playerManager = playerManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import eu.cloudnetservice.modules.signs.platform.PlatformSign;
import eu.cloudnetservice.modules.signs.platform.bukkit.event.BukkitCloudSignInteractEvent;
import lombok.NonNull;
import org.bukkit.ChatColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
Expand All @@ -46,7 +46,7 @@ public BukkitPlatformSign(
@NonNull PluginManager pluginManager,
@NonNull PlayerManager playerManager
) {
super(base, playerManager, input -> ChatColor.translateAlternateColorCodes('&', input));
super(base, playerManager, input -> LegacyComponentSerializer.legacySection().serialize(input));
this.server = server;

this.pluginManager = pluginManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import eu.cloudnetservice.common.tuple.Tuple2;
import eu.cloudnetservice.driver.service.ServiceInfoSnapshot;
import eu.cloudnetservice.ext.adventure.AdventureTextFormatLookup;
import eu.cloudnetservice.ext.component.ComponentFormats;
import eu.cloudnetservice.modules.bridge.player.PlayerManager;
import eu.cloudnetservice.modules.signs.Sign;
import eu.cloudnetservice.modules.signs.configuration.SignLayout;
Expand Down Expand Up @@ -51,8 +50,7 @@ public MinestomPlatformSign(
@NonNull InstanceManager instanceManager
) {
super(base, playerManager, input -> {
var coloredComponent = ComponentFormats.BUNGEE_TO_ADVENTURE.convert(input);
return GsonComponentSerializer.gson().serialize(coloredComponent);
return GsonComponentSerializer.gson().serialize(input);
});

this.eventHandler = eventHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import cn.nukkit.math.BlockFace;
import cn.nukkit.plugin.PluginManager;
import cn.nukkit.utils.Faceable;
import cn.nukkit.utils.TextFormat;
import com.google.common.primitives.Ints;
import eu.cloudnetservice.driver.service.ServiceInfoSnapshot;
import eu.cloudnetservice.modules.bridge.player.PlayerManager;
Expand All @@ -35,6 +34,7 @@
import eu.cloudnetservice.modules.signs.platform.nukkit.event.NukkitCloudSignInteractEvent;
import java.util.Arrays;
import lombok.NonNull;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.jetbrains.annotations.Nullable;

public class NukkitPlatformSign extends PlatformSign<Player, String> {
Expand All @@ -52,7 +52,7 @@ public NukkitPlatformSign(
@NonNull PluginManager pluginManager,
@NonNull PlayerManager playerManager
) {
super(base, playerManager, input -> TextFormat.colorize('&', input));
super(base, playerManager, input -> LegacyComponentSerializer.legacySection().serialize(input));

this.server = server;
this.pluginManager = pluginManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package eu.cloudnetservice.modules.signs.platform.sponge;

import eu.cloudnetservice.driver.service.ServiceInfoSnapshot;
import eu.cloudnetservice.ext.component.ComponentFormats;
import eu.cloudnetservice.modules.bridge.player.PlayerManager;
import eu.cloudnetservice.modules.signs.Sign;
import eu.cloudnetservice.modules.signs.configuration.SignLayout;
Expand Down Expand Up @@ -54,7 +53,7 @@ public SpongePlatformSign(
@NonNull WorldManager worldManager,
@NonNull PlayerManager playerManager
) {
super(base, playerManager, ComponentFormats.BUNGEE_TO_ADVENTURE::convert);
super(base, playerManager, input -> input);

this.game = game;
this.eventManager = eventManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
import static eu.cloudnetservice.modules.bridge.BridgeServiceHelper.fillCommonPlaceholders;

import eu.cloudnetservice.driver.service.ServiceInfoSnapshot;
import eu.cloudnetservice.ext.component.MinimessageUtils;
import eu.cloudnetservice.modules.bridge.BridgeServiceHelper;
import eu.cloudnetservice.modules.signs.Sign;
import eu.cloudnetservice.modules.signs.configuration.SignConfigurationEntry;
import eu.cloudnetservice.modules.signs.configuration.SignGroupConfiguration;
import eu.cloudnetservice.modules.signs.configuration.SignLayout;
import eu.cloudnetservice.modules.signs.configuration.SignLayoutsHolder;
import java.util.HashMap;
import java.util.function.BiConsumer;
import java.util.function.Function;
import lombok.NonNull;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.jetbrains.annotations.Nullable;

/**
Expand Down Expand Up @@ -107,12 +111,14 @@ public static <C> void updateSignLines(
@NonNull SignLayout layout,
@NonNull String signTargetGroup,
@Nullable ServiceInfoSnapshot target,
@NonNull Function<String, C> lineMapper,
@NonNull Function<Component, C> lineMapper,
@NonNull BiConsumer<Integer, C> lineSetter
) {
var placeholders = new HashMap<String, String>();
fillCommonPlaceholders(placeholders, signTargetGroup, target);
var lines = layout.lines();
for (var i = 0; i < Math.min(4, lines.size()); i++) {
var converted = lineMapper.apply(fillCommonPlaceholders(lines.get(i), signTargetGroup, target));
var converted = lineMapper.apply(MiniMessage.miniMessage().deserialize(lines.get(i), MinimessageUtils.tagsFromMap(placeholders)));
lineSetter.accept(i, converted);
}
}
Expand Down
1 change: 0 additions & 1 deletion modules/syncproxy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ dependencies {
"annotationProcessor"(libs.aerogelAuto)

"implementation"(projects.ext.adventureHelper)
"api"(libs.adventureMinimessage)
}

moduleJson {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import eu.cloudnetservice.driver.channel.ChannelMessage;
import eu.cloudnetservice.driver.network.buffer.DataBuf;
import eu.cloudnetservice.driver.service.ServiceInfoSnapshot;
import eu.cloudnetservice.ext.component.MinimessageUtils;
import eu.cloudnetservice.modules.bridge.BridgeServiceHelper;
import eu.cloudnetservice.modules.syncproxy.SyncProxyConstants;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -31,7 +31,6 @@
import lombok.NonNull;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;
Expand All @@ -50,25 +49,6 @@ public record SyncProxyConfiguration(
"service-start", "<gray>The service <yellow><service></yellow> is <green>starting</green> on node <yellow><node></yellow>...</gray>",
"service-stop", "<gray>The service <yellow><service></yellow> is <red>stopping</red> on node <yellow><node></yellow>...</gray>");

public static @Nullable String fillCommonPlaceholders(
@NonNull ServiceInfoSnapshot serviceInfoSnapshot,
@Nullable String input,
int onlinePlayers,
int maxPlayers
) {
if (input == null) {
return null;
}

var placeholders = new HashMap<String, String>();
fillCommonPlaceholders(placeholders, serviceInfoSnapshot, onlinePlayers, maxPlayers);
for (var placeholder : placeholders.entrySet()) {
input = input.replace("%" + placeholder.getKey() + "%", placeholder.getValue());
}

return input;
}

public static void fillCommonPlaceholders(
@NonNull Map<String, String> map,
@NonNull ServiceInfoSnapshot serviceInfoSnapshot,
Expand All @@ -85,13 +65,9 @@ public static TagResolver[] adventurePlaceholders(
int onlinePlayers,
int maxPlayers
) {
var resolvers = new ArrayList<TagResolver>();
var placeholders = new HashMap<String, String>();
fillCommonPlaceholders(placeholders, serviceInfoSnapshot, onlinePlayers, maxPlayers);
for (var placeholder : placeholders.entrySet()) {
resolvers.add(Placeholder.unparsed(placeholder.getKey(), placeholder.getValue()));
}
return resolvers.toArray(new TagResolver[0]);
return MinimessageUtils.tagsFromMap(placeholders);
}

public static @NonNull Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ public boolean checkPlayerMaintenance(@NonNull P player) {

public abstract @NonNull UUID playerUniqueId(@NonNull P player);

public abstract void playerTabList(@NonNull P player, @NonNull Map<String, String> placeholders, @Nullable String header, @Nullable String footer);
public abstract void playerTabList(@NonNull P player, @NonNull Map<String, String> placeholders,
@Nullable String header, @Nullable String footer);

public abstract void disconnectPlayer(@NonNull P player, @NonNull Component message);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Objects;
import java.util.UUID;
import lombok.NonNull;
Expand Down Expand Up @@ -85,6 +86,8 @@ public void handleProxyPing(@NonNull ProxyPingEvent event) {
var protocolText = motd.protocolText();
// check if there is a protocol text in the config
if (protocolText != null) {
var placeholders = new HashMap<String, String>();
SyncProxyConfiguration.fillCommonPlaceholders(placeholders, serviceInfo, onlinePlayers, maxPlayers);
response.setVersion(new Protocol(
LegacyComponentSerializer.legacySection().serialize(
MiniMessage.miniMessage().deserialize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public void registerService(@NonNull ServiceRegistry registry) {
}

@Override
public void playerTabList(@NonNull ProxiedPlayer player, @NonNull Map<String, String> placeholders, @Nullable String header, @Nullable String footer) {
public void playerTabList(@NonNull ProxiedPlayer player, @NonNull Map<String, String> placeholders,
@Nullable String header, @Nullable String footer) {
placeholders.put("ping", String.valueOf(player.getPing()));
placeholders.put("server", player.getServer() == null ? "UNAVAILABLE" : player.getServer().getInfo().getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import eu.cloudnetservice.driver.permission.PermissionManagement;
import eu.cloudnetservice.driver.provider.CloudServiceProvider;
import eu.cloudnetservice.driver.registry.ServiceRegistry;
import eu.cloudnetservice.ext.component.ComponentFormats;
import eu.cloudnetservice.ext.component.MinimessageUtils;
import eu.cloudnetservice.modules.syncproxy.platform.PlatformSyncProxyManagement;
import eu.cloudnetservice.wrapper.configuration.WrapperConfiguration;
import eu.cloudnetservice.wrapper.holder.ServiceInfoHolder;
Expand All @@ -38,8 +38,6 @@
import lombok.NonNull;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.jetbrains.annotations.Nullable;

@Singleton
Expand Down Expand Up @@ -94,7 +92,8 @@ public void registerService(@NonNull ServiceRegistry registry) {
}

@Override
public void playerTabList(@NonNull Player player, @NonNull Map<String, String> placeholders, @Nullable String header, @Nullable String footer) {
public void playerTabList(@NonNull Player player, @NonNull Map<String, String> placeholders,
@Nullable String header, @Nullable String footer) {
if (header == null || footer == null) {
player.getTabList().clearHeaderAndFooter();
} else {
Expand All @@ -106,17 +105,11 @@ public void playerTabList(@NonNull Player player, @NonNull Map<String, String> p
player.sendPlayerListHeaderAndFooter(
MiniMessage.miniMessage().deserialize(
header,
placeholders.entrySet()
.stream()
.map((entry) -> Placeholder.unparsed(entry.getKey(), entry.getValue()))
.toArray((size) -> new TagResolver[size])
MinimessageUtils.tagsFromMap(placeholders)
),
MiniMessage.miniMessage().deserialize(
footer,
placeholders.entrySet()
.stream()
.map((entry) -> Placeholder.unparsed(entry.getKey(), entry.getValue()))
.toArray((size) -> new TagResolver[size])
MinimessageUtils.tagsFromMap(placeholders)
));
}
}
Expand Down
Loading

0 comments on commit 42f0afa

Please sign in to comment.