Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement renderable components & fix player component #1105

Merged
merged 2 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ default MatchPlayer getPlayer(@Nullable UUID playerId) {
return playerId == null ? null : getPlayer(Bukkit.getPlayer(playerId));
}

@Nullable
default MatchPlayerState getPlayerState(@Nullable UUID playerId) {
if (playerId == null) return null;
MatchPlayer matchPlayer = getPlayer(playerId);
return matchPlayer == null ? null : matchPlayer.getState();
}

@Nullable
default MatchPlayerState getPlayerState(@Nullable Player player) {
if (player == null) return null;
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/player/MatchPlayerState.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Location;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.party.Party;
import tc.oc.pgm.util.Audience;
Expand All @@ -18,29 +20,43 @@ public interface MatchPlayerState extends Audience, Named {
*
* @return The {@link Match}.
*/
@NotNull
Match getMatch();

/**
* Get the {@link Party} of the {@link MatchPlayerState}.
*
* @return The {@link Party}.
*/
@NotNull
Party getParty();

/**
* Get the unique identifier for the {@link MatchPlayerState}.
*
* @return The unique identifier.
*/
@NotNull
UUID getId();

/**
* Get the {@link Location} of the {@link tc.oc.pgm.api.player.MatchPlayer} at the snapshot time.
*
* @return The last known {@link Location}.
*/
@NotNull
Location getLocation();

/** @return if the player is currently dead */
boolean isDead();

/** @return if the player is vanished */
boolean isVanished();

/** @return the players' current nick */
@Nullable
String getNick();

/**
* Get the current {@link tc.oc.pgm.api.player.MatchPlayer} if they are online and their {@link
* Party} is the same.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package tc.oc.pgm.api.player;

import org.jetbrains.annotations.NotNull;
import tc.oc.pgm.api.party.Competitor;

/** A {@link MatchPlayerState} that exclusively represents a {@link Competitor}. */
public interface ParticipantState extends MatchPlayerState {

@Override
@NotNull
Competitor getParty();
}
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ private Component formatAltAccountList(MatchPlayer target, List<MatchPlayer> alt
Component names =
join(
JoinConfiguration.separator(text(", ", NamedTextColor.GRAY)),
alts.stream().map(mp -> mp.getName(NameStyle.CONCISE)).collect(Collectors.toList()));
alts.stream().map(mp -> mp.getName(NameStyle.FANCY)).collect(Collectors.toList()));
Component size = text(alts.size(), NamedTextColor.YELLOW);

return text()
Expand Down Expand Up @@ -738,7 +738,7 @@ public static Component formatPunishmentReason(String reason) {
* Formatting of Kick Screens (KICK/BAN/TEMPBAN)
*/
public static String formatPunishmentScreen(
PunishmentType type, Component punisher, String reason, Duration expires) {
Player viewer, PunishmentType type, Component punisher, String reason, Duration expires) {
List<Component> lines = Lists.newArrayList();

Component header =
Expand Down Expand Up @@ -781,8 +781,7 @@ public static String formatPunishmentScreen(
lines.add(empty());
lines.add(footer); // Footer line - END

return TextTranslations.translateLegacy(
join(JoinConfiguration.newlines(), lines), null); // TODO add viewer
return TextTranslations.translateLegacy(join(JoinConfiguration.newlines(), lines), viewer);
}

/*
Expand Down Expand Up @@ -824,7 +823,7 @@ private void broadcastPunishment(PlayerPunishmentEvent event) {
event.getType(),
event.getPlayer().getMatch(),
event.getSender(),
event.getPlayer().getName(NameStyle.CONCISE),
event.getPlayer().getName(NameStyle.FANCY),
event.getReason(),
event.isSilent(),
event.getDuration());
Expand Down Expand Up @@ -922,7 +921,7 @@ private void banPlayer(String target, String reason, Component source, Instant e
target,
reason,
expires != null ? Date.from(expires) : null,
TextTranslations.translateLegacy(source, null));
TextTranslations.translateLegacy(source));
}

// On login of accounts whose IP match a recently banned player, alert staff.
Expand Down Expand Up @@ -970,7 +969,7 @@ public String getPunishmentScreenFromName(Player viewer, String name) {
: null;

return formatPunishmentScreen(
type, text(ban.getSource(), NamedTextColor.AQUA), ban.getReason(), length);
viewer, type, text(ban.getSource(), NamedTextColor.AQUA), ban.getReason(), length);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private Component createInteractiveBroadcast(
String.format("moderation.freeze.broadcast.%s", frozen ? "frozen" : "thaw"),
NamedTextColor.GRAY,
senderName,
freezee.getName(NameStyle.CONCISE)))
freezee.getName(NameStyle.FANCY)))
.hoverEvent(
showText(translatable("moderation.freeze.broadcast.hover", NamedTextColor.GRAY)))
.clickEvent(runCommand("/f " + freezee.getBukkit().getName()))
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/tc/oc/pgm/db/UsernameImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tc.oc.pgm.db;

import static tc.oc.pgm.util.Assert.assertNotNull;
import static tc.oc.pgm.util.text.PlayerComponent.player;
import static tc.oc.pgm.util.player.PlayerComponent.player;

import java.util.UUID;
import net.kyori.adventure.text.Component;
Expand All @@ -10,7 +10,6 @@
import tc.oc.pgm.api.player.Username;
import tc.oc.pgm.util.UsernameResolver;
import tc.oc.pgm.util.named.NameStyle;
import tc.oc.pgm.util.text.PlayerComponent;

class UsernameImpl implements Username {

Expand All @@ -35,7 +34,7 @@ public String getNameLegacy() {

@Override
public Component getName(NameStyle style) {
return name == null ? PlayerComponent.UNKNOWN : player(Bukkit.getPlayer(id), name, style);
return player(Bukkit.getPlayer(id), name, style);
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/tc/oc/pgm/destroyable/Destroyable.java
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,7 @@ public boolean isCompleted(Competitor team) {
return this.contributions;
}

Map<MatchPlayerState, Integer> playerDamage =
new DefaultMapAdapter<>(new HashMap<MatchPlayerState, Integer>(), 0);
Map<MatchPlayerState, Integer> playerDamage = new DefaultMapAdapter<>(new HashMap<>(), 0);

int totalDamage = 0;
for (DestroyableHealthChange change : this.events) {
Expand Down
11 changes: 5 additions & 6 deletions core/src/main/java/tc/oc/pgm/ffa/Tribute.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package tc.oc.pgm.ffa;

import static net.kyori.adventure.text.Component.*;
import static tc.oc.pgm.util.Assert.assertNotNull;
import static tc.oc.pgm.util.text.PlayerComponent.player;
import static tc.oc.pgm.util.player.PlayerComponent.player;

import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -64,7 +63,7 @@ public Tribute(final MatchPlayer player, final @Nullable ChatColor color) {
this.chatColor = color == null ? ChatColor.YELLOW : color;
this.color = BukkitUtils.colorOf(this.chatColor);
this.dyeColor = BukkitUtils.chatColorToDyeColor(this.chatColor);
this.textColor = TextFormatter.convert(color);
this.textColor = TextFormatter.convert(chatColor);
this.query = new PartyQuery(null, this);
}

Expand Down Expand Up @@ -110,7 +109,7 @@ public TextColor getTextColor() {

@Override
public Component getName(final NameStyle style) {
return player(player != null ? player.getBukkit() : null, style);
return player(player, style);
}

@Override
Expand All @@ -120,7 +119,7 @@ public String getNameLegacy() {

@Override
public Component getChatPrefix() {
return empty();
return Component.empty();
}

@Override
Expand Down Expand Up @@ -158,7 +157,7 @@ private void checkPlayer(final UUID playerId) {
public void addPlayer(final MatchPlayer player) {
checkPlayer(assertNotNull(player).getId());
this.player = player;
this.players = Collections.unmodifiableList(Collections.singletonList(player));
this.players = Collections.singletonList(player);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/tc/oc/pgm/flag/Flag.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public Component getComponentName() {
}

public String getColoredName() {
return TextTranslations.translateLegacy(getComponentName(), null);
return TextTranslations.translateLegacy(getComponentName());
}

public ImmutableSet<NetDefinition> getNets() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tc.oc.pgm.inventory;

import static tc.oc.pgm.util.text.PlayerComponent.player;
import static tc.oc.pgm.util.player.PlayerComponent.player;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
Expand Down Expand Up @@ -298,9 +298,7 @@ protected void previewPlayerInventory(Player viewer, PlayerInventory inventory)
// restrictions on inventory titles
String title =
StringUtils.substring(
TextTranslations.translateLegacy(player(holder, NameStyle.CONCISE, viewer), viewer),
0,
32);
TextTranslations.translateLegacy(player(holder, NameStyle.FANCY), viewer), 0, 32);

Inventory preview = Bukkit.getServer().createInventory(viewer, 45, title);

Expand Down
26 changes: 9 additions & 17 deletions core/src/main/java/tc/oc/pgm/listeners/AntiGriefListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import static net.kyori.adventure.key.Key.key;
import static net.kyori.adventure.sound.Sound.sound;
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.Component.translatable;
import static tc.oc.pgm.util.text.PlayerComponent.player;
import static tc.oc.pgm.util.player.PlayerComponent.player;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.ChatColor;
import org.bukkit.Location;
Expand Down Expand Up @@ -94,41 +94,33 @@ private void participantDefuse(Player player, Entity entity) {
this.notifyDefuse(
clicker,
entity,
ChatColor.RED
+ TextTranslations.translate(
"moderation.defuse.player",
clicker.getBukkit(),
player(owner.getBukkit(), NameStyle.CONCISE, clicker.getBukkit())
.color(NamedTextColor.RED)));
translatable("moderation.defuse.player", NamedTextColor.RED, owner.getName()));

ChatDispatcher.broadcastAdminChatMessage(
translatable(
"moderation.defuse.alert.player",
NamedTextColor.GRAY,
clicker.getName(NameStyle.FANCY),
owner.getName(NameStyle.FANCY),
clicker.getName(),
owner.getName(),
MinecraftComponent.entity(entity.getType()).color(NamedTextColor.DARK_RED)),
clicker.getMatch());
} else {
this.notifyDefuse(
clicker,
entity,
ChatColor.RED
+ TextTranslations.translate("moderation.defuse.world", clicker.getBukkit()));
clicker, entity, translatable("moderation.defuse.world", NamedTextColor.RED));

ChatDispatcher.broadcastAdminChatMessage(
translatable(
"moderation.defuse.alert.world",
NamedTextColor.GRAY,
clicker.getName(NameStyle.FANCY),
clicker.getName(),
MinecraftComponent.entity(entity.getType()).color(NamedTextColor.DARK_RED)),
clicker.getMatch());
}
}
}

private void notifyDefuse(MatchPlayer clicker, Entity entity, String message) {
clicker.sendMessage(text(message));
private void notifyDefuse(MatchPlayer clicker, Entity entity, Component message) {
clicker.sendMessage(message);
clicker
.getMatch()
.playSound(
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/tc/oc/pgm/listeners/ChatDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static ChatDispatcher get() {
private static final String GLOBAL_FORMAT = "<%s>: %s";
private static final String PREFIX_FORMAT = "%s: %s";
private static final String AC_FORMAT =
TextTranslations.translateLegacy(ADMIN_CHAT_PREFIX, null) + PREFIX_FORMAT;
TextTranslations.translateLegacy(ADMIN_CHAT_PREFIX) + PREFIX_FORMAT;

private static final Predicate<MatchPlayer> AC_FILTER =
viewer -> viewer.getBukkit().hasPermission(Permissions.ADMINCHAT);
Expand Down Expand Up @@ -162,7 +162,7 @@ public void sendTeam(
match,
sender,
message,
TextTranslations.translateLegacy(party.getChatPrefix(), null) + PREFIX_FORMAT,
TextTranslations.translateLegacy(party.getChatPrefix()) + PREFIX_FORMAT,
getChatFormat(party.getChatPrefix(), sender, message),
match.getPlayers(),
viewer ->
Expand Down Expand Up @@ -222,7 +222,7 @@ public void sendDirect(
throw exception("command.message.blocked", receiver.getName(NameStyle.FANCY));

if (isMuted(receiver))
throw exception("moderation.mute.target", receiver.getName(NameStyle.CONCISE));
throw exception("moderation.mute.target", receiver.getName(NameStyle.FANCY));
}

lastMessagedBy.put(receiver.getBukkit(), sender.getId());
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/tc/oc/pgm/listeners/PGMListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void onPrePlayerLogin(final AsyncPlayerPreLoginEvent event) {
if (!mm.getMatches().hasNext()) {
event.disallow(
AsyncPlayerPreLoginEvent.Result.KICK_OTHER,
TextTranslations.translate("misc.incorrectWorld", null));
TextTranslations.translate("misc.incorrectWorld"));
}
}

Expand Down Expand Up @@ -209,7 +209,7 @@ public static void announceJoinOrLeave(
SettingValue option = viewer.getSettings().getValue(SettingKey.JOIN);
if (option.equals(SettingValue.JOIN_ON)) {
Component component =
translatable(key, NamedTextColor.YELLOW, player.getName(NameStyle.CONCISE));
translatable(key, NamedTextColor.YELLOW, player.getName(NameStyle.FANCY));
viewer.sendMessage(
staffOnly
? ChatDispatcher.ADMIN_CHAT_PREFIX.append(component.color(NamedTextColor.YELLOW))
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/tc/oc/pgm/match/MatchPlayerImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tc.oc.pgm.match;

import static tc.oc.pgm.util.Assert.assertNotNull;
import static tc.oc.pgm.util.text.PlayerComponent.player;
import static tc.oc.pgm.util.player.PlayerComponent.player;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
Expand Down Expand Up @@ -413,7 +413,7 @@ public World getWorld() {

@Override
public Component getName(NameStyle style) {
return player(getBukkit(), style);
return player(this, style);
}

@Override
Expand Down
Loading