NobelD / NobleWhitelist

A simple whitelist management plugin including uuid, name and bypass access.
MIT License
7 stars 2 forks source link

Vanilla whitelist migrator #10

Open NobelD opened 3 months ago

NobelD commented 3 months ago

Because a function is being requested to migrate from the vanilla whitelist to this plugin a small lightweight plugin was created temporarily. Later it will be added to the base plugin.

/nwlmigrate -> Migrate from the vanilla whitelist to this plugin. /nwlimport -> Registers all players that have data saved (only uuid)

Source Code of the plugin ```java package me.nobeld.vanillanwl; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import me.nobeld.noblewhitelist.NobleWhitelist; import me.nobeld.noblewhitelist.model.whitelist.WhitelistEntry; import me.nobeld.noblewhitelist.util.UUIDUtil; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; import java.util.logging.Level; public class Main extends JavaPlugin { private static NobleWhitelist base; @Override public void onEnable() { base = NobleWhitelist.getPlugin(); getCommand("nwlmigrate").setExecutor(new Migrate()); getCommand("nwlimport").setExecutor(new Import()); getLogger().log(Level.WARNING, "Plugin migrator for Vanilla Whitelist to NobleWhitelist was enabled."); getLogger().log(Level.WARNING, "This plugin was made only for NobleWhitelist 1.2.4."); getLogger().log(Level.WARNING, "Use the command /nwlmigrate to start the process."); getLogger().log(Level.WARNING, "After migrate remove this plugin, as it has no another function."); } public class Migrate implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { Bukkit.getScheduler().runTaskAsynchronously(Main.this, () -> { try (InputStream file = Files.newInputStream(Paths.get("whitelist.json"))) { JsonElement element = JsonParser.parseReader(new InputStreamReader(file)); JsonArray json = element.getAsJsonArray(); if (json.isEmpty()) { sendMessage(sender, "No entries were found in the vanilla whitelist."); } else { int total = 0; int skipped = 0; for (JsonElement e : json.asList()) { try { String name = e.getAsJsonObject().get("name").getAsString(); UUID uuid = UUIDUtil.parseUUID(e.getAsJsonObject().get("uuid").getAsString()); if (name == null && uuid == null) { skipped++; continue; } base.whitelistData().saveEntry(new WhitelistEntry(name, uuid)); total++; } catch (Exception ex) { logMessage(Level.WARNING, "Skipped error for an entry.", ex); skipped++; } } if (total == 0) { sendMessage(sender, "No available entries were found, no migration was made."); } else { sendMessage(sender, "A total of " + total + " entries were migrated."); } if (skipped != 0) { sendMessage(sender, "A total of " + skipped + " entries were skipped."); } } } catch (IOException e) { sendMessage(sender, "An exception occurred while trying to migrate, check console for more details."); logMessage(Level.SEVERE, "An exception occurred while trying to migrate.", e); } }); return true; } } public class Import implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { Bukkit.getScheduler().runTaskAsynchronously(Main.this, () -> { Path path; try (InputStream i = Files.newInputStream(Paths.get("server.properties"))) { Properties properties = new Properties(); properties.load(i); String levelName = properties.getProperty("level-name"); path = Paths.get(levelName, "playerdata"); } catch (IOException e) { logMessage(Level.SEVERE, "An error occurred while reading the properties.", e); return; } File file = new File(path.toAbsolutePath().toString()); if (!file.exists() || !file.isDirectory()) { logMessage(Level.WARNING, "No usable directory was found."); return; } File[] files = file.listFiles(); if (files == null || files.length == 0) { logMessage(Level.WARNING, "No usable files were found."); return; } int total = 0; int skipped = 0; for (File f : files) { String name = f.getName(); if (name.endsWith(".dat_old")) { continue; } name = name.replace(".dat", ""); UUID uuid = UUIDUtil.parseUUID(name); if (uuid == null) { skipped++; continue; } base.whitelistData().saveEntry(new WhitelistEntry(null, uuid)); total++; } if (total == 0) { sendMessage(sender, "No available entries were found, no migration was made."); } else { sendMessage(sender, "A total of " + total + " entries were imported. (only uuid)"); } if (skipped != 0) { sendMessage(sender, "A total of " + skipped + " entries were skipped."); } }); return true; } } private void sendMessage(CommandSender sender, String msg) { Bukkit.getScheduler().runTask(Main.this, () -> sender.sendMessage("[NWLMigrator] " + msg)); } private void logMessage(Level lvl, String msg) { Bukkit.getScheduler().runTask(Main.this, () -> Main.this.getLogger().log(lvl, msg)); } private void logMessage(Level lvl, String msg, Throwable e) { Bukkit.getScheduler().runTask(Main.this, () -> Main.this.getLogger().log(lvl, msg, e)); } } ```

Link: VanillaNWL-1.0.0.jar.zip

IasonKalaitzakis commented 3 months ago

Ah thanks a ton! I will try it as soon as I can and let you know if it worked for me!