diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/HMCCosmeticsPlugin.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/HMCCosmeticsPlugin.java index d30cf1ac..d2427229 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/HMCCosmeticsPlugin.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/HMCCosmeticsPlugin.java @@ -3,6 +3,7 @@ package com.hibiscusmc.hmccosmetics; import com.hibiscusmc.hmccosmetics.api.events.HMCCosmeticSetupEvent; import com.hibiscusmc.hmccosmetics.command.CosmeticCommand; import com.hibiscusmc.hmccosmetics.command.CosmeticCommandTabComplete; +import com.hibiscusmc.hmccosmetics.config.migration.WardrobeMigration; import com.hibiscusmc.hmccosmetics.config.section.DatabaseSettings; import com.hibiscusmc.hmccosmetics.config.Settings; import com.hibiscusmc.hmccosmetics.config.WardrobeSettings; @@ -75,6 +76,10 @@ public final class HMCCosmeticsPlugin extends HibiscusPlugin { saveResource("menus/defaultmenu_hands.yml", false); saveResource("menus/defaultmenu_backpacks.yml", false); } + if (!Path.of(getDataFolder().getPath() + "/wardrobes/").toFile().exists()) { + saveResource("wardrobes/defaultwardrobe.yml", false); + WardrobeMigration.migrate(this); + } // Configuration Sync final File configFile = Path.of(getInstance().getDataFolder().getPath(), "config.yml").toFile(); diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/command/CosmeticCommand.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/command/CosmeticCommand.java index 58f61047..11c1b84b 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/command/CosmeticCommand.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/command/CosmeticCommand.java @@ -215,7 +215,7 @@ public class CosmeticCommand implements CommandExecutor { } return true; } - case ("wardrobe") -> { + case ("wardrobes") -> { if (sender instanceof Player) player = ((Player) sender).getPlayer(); if (args.length == 1) { @@ -354,10 +354,10 @@ public class CosmeticCommand implements CommandExecutor { } Wardrobe wardrobe = WardrobeSettings.getWardrobe(args[1]); if (wardrobe == null) { - wardrobe = new Wardrobe(args[1], new WardrobeLocation(null, null, null), null, -1, null); - WardrobeSettings.addWardrobe(wardrobe); - //MessagesUtil.sendMessage(player, "no-wardrobes"); - //return true; + //wardrobe = new Wardrobe(args[1], new WardrobeLocation(null, null, null), null, -1, null); + //WardrobeSettings.addWardrobe(wardrobe); + MessagesUtil.sendMessage(player, "no-wardrobes"); + return true; } if (args[2].equalsIgnoreCase("npclocation")) { diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/command/CosmeticCommandTabComplete.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/command/CosmeticCommandTabComplete.java index 82d59939..d340e45a 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/command/CosmeticCommandTabComplete.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/command/CosmeticCommandTabComplete.java @@ -36,7 +36,7 @@ public class CosmeticCommandTabComplete implements TabCompleter { if (hasPermission(sender, "hmccosmetics.cmd.unapply")) completions.add("unapply"); if (hasPermission(sender, "hmccosmetics.cmd.menu")) completions.add("menu"); if (hasPermission(sender, "hmccosmetics.cmd.reload")) completions.add("reload"); - if (hasPermission(sender, "hmccosmetics.cmd.wardrobe")) completions.add("wardrobe"); + if (hasPermission(sender, "hmccosmetics.cmd.wardrobe")) completions.add("wardrobes"); if (hasPermission(sender, "hmccosmetics.cmd.dataclear")) completions.add("dataclear"); if (hasPermission(sender, "hmccosmetics.cmd.dye")) completions.add("dye"); if (hasPermission(sender, "hmccosmetics.cmd.setwardrobesetting")) completions.add("setwardrobesetting"); @@ -80,7 +80,7 @@ public class CosmeticCommandTabComplete implements TabCompleter { completions.add("true"); completions.add("false"); } - case "wardrobe" -> { + case "wardrobes" -> { for (Wardrobe wardrobe : WardrobeSettings.getWardrobes()) { if (wardrobe.hasPermission()) { if (user.getPlayer().hasPermission(wardrobe.getPermission())) completions.add(wardrobe.getId()); @@ -108,7 +108,7 @@ public class CosmeticCommandTabComplete implements TabCompleter { case "dye" -> { completions.add("#FFFFFF"); } - case "menu", "wardrobe", "apply", "unapply" -> { + case "menu", "wardrobes", "apply", "unapply" -> { for (Player player : Bukkit.getOnlinePlayers()) { completions.add(player.getName()); } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/WardrobeSettings.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/WardrobeSettings.java index 81037ad8..c2cb4fa8 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/WardrobeSettings.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/WardrobeSettings.java @@ -6,16 +6,25 @@ import com.hibiscusmc.hmccosmetics.config.section.WardrobeLocation; import com.hibiscusmc.hmccosmetics.util.MessagesUtil; import lombok.Getter; import me.lojosho.hibiscuscommons.config.serializer.LocationSerializer; +import me.lojosho.shaded.configurate.CommentedConfigurationNode; +import me.lojosho.shaded.configurate.ConfigurateException; import me.lojosho.shaded.configurate.ConfigurationNode; +import me.lojosho.shaded.configurate.yaml.YamlConfigurationLoader; import net.kyori.adventure.bossbar.BossBar; import org.apache.commons.lang3.EnumUtils; import org.bukkit.GameMode; import org.bukkit.Location; +import org.bukkit.configuration.file.YamlConfiguration; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collection; import java.util.HashMap; import java.util.Set; import java.util.logging.Level; +import java.util.stream.Stream; public class WardrobeSettings { @@ -96,7 +105,7 @@ public class WardrobeSettings { private static boolean preventDamage; @Getter private static GameMode exitGamemode; - private static HashMap wardrobes; + private static final HashMap wardrobes = new HashMap<>(); @Getter private static String bossbarMessage; @Getter @@ -163,28 +172,50 @@ public class WardrobeSettings { transitionStay = transitionNode.node(TRANSITION_STAY_PATH).getInt(2000); transitionFadeOut = transitionNode.node(TRANSITION_FADE_OUT_PATH).getInt(2000); - wardrobes = new HashMap<>(); - for (ConfigurationNode wardrobesNode : source.node(WARDROBES_PATH).childrenMap().values()) { - String id = wardrobesNode.key().toString(); - try { - Location npcLocation = LocationSerializer.INSTANCE.deserialize(Location.class, wardrobesNode.node(NPC_LOCATION_PATH)); - MessagesUtil.sendDebugMessages("Wardrobe Location: " + npcLocation); - Location viewerLocation = LocationSerializer.INSTANCE.deserialize(Location.class, wardrobesNode.node(VIEWER_LOCATION_PATH)); - MessagesUtil.sendDebugMessages("Viewer Location: " + viewerLocation); - Location leaveLocation = LocationSerializer.INSTANCE.deserialize(Location.class, wardrobesNode.node(LEAVE_LOCATION_PATH)); - if (leaveLocation == null) leaveLocation = viewerLocation; - MessagesUtil.sendDebugMessages("Leave Location: " + leaveLocation); - WardrobeLocation wardrobeLocation = new WardrobeLocation(npcLocation, viewerLocation, leaveLocation); + wardrobes.clear(); + File wardrobeFolder = new File(HMCCosmeticsPlugin.getInstance().getDataFolder() + "/wardrobes"); + try (Stream walkStream = Files.walk(wardrobeFolder.toPath())) { + walkStream.filter(p -> p.toFile().isFile()).forEach(child -> { + if (child.toString().contains(".yml") || child.toString().contains(".yaml")) { + MessagesUtil.sendDebugMessages("Scanning " + child); + // Loads file + YamlConfigurationLoader loader = YamlConfigurationLoader.builder().path(child).build(); + CommentedConfigurationNode wardrobesNode; + try { + wardrobesNode = loader.load(); + } catch (ConfigurateException e) { + throw new RuntimeException(e); + } + for (ConfigurationNode wardrobeConfig : wardrobesNode.childrenMap().values()) { + registerWardrobe(wardrobeConfig, child.toFile()); + } + } + }); + } catch (Exception e) { + e.printStackTrace(); + } + } - String permission = wardrobesNode.node(PERMISSION_PATH).getString(); - String defaultMenu = wardrobesNode.node(WARDROBE_DEFAULT_MENU).getString(); - int distance = wardrobesNode.node(DISTANCE_PATH).getInt(-1); + private static void registerWardrobe(ConfigurationNode wardrobesNode, File file) { + String id = wardrobesNode.key().toString(); + try { + Location npcLocation = LocationSerializer.INSTANCE.deserialize(Location.class, wardrobesNode.node(NPC_LOCATION_PATH)); + MessagesUtil.sendDebugMessages("Wardrobe Location: " + npcLocation); + Location viewerLocation = LocationSerializer.INSTANCE.deserialize(Location.class, wardrobesNode.node(VIEWER_LOCATION_PATH)); + MessagesUtil.sendDebugMessages("Viewer Location: " + viewerLocation); + Location leaveLocation = LocationSerializer.INSTANCE.deserialize(Location.class, wardrobesNode.node(LEAVE_LOCATION_PATH)); + if (leaveLocation == null) leaveLocation = viewerLocation; + MessagesUtil.sendDebugMessages("Leave Location: " + leaveLocation); + WardrobeLocation wardrobeLocation = new WardrobeLocation(npcLocation, viewerLocation, leaveLocation); - Wardrobe wardrobe = new Wardrobe(id, wardrobeLocation, permission, distance, defaultMenu); - addWardrobe(wardrobe); - } catch (Exception e) { - MessagesUtil.sendDebugMessages("Unable to create wardrobe " + id, Level.SEVERE); - } + String permission = wardrobesNode.node(PERMISSION_PATH).getString(); + String defaultMenu = wardrobesNode.node(WARDROBE_DEFAULT_MENU).getString(); + int distance = wardrobesNode.node(DISTANCE_PATH).getInt(-1); + + Wardrobe wardrobe = new Wardrobe(id, wardrobeLocation, permission, distance, defaultMenu, file); + addWardrobe(wardrobe); + } catch (Exception e) { + MessagesUtil.sendDebugMessages("Unable to create wardrobe " + id, Level.SEVERE); } } @@ -215,16 +246,20 @@ public class WardrobeSettings { public static void setNPCLocation(Wardrobe wardrobe, Location newLocation) { wardrobe.getLocation().setNpcLocation(newLocation); - HMCCosmeticsPlugin plugin = HMCCosmeticsPlugin.getInstance(); + File wardrobeFile = wardrobe.getWardrobeFile(); + if (wardrobeFile == null) { + return; + } + YamlConfiguration config = YamlConfiguration.loadConfiguration(wardrobeFile); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".npc-location." + "world", newLocation.getWorld().getName()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".npc-location." + "x", newLocation.getX()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".npc-location." + "y", newLocation.getY()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".npc-location." + "z", newLocation.getZ()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".npc-location." + "yaw", newLocation.getYaw()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".npc-location." + "pitch", newLocation.getPitch()); + config.set(wardrobe.getId() + ".npc-location." + "world", newLocation.getWorld().getName()); + config.set(wardrobe.getId() + ".npc-location." + "x", newLocation.getX()); + config.set(wardrobe.getId() + ".npc-location." + "y", newLocation.getY()); + config.set(wardrobe.getId() + ".npc-location." + "z", newLocation.getZ()); + config.set(wardrobe.getId() + ".npc-location." + "yaw", newLocation.getYaw()); + config.set(wardrobe.getId() + ".npc-location." + "pitch", newLocation.getPitch()); - plugin.saveConfig(); + saveConfig(config, wardrobeFile); } /** @@ -234,16 +269,20 @@ public class WardrobeSettings { public static void setViewerLocation(Wardrobe wardrobe, Location newLocation) { wardrobe.getLocation().setViewerLocation(newLocation); - HMCCosmeticsPlugin plugin = HMCCosmeticsPlugin.getInstance(); + File wardrobeFile = wardrobe.getWardrobeFile(); + if (wardrobeFile == null) { + return; + } + YamlConfiguration config = YamlConfiguration.loadConfiguration(wardrobeFile); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".viewer-location.world", newLocation.getWorld().getName()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".viewer-location.x", newLocation.getX()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".viewer-location.y", newLocation.getY()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".viewer-location.z", newLocation.getZ()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".viewer-location.yaw", newLocation.getYaw()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".viewer-location.pitch", newLocation.getPitch()); + config.set(wardrobe.getId() + ".viewer-location.world", newLocation.getWorld().getName()); + config.set(wardrobe.getId() + ".viewer-location.x", newLocation.getX()); + config.set(wardrobe.getId() + ".viewer-location.y", newLocation.getY()); + config.set(wardrobe.getId() + ".viewer-location.z", newLocation.getZ()); + config.set(wardrobe.getId() + ".viewer-location.yaw", newLocation.getYaw()); + config.set(wardrobe.getId() + ".viewer-location.pitch", newLocation.getPitch()); - plugin.saveConfig(); + saveConfig(config, wardrobeFile); } /** @@ -253,45 +292,69 @@ public class WardrobeSettings { public static void setLeaveLocation(Wardrobe wardrobe, Location newLocation) { wardrobe.getLocation().setLeaveLocation(newLocation); - HMCCosmeticsPlugin plugin = HMCCosmeticsPlugin.getInstance(); + File wardrobeFile = wardrobe.getWardrobeFile(); + if (wardrobeFile == null) { + return; + } + YamlConfiguration config = YamlConfiguration.loadConfiguration(wardrobeFile); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".leave-location.world", newLocation.getWorld().getName()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".leave-location.x", newLocation.getX()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".leave-location.y", newLocation.getY()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".leave-location.z", newLocation.getZ()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".leave-location.yaw", newLocation.getYaw()); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".leave-location.pitch", newLocation.getPitch()); + config.set(wardrobe.getId() + ".leave-location.world", newLocation.getWorld().getName()); + config.set(wardrobe.getId() + ".leave-location.x", newLocation.getX()); + config.set(wardrobe.getId() + ".leave-location.y", newLocation.getY()); + config.set(wardrobe.getId() + ".leave-location.z", newLocation.getZ()); + config.set(wardrobe.getId() + ".leave-location.yaw", newLocation.getYaw()); + config.set(wardrobe.getId() + ".leave-location.pitch", newLocation.getPitch()); - plugin.saveConfig(); + saveConfig(config, wardrobeFile); } public static void setWardrobePermission(Wardrobe wardrobe, String permission) { wardrobe.setPermission(permission); - HMCCosmeticsPlugin plugin = HMCCosmeticsPlugin.getInstance(); + File wardrobeFile = wardrobe.getWardrobeFile(); + if (wardrobeFile == null) { + return; + } + YamlConfiguration config = YamlConfiguration.loadConfiguration(wardrobeFile); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".permission", permission); + config.set(wardrobe.getId() + ".permission", permission); - plugin.saveConfig(); + saveConfig(config, wardrobeFile); } public static void setWardrobeDistance(Wardrobe wardrobe, int distance) { wardrobe.setDistance(distance); - HMCCosmeticsPlugin plugin = HMCCosmeticsPlugin.getInstance(); + File wardrobeFile = wardrobe.getWardrobeFile(); + if (wardrobeFile == null) { + return; + } + YamlConfiguration config = YamlConfiguration.loadConfiguration(wardrobeFile); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".distance", distance); + config.set(wardrobe.getId() + ".distance", distance); - plugin.saveConfig(); + saveConfig(config, wardrobeFile); } public static void setWardrobeDefaultMenu(Wardrobe wardrobe, String defaultMenu) { wardrobe.setDefaultMenu(defaultMenu); - HMCCosmeticsPlugin plugin = HMCCosmeticsPlugin.getInstance(); + File wardrobeFile = wardrobe.getWardrobeFile(); + if (wardrobeFile == null) { + return; + } + YamlConfiguration config = YamlConfiguration.loadConfiguration(wardrobeFile); - plugin.getConfig().set("wardrobe.wardrobes." + wardrobe.getId() + ".default-menu", defaultMenu); + config.set(wardrobe.getId() + ".default-menu", defaultMenu); - plugin.saveConfig(); + saveConfig(config, wardrobeFile); + } + + private static void saveConfig(YamlConfiguration config, File file) { + try { + config.save(file); + } catch (IOException e) { + throw new RuntimeException(e); + } } } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/migration/WardrobeMigration.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/migration/WardrobeMigration.java new file mode 100644 index 00000000..31f1849b --- /dev/null +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/migration/WardrobeMigration.java @@ -0,0 +1,35 @@ +package com.hibiscusmc.hmccosmetics.config.migration; + +import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.io.IOException; + +public class WardrobeMigration { + + public static void migrate(@NotNull HMCCosmeticsPlugin instance) { + File moveToFile = new File(instance.getDataFolder().getPath() + "/wardrobes/defaultwardrobe.yml"); + + YamlConfiguration moveTo = YamlConfiguration.loadConfiguration(moveToFile); + FileConfiguration moveFrom = instance.getConfig(); + ConfigurationSection wardrobes = moveFrom.getConfigurationSection("wardrobe.wardrobes"); + if (wardrobes == null) return; + if (moveFrom.getKeys(false).isEmpty()) return; + + for (String key : wardrobes.getKeys(false)) { + moveTo.set(key, wardrobes.getConfigurationSection(key)); + } + + moveFrom.set("wardrobe.wardrobes", null); + try { + instance.saveConfig(); + moveTo.save(moveToFile); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/section/Wardrobe.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/section/Wardrobe.java index 09203abf..e572f57b 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/section/Wardrobe.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/section/Wardrobe.java @@ -7,6 +7,7 @@ import org.bukkit.Location; import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; +import java.io.File; public class Wardrobe { @@ -20,6 +21,8 @@ public class Wardrobe { private WardrobeLocation location; @Getter @Setter @Nullable private String defaultMenu; + @Getter @Nullable + private File wardrobeFile; /** * This creates a Wardrobe object with all the information that a user will need when entering. @@ -29,12 +32,13 @@ public class Wardrobe { * @param distance The distance from the wardrobe that the player can be to enter, if -1, the player can enter from any distance * @param defaultMenu The default menu that the player will open when entering the wardrobe. */ - public Wardrobe(@NotNull String id, @NotNull WardrobeLocation location, @Nullable String permission, int distance, @Nullable String defaultMenu) { + public Wardrobe(@NotNull String id, @NotNull WardrobeLocation location, @Nullable String permission, int distance, @Nullable String defaultMenu, @Nullable File wardrobeFile) { this.id = id; this.location = location; this.distance = distance; - if (permission != null) this.permission = permission; - if (defaultMenu != null) this.defaultMenu = defaultMenu; + this.permission = permission; + this.defaultMenu = defaultMenu; + this.wardrobeFile = wardrobeFile; } /** diff --git a/common/src/main/resources/config.yml b/common/src/main/resources/config.yml index 25405699..ad161b2d 100644 --- a/common/src/main/resources/config.yml +++ b/common/src/main/resources/config.yml @@ -180,31 +180,3 @@ wardrobe: title-fade-in: 1000 # milliseconds title-stay: 500 # milliseconds title-fade-out: 1000 # milliseconds - wardrobes: - default: - # Distance in blocks that a player can interact with the wardrobe. -1 to ignore. - distance: -1 - # Permission required to use the wardrobe. - permission: "hmccosmetics.wardrobe.default" - default-menu: defaultmenu - npc-location: - world: "world" - x: 0 - y: 0 - z: 0 - yaw: 0 - pitch: 0 - viewer-location: - world: "world" - x: 5 - y: 0 - z: 5 - yaw: 0 - pitch: 0 - leave-location: - world: "world" - x: 5 - y: 5 - z: 5 - yaw: 0 - pitch: 0 diff --git a/common/src/main/resources/wardrobes/defaultwardrobe.yml b/common/src/main/resources/wardrobes/defaultwardrobe.yml new file mode 100644 index 00000000..84f9bd7f --- /dev/null +++ b/common/src/main/resources/wardrobes/defaultwardrobe.yml @@ -0,0 +1,27 @@ +default: + # Distance in blocks that a player can interact with the wardrobe. -1 to ignore. + distance: -1 + # Permission required to use the wardrobe. + permission: "hmccosmetics.wardrobe.default" + default-menu: defaultmenu + npc-location: + world: "world" + x: 0 + y: 0 + z: 0 + yaw: 0 + pitch: 0 + viewer-location: + world: "world" + x: 5 + y: 0 + z: 5 + yaw: 0 + pitch: 0 + leave-location: + world: "world" + x: 5 + y: 5 + z: 5 + yaw: 0 + pitch: 0 \ No newline at end of file