Skip to content

Commit

Permalink
playerex:level
Browse files Browse the repository at this point in the history
*Made %playerex:level% also persistent for offline players.
  • Loading branch information
CleverNucleus committed Sep 20, 2021
1 parent 481b2b2 commit 6816d73
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.clevernucleus.playerex.util.NameLevelPair;

import dev.onyxstudios.cca.api.v3.component.Component;
import net.minecraft.server.network.ServerPlayerEntity;

/**
* Interface providing access to the PlayerEx persistent player cache. For any player that has ever joined the server.
Expand All @@ -21,4 +22,11 @@ public interface PersistentPlayerCache extends Component {
* @return Gets the name-level pair for every player.
*/
NameLevelPair[] get();

/**
*
* @param player
* @return The NameLevelPair associated with this player, or an empty one if the player doesn't exist.
*/
NameLevelPair get(final ServerPlayerEntity player);
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ public NameLevelPair[] get() {
return pairs;
}

@Override
public NameLevelPair get(final ServerPlayerEntity player) {
GameProfile profile = player.getGameProfile();
UUID uuid = profile.getId();

if(uuid == null || !this.cache.containsKey(uuid)) return new NameLevelPair("", 0);

return this.cache.get(uuid);
}

@Override
public void readFromNbt(NbtCompound tag) {
if(!tag.contains("Cache")) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import eu.pb4.placeholders.PlaceholderContext;
import eu.pb4.placeholders.PlaceholderHandler;
import eu.pb4.placeholders.PlaceholderResult;
import net.minecraft.entity.attribute.AttributeContainer;
import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;

public final class StoredPlaceholder {
Expand Down Expand Up @@ -53,18 +51,14 @@ private static PlaceholderHandler topName() {

static {
register("level", ctx -> {
PlayerEntity player = ctx.getPlayer();
ServerPlayerEntity player = ctx.getPlayer();

if(player == null) return PlaceholderResult.invalid("Null player!");

AttributeContainer container = player.getAttributes();
EntityAttribute attribute = ExAPI.LEVEL.get();
PersistentPlayerCache cache = ExAPI.persistentPlayerCache(ctx.getServer());
NameLevelPair pair = cache.get(player);

if(attribute == null || !container.hasAttribute(attribute)) return PlaceholderResult.invalid("Player doesn't have a level!");

int level = Math.round((float)container.getValue(attribute));

return PlaceholderResult.value(String.valueOf(level));
return PlaceholderResult.value(pair.level());
});
register("name_top", topName());
register("level_top", topLevel());
Expand Down

0 comments on commit 6816d73

Please sign in to comment.