Skip to content

Commit

Permalink
Add placeholder for team names and colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Dec 27, 2024
1 parent 5f6cb03 commit 1f81cf0
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
import eu.decentsoftware.holograms.api.holograms.HologramPage;
import eu.decentsoftware.holograms.api.holograms.enums.HologramLineType;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.battleplugins.arena.competition.LiveCompetition;
import org.battleplugins.arena.feature.hologram.Hologram;
import org.battleplugins.arena.util.Util;
import org.bukkit.Location;

import java.util.ArrayList;
import java.util.List;

public class DecentHologram implements Hologram {
private static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer.builder()
.hexColors()
.useUnusualXRepeatedCharacterHexFormat()
.build();

private final LiveCompetition<?> competition;
private final eu.decentsoftware.holograms.api.holograms.Hologram impl;

Expand Down Expand Up @@ -47,7 +42,7 @@ public List<Component> getLines() {
List<Component> lines = new ArrayList<>();
for (HologramLine line : page.getLines()) {
if (line.getType() == HologramLineType.TEXT) {
lines.add(SERIALIZER.deserialize(line.getText()));
lines.add(Util.deserializeFromLegacy(line.getText()));
}
}

Expand All @@ -66,7 +61,7 @@ public void setLines(Component... lines) {
}

for (Component line : lines) {
page.addLine(new HologramLine(page, this.getLocation(), SERIALIZER.serialize(line)));
page.addLine(new HologramLine(page, this.getLocation(), Util.serializeToLegacy(line)));
}
}

Expand All @@ -77,7 +72,7 @@ public void addLine(Component line) {
}

HologramPage page = this.impl.getPage(0);
page.addLine(new HologramLine(page, this.getLocation(), SERIALIZER.serialize(line)));
page.addLine(new HologramLine(page, this.getLocation(), Util.serializeToLegacy(line)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package org.battleplugins.arena.module.placeholderapi;

import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.kyori.adventure.text.Component;
import org.battleplugins.arena.Arena;
import org.battleplugins.arena.ArenaPlayer;
import org.battleplugins.arena.BattleArena;
import org.battleplugins.arena.competition.Competition;
import org.battleplugins.arena.competition.LiveCompetition;
import org.battleplugins.arena.competition.map.CompetitionMap;
import org.battleplugins.arena.competition.map.LiveCompetitionMap;
import org.battleplugins.arena.competition.phase.CompetitionPhaseType;
import org.battleplugins.arena.messages.Messages;
import org.battleplugins.arena.resolver.Resolver;
import org.battleplugins.arena.resolver.ResolverKey;
import org.battleplugins.arena.resolver.ResolverKeys;
import org.battleplugins.arena.team.ArenaTeam;
import org.battleplugins.arena.util.Util;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -58,6 +60,37 @@ public BattleArenaExpansion(BattleArena plugin) {
if (resolverKey != null && resolver.has(resolverKey)) {
return resolver.resolveToString(resolverKey);
}

// Additional placeholders for competition
switch (placeholder) {
case "team_color": {
ArenaTeam team = arenaPlayer.getTeam();
if (team != null) {
return team.getTextColor().asHexString();
}
}
case "team_color_legacy": {
ArenaTeam team = arenaPlayer.getTeam();
if (team != null) {
Component teamColor = Component.empty().color(team.getTextColor());
return Util.serializeToLegacy(teamColor);
}
}
case "formatted_team_name": {
ArenaTeam team = arenaPlayer.getTeam();
if (team != null) {
Component teamName = team.getFormattedName();
return Messages.wrap(teamName).asPlainText();
}
}
case "formatted_team_name_legacy": {
ArenaTeam team = arenaPlayer.getTeam();
if (team != null) {
Component teamName = team.getFormattedName();
return Util.serializeToLegacy(teamName);
}
}
}
}

// If player is null or no other placeholder resolvers have made it to this point,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public String asPlainText() {
return PlainTextComponentSerializer.plainText().serialize(this.toComponent());
}

public String asMiniMessage() {
return Messages.MINI_MESSAGE.serialize(this.toComponent());
}

public Component toComponent() {
return this.text;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ public static Message wrap(String defaultText) {
return new Message("unregistered", MINI_MESSAGE.deserialize(defaultText, RESOLVER));
}

public static Message wrap(Component defaultComponent) {
return new Message("unregistered", defaultComponent);
}

public static Message info(String translationKey, String defaultText) {
return message(translationKey, MINI_MESSAGE.deserialize(defaultText, RESOLVER).color(PRIMARY_COLOR));
}
Expand Down
33 changes: 33 additions & 0 deletions plugin/src/main/java/org/battleplugins/arena/util/Util.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.battleplugins.arena.util;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.battleplugins.arena.BattleArena;
import org.battleplugins.arena.config.ArenaOption;
import org.battleplugins.arena.messages.Messages;
import org.jetbrains.annotations.ApiStatus;

import java.io.File;
import java.io.IOException;
Expand All @@ -21,6 +24,36 @@
import java.util.stream.Stream;

public class Util {
private static final LegacyComponentSerializer LEGACY_SERIALIZER = LegacyComponentSerializer.builder()
.hexColors()
.useUnusualXRepeatedCharacterHexFormat()
.build();

/**
* Serializes a component to a legacy string.
* <p>
* This method is internal and should not be used by other plugins.
*
* @param component the component to serialize
* @return the serialized component
*/
@ApiStatus.Internal
public static String serializeToLegacy(Component component) {
return LEGACY_SERIALIZER.serialize(component);
}

/**
* Deserializes a legacy string to a component.
* <p>
* This method is internal and should not be used by other plugins.
*
* @param legacy the legacy string to deserialize
* @return the deserialized component
*/
@ApiStatus.Internal
public static Component deserializeFromLegacy(String legacy) {
return LEGACY_SERIALIZER.deserialize(legacy);
}

public static String toTimeStringShort(Duration duration) {
long seconds = duration.getSeconds();
Expand Down

0 comments on commit 1f81cf0

Please sign in to comment.