|
| 1 | +package net.thenextlvl.perworlds.importer.multiverse; |
| 2 | + |
| 3 | +import com.google.gson.JsonParseException; |
| 4 | +import com.google.gson.JsonParser; |
| 5 | +import com.google.gson.stream.JsonReader; |
| 6 | +import net.thenextlvl.perworlds.PerWorldsPlugin; |
| 7 | +import net.thenextlvl.perworlds.WorldGroup; |
| 8 | +import net.thenextlvl.perworlds.data.PlayerData; |
| 9 | +import net.thenextlvl.perworlds.group.PaperWorldGroup; |
| 10 | +import net.thenextlvl.perworlds.importer.Importer; |
| 11 | +import net.thenextlvl.perworlds.model.PaperPlayerData; |
| 12 | +import org.bukkit.configuration.file.YamlConfiguration; |
| 13 | +import org.jspecify.annotations.NullMarked; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.InputStreamReader; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.UUID; |
| 24 | + |
| 25 | +import static java.nio.file.StandardOpenOption.READ; |
| 26 | + |
| 27 | +@NullMarked |
| 28 | +public class MVInventoriesImporter extends Importer { |
| 29 | + |
| 30 | + public MVInventoriesImporter(PerWorldsPlugin plugin) { |
| 31 | + super(plugin, "Multiverse-Inventories"); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Map<String, Set<String>> readGroups() throws IOException { |
| 36 | + var path = getDataPath().resolve("groups.yml"); |
| 37 | + if (!Files.exists(path)) return new HashMap<>(); |
| 38 | + |
| 39 | + try (var reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) { |
| 40 | + var config = YamlConfiguration.loadConfiguration(reader); |
| 41 | + |
| 42 | + var groups = config.getConfigurationSection("groups"); |
| 43 | + if (groups == null) return new HashMap<>(); |
| 44 | + |
| 45 | + var result = new HashMap<String, Set<String>>(); |
| 46 | + groups.getKeys(false).forEach(group -> { |
| 47 | + var groupSection = groups.getConfigurationSection(group); |
| 48 | + if (groupSection == null) return; |
| 49 | + |
| 50 | + var worlds = groupSection.getStringList("worlds"); |
| 51 | + result.put(group, new HashSet<>(worlds)); |
| 52 | + }); |
| 53 | + return result; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Map<UUID, String> readPlayers() throws IOException { |
| 59 | + var path = getDataPath().resolve("playernames.json"); |
| 60 | + if (!Files.exists(path)) return new HashMap<>(); |
| 61 | + try (var reader = new JsonReader(new InputStreamReader( |
| 62 | + Files.newInputStream(path, READ), |
| 63 | + StandardCharsets.UTF_8 |
| 64 | + ))) { |
| 65 | + var object = JsonParser.parseReader(reader).getAsJsonObject(); |
| 66 | + var result = new HashMap<UUID, String>(object.size()); |
| 67 | + for (var entry : object.entrySet()) { |
| 68 | + try { |
| 69 | + var uuid = UUID.fromString(entry.getKey()); |
| 70 | + result.put(uuid, entry.getValue().getAsString()); |
| 71 | + } catch (IllegalArgumentException ignored) { |
| 72 | + } |
| 73 | + } |
| 74 | + return result; |
| 75 | + } catch (JsonParseException e) { |
| 76 | + plugin.getComponentLogger().warn("Failed to parse {}", path, e); |
| 77 | + return new HashMap<>(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public PlayerData readPlayer(UUID uuid, String name, WorldGroup group) throws IOException { |
| 83 | + var data = new PaperPlayerData(uuid, ((PaperWorldGroup) group)); |
| 84 | + |
| 85 | + return data; |
| 86 | + } |
| 87 | +} |
0 commit comments