package re.jag.parquet.commands; import java.util.UUID; import static net.minecraft.server.command.CommandManager.literal; import static net.minecraft.server.command.CommandManager.argument; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.arguments.FloatArgumentType; import com.mojang.brigadier.arguments.StringArgumentType; 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.item.Item; import net.minecraft.scoreboard.ScoreboardCriterion; import net.minecraft.scoreboard.ScoreboardObjective; import net.minecraft.scoreboard.ScoreboardPlayerScore; import net.minecraft.scoreboard.ServerScoreboard; 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.Stat; import net.minecraft.stat.StatType; import net.minecraft.block.Block; public class Savedata { public static void register(CommandDispatcher dispatcher) { LiteralArgumentBuilder savedata = literal("savedata"). requires(source -> source.hasPermissionLevel(4)). executes((c) -> help(c.getSource())). 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("criteria", ObjectiveCriteriaArgumentType.objectiveCriteria()). executes((c) -> print_player_stat ( c.getSource(), EntityArgumentType.getPlayer(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"), 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"), 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); return 1; } private static int list_local_saves(ServerCommandSource source) { String uuid_list[] = source.getMinecraftServer().getWorld(DimensionType.OVERWORLD).getSaveHandler().getSavedPlayerIds(); for (int i = 0; i < uuid_list.length; i++) { GameProfile profile = source.getMinecraftServer().getUserCache().getByUuid(UUID.fromString(uuid_list[i])); String name = "N.A."; if(profile != null) name = profile.getName(); source.sendFeedback(new LiteralText(name + " (" + uuid_list[i] + ")"), false); } return 1; } private static int import_stat_to_scoreboard(ServerCommandSource source, ServerPlayerEntity 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); int mod_score = multiplier >= 0 ? (int)((float)score * multiplier) : (int)((float)score / multiplier * (-1)); player_score.setScore(mod_score); return 1; } private static int get_player_save_uuid(ServerCommandSource source, String uuid) { return 1; } private static int print_player_stat(ServerCommandSource source, ServerPlayerEntity player, ScoreboardCriterion stat) { Integer stat_value = get_player_stat(source, player, stat); if (stat_value >= 0) { source.sendFeedback(new LiteralText(stat_value.toString()), false); return 1; } return 0; } private static int get_player_stat(ServerCommandSource source, ServerPlayerEntity player, ScoreboardCriterion criteria) { //TODO Add offline player stat read String criteria_name = criteria.getName(); int criteria_name_seperator = criteria_name.indexOf(':'); String registry_name=""; String stat_name=""; if (criteria_name_seperator < 0) { source.sendError(new LiteralText("Expected valid registry and stat ID")); return -1; } else { registry_name = criteria_name.substring(0,criteria_name_seperator); stat_name = criteria_name.substring(criteria_name_seperator + 1, criteria_name.length() ); } StatType stat_type = Registry.STAT_TYPE.get(Identifier.splitOn(registry_name, '.')); Object stat_obj = stat_type.getRegistry().get(Identifier.splitOn(stat_name, '.')); if (stat_obj instanceof Block) { @SuppressWarnings("unchecked") StatType stat_type_block = (StatType) stat_type; Stat stat = stat_type_block.getOrCreateStat((Block) stat_obj); return player.getStatHandler().getStat(stat); } else if (stat_obj instanceof Item) { @SuppressWarnings("unchecked") StatType stat_type_item = (StatType) stat_type; Stat stat = stat_type_item.getOrCreateStat((Item) stat_obj); return player.getStatHandler().getStat(stat); } else if (stat_obj instanceof Identifier) { @SuppressWarnings("unchecked") StatType stat_type_ident = (StatType) stat_type; Stat stat = stat_type_ident.getOrCreateStat((Identifier) stat_obj); return player.getStatHandler().getStat(stat); } else { source.sendError(new LiteralText("Unknown Object")); return -1; } } }