Skip to content

Commit

Permalink
Add command to print player stats
Browse files Browse the repository at this point in the history
  • Loading branch information
KaptainWutax committed Jan 14, 2025
1 parent 8561154 commit f4e68b6
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 8 deletions.
113 changes: 113 additions & 0 deletions common/src/main/java/abeshutt/staracademy/command/StatsCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package abeshutt.staracademy.command;

import abeshutt.staracademy.StarAcademyMod;
import abeshutt.staracademy.init.ModWorldData;
import abeshutt.staracademy.world.data.PlayerProfileData;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.RegistryEntryArgumentType;
import net.minecraft.entity.Entity;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.stat.ServerStatHandler;
import net.minecraft.stat.Stat;
import net.minecraft.stat.StatType;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.Pair;
import net.minecraft.util.WorldSavePath;

import java.io.File;
import java.util.*;

import static net.minecraft.command.argument.RegistryEntryArgumentType.registryEntry;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
import static net.minecraft.util.Formatting.GRAY;

public class StatsCommand extends Command {

@Override
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess access, CommandManager.RegistrationEnvironment environment) {
LiteralArgumentBuilder<ServerCommandSource> node = literal("print");

for(StatType<?> type : Registries.STAT_TYPE) {
Identifier id = Registries.STAT_TYPE.getId(type);
if(id == null) continue;
node.then(literal(id.toString())
.then(argument("stat", registryEntry(access, type.getRegistry().getKey()))
.then(literal("all")
.executes(context -> this.onPrintStats(type, type.getRegistry().getKey(), false, context)))
.then(argument("target", EntityArgumentType.players())
.executes(context -> this.onPrintStats(type, type.getRegistry().getKey(), true, context)))));
}

dispatcher.register(literal(StarAcademyMod.ID)
.then(literal("stats").then(node)));
}

private int onPrintStats(StatType type, RegistryKey key, boolean hasTarget, CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
RegistryEntry.Reference entry = RegistryEntryArgumentType.getRegistryEntry(context, "stat", key);
PlayerProfileData data = ModWorldData.PLAYER_PROFILE.getGlobal(context.getSource().getServer());

Set<UUID> players = new HashSet<>();

if(hasTarget) {
EntityArgumentType.getPlayers(context, "target").stream().map(Entity::getUuid).forEach(players::add);
} else {
players.addAll(data.getProfiles().keySet());
}

List<Pair<UUID, Integer>> values = new ArrayList<>();

for(UUID uuid : players) {
ServerStatHandler stats = getStats(context.getSource().getServer(), uuid);
int value = stats.getStat(type, entry.value());
if(value == 0) continue;
values.add(new Pair<>(uuid, value));
}

values.sort((o1, o2) -> Integer.compare(o2.getRight(), o1.getRight()));

context.getSource().sendFeedback(() -> {
Stat stat = type.getOrCreateStat(entry.value());
String translationKey = "stat." + stat.getValue().toString().replace(':', '.');

return Text.empty().append(type.getName())
.append(Text.literal(" / ").formatted(GRAY))
.append(Text.translatable(translationKey));
}, false);

for(Pair<UUID, Integer> value : values) {
Stat stat = type.getOrCreateStat(entry.value());

context.getSource().sendFeedback(() -> {
return Text.empty().append(Text.literal(" > ").formatted(GRAY))
.append(Text.literal(data.getProfile(value.getLeft()).flatMap(profile -> {
return Optional.ofNullable(profile.getName()).map(String::toString);
}).orElseGet(() -> value.getLeft().toString())))
.append(Text.literal(" | ").formatted(GRAY))
.append(stat.format(value.getRight()));
}, false);
}

return 0;
}

public static ServerStatHandler getStats(MinecraftServer server, UUID uuid) {
ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid);
if(player != null) return player.getStatHandler();
File root = server.getSavePath(WorldSavePath.STATS).toFile();
return new ServerStatHandler(server, new File(root, uuid + ".json"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher, C
register(SafariCommand::new, dispatcher, access, environment);
register(PartnerCommand::new, dispatcher, access, environment);
register(WardrobeCommand::new, dispatcher, access, environment);
register(StatsCommand::new, dispatcher, access, environment);
}

private static <T extends Command> T register(Supplier<T> supplier, CommandDispatcher<ServerCommandSource> dispatcher,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

public class ModWorldData extends ModRegistries {

public static WorldDataType<PlayerProfileData> PLAYER_NAME;
public static WorldDataType<PlayerProfileData> PLAYER_PROFILE;
public static WorldDataType<StarBadgeData> STAR_BADGE;
public static WorldDataType<PokemonStarterData> POKEMON_STARTER;
public static WorldDataType<SafariData> SAFARI;
public static WorldDataType<WardrobeData> WARDROBE;
public static WorldDataType<PartnerData> PARTNER;

public static void register() {
PLAYER_NAME = new WorldDataType<>(StarAcademyMod.ID + ".player_profile", PlayerProfileData::new);
PLAYER_PROFILE = new WorldDataType<>(StarAcademyMod.ID + ".player_profile", PlayerProfileData::new);
STAR_BADGE = new WorldDataType<>(StarAcademyMod.ID + ".star_badge", StarBadgeData::new);
POKEMON_STARTER = new WorldDataType<>(StarAcademyMod.ID + ".pokemon_starter", PokemonStarterData::new);
SAFARI = new WorldDataType<>(StarAcademyMod.ID + ".safari", SafariData::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import abeshutt.staracademy.item.renderer.NullSpecialItemRenderer;
import abeshutt.staracademy.item.renderer.SpecialItemRenderer;
import abeshutt.staracademy.util.ISpecialItemModel;
import abeshutt.staracademy.world.StarOwnership;
import abeshutt.staracademy.world.data.PlayerProfileData;
import abeshutt.staracademy.world.data.SafariData;
import abeshutt.staracademy.world.random.JavaRandom;
import abeshutt.staracademy.world.roll.IntRoll;
import com.mojang.authlib.GameProfile;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.entity.player.PlayerEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ public void readNbt(NbtCompound nbt) {

public static void init() {
PlayerEvent.PLAYER_JOIN.register(player -> {
PlayerProfileData data = ModWorldData.PLAYER_NAME.getGlobal(player.getWorld());
PlayerProfileData data = ModWorldData.PLAYER_PROFILE.getGlobal(player.getWorld());
data.onJoin(player);
});

TickEvent.SERVER_POST.register(server -> {
PlayerProfileData data = ModWorldData.PLAYER_NAME.getGlobal(server);
PlayerProfileData data = ModWorldData.PLAYER_PROFILE.getGlobal(server);
data.onTick(server);
});
}
Expand Down
3 changes: 2 additions & 1 deletion common/src/main/resources/assets/academy/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@
"cobblemon.ui.starter.choosebutton.same": "I don't choose you!",
"entity.academy.partner_npc": "Partner NPC",
"entity.academy.safari_npc": "Safari NPC",
"entity.academy.nurse_npc": "Nurse NPC"
"entity.academy.nurse_npc": "Nurse NPC",
"stat_type.minecraft.custom": "Custom"
}

0 comments on commit f4e68b6

Please sign in to comment.