|
| 1 | +package net.thenextlvl.perworlds.importer; |
| 2 | + |
| 3 | +import net.thenextlvl.perworlds.PerWorldsPlugin; |
| 4 | +import net.thenextlvl.perworlds.WorldGroup; |
| 5 | +import net.thenextlvl.perworlds.data.PlayerData; |
| 6 | +import org.bukkit.generator.WorldInfo; |
| 7 | +import org.jspecify.annotations.NullMarked; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.util.HashSet; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Objects; |
| 15 | +import java.util.Set; |
| 16 | +import java.util.UUID; |
| 17 | + |
| 18 | +@NullMarked |
| 19 | +public abstract class Importer { |
| 20 | + protected final PerWorldsPlugin plugin; |
| 21 | + |
| 22 | + private final Path dataPath; |
| 23 | + private final String name; |
| 24 | + |
| 25 | + protected Importer(PerWorldsPlugin plugin, String name) { |
| 26 | + this.dataPath = Path.of("plugins", name); |
| 27 | + this.name = name; |
| 28 | + this.plugin = plugin; |
| 29 | + } |
| 30 | + |
| 31 | + public Path getDataPath() { |
| 32 | + return dataPath; |
| 33 | + } |
| 34 | + |
| 35 | + public String getName() { |
| 36 | + return name; |
| 37 | + } |
| 38 | + |
| 39 | + public void load() { |
| 40 | + try { |
| 41 | + var groups = loadGroups(); |
| 42 | + loadPlayers(groups); |
| 43 | + } catch (IOException e) { |
| 44 | + plugin.getComponentLogger().error("Failed to import {}", name, e); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public Set<WorldGroup> loadGroups() throws IOException { |
| 49 | + var read = readGroups(); |
| 50 | + var groups = new HashSet<WorldGroup>(read.size()); |
| 51 | + read.forEach((group, worlds) -> { |
| 52 | + var worldGroup = plugin.groupProvider().getGroup(group).orElseGet(() -> |
| 53 | + plugin.groupProvider().createGroup(group)); |
| 54 | + worlds.stream().map(plugin.getServer()::getWorld) |
| 55 | + .filter(Objects::nonNull) |
| 56 | + .forEach(worldGroup::addWorld); |
| 57 | + groups.add(worldGroup); |
| 58 | + }); |
| 59 | + return groups; |
| 60 | + } |
| 61 | + |
| 62 | + public void loadPlayers(Set<WorldGroup> groups) throws IOException { |
| 63 | + // todo: extract to readPlayer |
| 64 | + readPlayers().forEach((uuid, name) -> { |
| 65 | + groups.stream().forEach(group -> group.getWorlds().map(WorldInfo::getName) |
| 66 | + .map(this.dataPath.resolve("worlds")::resolve) |
| 67 | + .map(path -> path.resolve(name + ".json")) |
| 68 | + .filter(Files::isRegularFile) |
| 69 | + .findAny().ifPresent(path -> { |
| 70 | + // todo: load player data from path into the current group |
| 71 | + }) |
| 72 | + ); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + public abstract Map<String, Set<String>> readGroups() throws IOException; |
| 77 | + |
| 78 | + public abstract Map<UUID, String> readPlayers() throws IOException; |
| 79 | + |
| 80 | + public abstract PlayerData readPlayer(UUID uuid, String name, WorldGroup group) throws IOException; |
| 81 | +} |
0 commit comments