diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/re/jag/parquet/commands/Savedata.java | 74 |
1 files changed, 52 insertions, 22 deletions
diff --git a/src/main/java/re/jag/parquet/commands/Savedata.java b/src/main/java/re/jag/parquet/commands/Savedata.java index 20d6210..f4223f9 100644 --- a/src/main/java/re/jag/parquet/commands/Savedata.java +++ b/src/main/java/re/jag/parquet/commands/Savedata.java @@ -1,8 +1,10 @@ package re.jag.parquet.commands; +import java.io.File; import java.util.UUID; import static net.minecraft.server.command.CommandManager.literal; +import static net.minecraft.server.command.CommandSource.suggestMatching; import static net.minecraft.server.command.CommandManager.argument; import com.mojang.brigadier.CommandDispatcher; @@ -13,9 +15,9 @@ import com.mojang.authlib.GameProfile; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.command.arguments.EntityArgumentType; import net.minecraft.command.arguments.ObjectiveArgumentType; import net.minecraft.command.arguments.ObjectiveCriteriaArgumentType; +import net.minecraft.entity.EntityType; import net.minecraft.item.Item; import net.minecraft.scoreboard.ScoreboardCriterion; import net.minecraft.scoreboard.ScoreboardObjective; @@ -25,6 +27,7 @@ import net.minecraft.text.LiteralText; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; import net.minecraft.world.dimension.DimensionType; +import net.minecraft.stat.ServerStatHandler; import net.minecraft.stat.Stat; import net.minecraft.stat.StatType; import net.minecraft.block.Block; @@ -37,44 +40,41 @@ public class Savedata { then( literal("playersave"). then(literal("list"). executes((c) -> list_local_saves(c.getSource())) - ). - then(argument("UUID", StringArgumentType.word()). - executes((c) -> get_player_save_uuid(c.getSource(), StringArgumentType.getString(c, "UUID"))) ) ). then(literal("stats"). - then(argument("player", EntityArgumentType.player()). + then(argument("player", StringArgumentType.word()). + suggests( (c, b) -> suggestMatching(c.getSource().getPlayerNames() , b)). then(argument("criteria", ObjectiveCriteriaArgumentType.objectiveCriteria()). executes((c) -> print_player_stat ( c.getSource(), - EntityArgumentType.getPlayer(c, "player"), + StringArgumentType.getString(c, "player"), ObjectiveCriteriaArgumentType.getCriteria(c, "criteria") )). then(literal("import").then(argument("score", ObjectiveArgumentType.objective()). executes((c) -> import_stat_to_scoreboard ( c.getSource(), - EntityArgumentType.getPlayer(c, "player"), + StringArgumentType.getString(c, "player"), ObjectiveCriteriaArgumentType.getCriteria(c, "criteria"), ObjectiveArgumentType.getWritableObjective(c, "score"), 1)). then(argument("multiplier", FloatArgumentType.floatArg()). executes((c) -> import_stat_to_scoreboard( c.getSource(), - EntityArgumentType.getPlayer(c, "player"), + StringArgumentType.getString(c, "player"), ObjectiveCriteriaArgumentType.getCriteria(c, "criteria"), ObjectiveArgumentType.getWritableObjective(c, "score"), FloatArgumentType.getFloat(c, "multiplier")) ) ) )) - ) - ) + )) ); dispatcher.register(savedata); } private static int help(ServerCommandSource source) { //TODO Help Text - source.sendFeedback(new LiteralText("Test"), false); + source.sendFeedback(new LiteralText("Hölp goes here"), false); return 1; } @@ -94,13 +94,13 @@ public class Savedata { return 1; } - private static int import_stat_to_scoreboard(ServerCommandSource source, ServerPlayerEntity player,ScoreboardCriterion stat, ScoreboardObjective objective, float multiplier) { + private static int import_stat_to_scoreboard(ServerCommandSource source, String player,ScoreboardCriterion stat, ScoreboardObjective objective, float multiplier) { int score = get_player_stat(source, player, stat); if (score < 0) return 0; ServerScoreboard server_scoreboard = source.getMinecraftServer().getScoreboard(); - ScoreboardPlayerScore player_score = server_scoreboard.getPlayerScore(player.getEntityName(), objective); + ScoreboardPlayerScore player_score = server_scoreboard.getPlayerScore(player, objective); int mod_score = multiplier >= 0 ? (int)((float)score * multiplier) : (int)((float)score / multiplier * (-1)); @@ -109,11 +109,30 @@ public class Savedata { return 1; } - private static int get_player_save_uuid(ServerCommandSource source, String uuid) { - return 1; + public static ServerStatHandler get_player_stat_handler(ServerCommandSource source, String player_name) { + ServerPlayerEntity player = source.getMinecraftServer().getPlayerManager().getPlayer(player_name); + if (player != null) { + return player.getStatHandler(); + } + + GameProfile profile = source.getMinecraftServer().getUserCache().findByName(player_name); + if (profile == null) { + return null; + } + + UUID player_uuid = profile.getId(); + + File file = new File(source.getMinecraftServer().getWorld(DimensionType.OVERWORLD).getSaveHandler().getWorldDir(), "stats"); + File file2 = new File(file, player_uuid + ".json"); + + if (!file2.exists()) { + return null; + } + + return new ServerStatHandler(source.getMinecraftServer(), file2); } - private static int print_player_stat(ServerCommandSource source, ServerPlayerEntity player, ScoreboardCriterion stat) { + private static int print_player_stat(ServerCommandSource source, String player, ScoreboardCriterion stat) { Integer stat_value = get_player_stat(source, player, stat); if (stat_value >= 0) @@ -124,8 +143,14 @@ public class Savedata { return 0; } - private static int get_player_stat(ServerCommandSource source, ServerPlayerEntity player, ScoreboardCriterion criteria) { - //TODO Add offline player stat read + private static int get_player_stat(ServerCommandSource source, String player_name, ScoreboardCriterion criteria) { + ServerStatHandler stat_handler = get_player_stat_handler(source, player_name); + + if (stat_handler == null) { + source.sendError(new LiteralText("Failed to get Statistics for Player " + player_name)); + return -1; + } + String criteria_name = criteria.getName(); int criteria_name_seperator = criteria_name.indexOf(':'); @@ -147,19 +172,24 @@ public class Savedata { @SuppressWarnings("unchecked") StatType<Block> stat_type_block = (StatType<Block>) stat_type; Stat<Block> stat = stat_type_block.getOrCreateStat((Block) stat_obj); - return player.getStatHandler().getStat(stat); + return stat_handler.getStat(stat); } else if (stat_obj instanceof Item) { @SuppressWarnings("unchecked") StatType<Item> stat_type_item = (StatType<Item>) stat_type; Stat<Item> stat = stat_type_item.getOrCreateStat((Item) stat_obj); - return player.getStatHandler().getStat(stat); + return stat_handler.getStat(stat); } else if (stat_obj instanceof Identifier) { @SuppressWarnings("unchecked") StatType<Identifier> stat_type_ident = (StatType<Identifier>) stat_type; Stat<Identifier> stat = stat_type_ident.getOrCreateStat((Identifier) stat_obj); - return player.getStatHandler().getStat(stat); + return stat_handler.getStat(stat); + } else if (stat_obj instanceof EntityType<?>) { + @SuppressWarnings("unchecked") + StatType<EntityType<?>> stat_type_ident = (StatType<EntityType<?>>) stat_type; + Stat<EntityType<?>> stat = stat_type_ident.getOrCreateStat((EntityType<?>) stat_obj); + return stat_handler.getStat(stat); } else { - source.sendError(new LiteralText("Unknown Object")); + source.sendError(new LiteralText("Unknown Object " + stat_obj.getClass().getName())); return -1; } } |