Skip to content

Commit

Permalink
Fixes & cache username rendering
Browse files Browse the repository at this point in the history
Signed-off-by: Pablete1234 <[email protected]>
  • Loading branch information
Pablete1234 committed Nov 25, 2022
1 parent 5442ccb commit 32a0075
Show file tree
Hide file tree
Showing 28 changed files with 714 additions and 313 deletions.
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
2 changes: 2 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/player/ParticipantState.java
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();
}
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,14 +1,13 @@
package tc.oc.pgm.db;

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

import java.util.UUID;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.Nullable;
import tc.oc.pgm.api.player.Username;
import tc.oc.pgm.util.PlayerComponent;
import tc.oc.pgm.util.UsernameResolver;
import tc.oc.pgm.util.named.NameStyle;

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.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
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.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
25 changes: 9 additions & 16 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.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,40 +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.FANCY).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
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.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

0 comments on commit 32a0075

Please sign in to comment.