diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/HMCCosmeticsPlugin.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/HMCCosmeticsPlugin.java index 4acf131c..2f2285fa 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/HMCCosmeticsPlugin.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/HMCCosmeticsPlugin.java @@ -26,6 +26,12 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import org.spongepowered.configurate.ConfigurateException; +import org.spongepowered.configurate.ConfigurationNode; +import org.spongepowered.configurate.ConfigurationOptions; +import org.spongepowered.configurate.loader.ConfigurationLoader; +import org.spongepowered.configurate.objectmapping.ObjectMapper; +import org.spongepowered.configurate.objectmapping.meta.NodeResolver; +import org.spongepowered.configurate.yaml.NodeStyle; import org.spongepowered.configurate.yaml.YamlConfigurationLoader; import java.io.File; @@ -35,6 +41,7 @@ public final class HMCCosmeticsPlugin extends JavaPlugin { private static HMCCosmeticsPlugin instance; private static boolean disable = false; + private static YamlConfigurationLoader configLoader; @Override public void onEnable() { @@ -113,11 +120,13 @@ public final class HMCCosmeticsPlugin extends JavaPlugin { build.register(Location.class, LocationSerializer.INSTANCE); build.register(ItemStack.class, ItemSerializer.INSTANCE); })) + .nodeStyle(NodeStyle.BLOCK) .build(); try { - Settings.load(loader.load()); + Settings.load(loader.load(ConfigurationOptions.defaults())); WardrobeSettings.load(loader.load().node("wardrobe")); DatabaseSettings.load(loader.load().node("database-settings")); + configLoader = loader; } catch (ConfigurateException e) { throw new RuntimeException(e); } @@ -132,6 +141,7 @@ public final class HMCCosmeticsPlugin extends JavaPlugin { build.register(Location.class, LocationSerializer.INSTANCE); build.register(ItemStack.class, ItemSerializer.INSTANCE); })) + .nodeStyle(NodeStyle.BLOCK) .build(); try { MessagesUtil.setup(messagesLoader.load()); @@ -179,4 +189,17 @@ public final class HMCCosmeticsPlugin extends JavaPlugin { public static boolean isDisable() { return disable; } + + public static YamlConfigurationLoader getConfigLoader() { + return configLoader; + } + + public static void saveConfig(ConfigurationNode node) { + try { + HMCCosmeticsPlugin.getConfigLoader().save(node); + HMCCosmeticsPlugin.getInstance().getLogger().info("Set new location " + node.path()); + } catch (ConfigurateException e) { + throw new RuntimeException(e); + } + } } 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 6b501b1e..bae74fec 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/command/CosmeticCommand.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/command/CosmeticCommand.java @@ -2,6 +2,7 @@ package com.hibiscusmc.hmccosmetics.command; import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin; import com.hibiscusmc.hmccosmetics.config.Settings; +import com.hibiscusmc.hmccosmetics.config.WardrobeSettings; import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetic; import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot; import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetics; @@ -12,6 +13,8 @@ import com.hibiscusmc.hmccosmetics.gui.special.DyeMenu; import com.hibiscusmc.hmccosmetics.user.CosmeticUser; import com.hibiscusmc.hmccosmetics.user.CosmeticUsers; import com.hibiscusmc.hmccosmetics.util.MessagesUtil; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; @@ -83,6 +86,13 @@ public class CosmeticCommand implements CommandExecutor { return true; } + TagResolver placeholders = + TagResolver.resolver(Placeholder.parsed("cosmetic", cosmetic.getId()), + TagResolver.resolver(Placeholder.parsed("player", player.getName())), + TagResolver.resolver(Placeholder.parsed("cosmeticslot", cosmetic.getSlot().name()))); + + MessagesUtil.sendMessage(player, "equip-cosmetic", placeholders); + user.addPlayerCosmetic(cosmetic); user.updateCosmetic(cosmetic.getSlot()); return true; @@ -108,6 +118,13 @@ public class CosmeticCommand implements CommandExecutor { CosmeticUser user = CosmeticUsers.getUser(player); + TagResolver placeholders = + TagResolver.resolver(Placeholder.parsed("cosmetic", user.getCosmetic(cosmeticSlot).getId()), + TagResolver.resolver(Placeholder.parsed("player", player.getName())), + TagResolver.resolver(Placeholder.parsed("cosmeticslot", cosmeticSlot.name()))); + + MessagesUtil.sendMessage(player, "unequip-cosmetic", placeholders); + user.removeCosmeticSlot(cosmeticSlot); user.updateCosmetic(cosmeticSlot); return true; @@ -182,6 +199,35 @@ public class CosmeticCommand implements CommandExecutor { DyeMenu.openMenu(user, user.getCosmetic(CosmeticSlot.valueOf(args[1]))); } + else if (args[0].equalsIgnoreCase("setlocation")) { + + Player player = sender instanceof Player ? (Player) sender : null; + if (player == null) return true; + + if (args.length < 2) { + MessagesUtil.sendMessage(player, "not-enough-args"); + return true; + } + + if (args[1].equalsIgnoreCase("wardrobelocation")) { + WardrobeSettings.setWardrobeLocation(player.getLocation()); + MessagesUtil.sendMessage(player, "set-wardrobe-location"); + return true; + } + + if (args[1].equalsIgnoreCase("viewerlocation")) { + WardrobeSettings.setViewerLocation(player.getLocation()); + MessagesUtil.sendMessage(player, "set-wardrobe-viewing"); + return true; + } + + if (args[1].equalsIgnoreCase("leavelocation")) { + WardrobeSettings.setLeaveLocation(player.getLocation()); + MessagesUtil.sendMessage(player, "set-wardrobe-leaving"); + return true; + } + } + else if (args[0].equalsIgnoreCase("dump") && args.length == 1) { Player player = sender instanceof Player ? (Player) sender : null; if (player == null) return true; 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 389f4d0a..7cd882a2 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/command/CosmeticCommandTabComplete.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/command/CosmeticCommandTabComplete.java @@ -31,6 +31,7 @@ public class CosmeticCommandTabComplete implements TabCompleter { completions.add("reload"); completions.add("dataclear"); completions.add("dye"); + completions.add("setlocation"); } if (!(sender instanceof Player)) return completions; @@ -54,6 +55,10 @@ public class CosmeticCommandTabComplete implements TabCompleter { for (CosmeticSlot slot : user.getDyeableSlots()) { completions.add(slot.name()); } + } else if (args[0].equalsIgnoreCase("setlocation")) { + completions.add("wardrobelocation"); + completions.add("viewerlocation"); + completions.add("leavelocation"); } } 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 3f930339..6eaf693e 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/WardrobeSettings.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/WardrobeSettings.java @@ -1,5 +1,6 @@ package com.hibiscusmc.hmccosmetics.config; +import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin; import com.hibiscusmc.hmccosmetics.config.serializer.LocationSerializer; import com.hibiscusmc.hmccosmetics.util.MessagesUtil; import com.hibiscusmc.hmccosmetics.util.misc.Utils; @@ -27,6 +28,7 @@ public class WardrobeSettings { private static final String EQUIP_PUMPKIN_WARDROBE = "equip-pumpkin"; private static final String RETURN_LAST_LOCATION = "return-last-location"; + private static ConfigurationNode configRoot; private static boolean disableOnDamage; private static int displayRadius; private static boolean portable; @@ -43,6 +45,7 @@ public class WardrobeSettings { private static Location leaveLocation; public static void load(ConfigurationNode source) { + configRoot = source; disableOnDamage = source.node(DISABLE_ON_DAMAGE_PATH).getBoolean(); displayRadius = source.node(DISPLAY_RADIUS_PATH).getInt(); @@ -133,4 +136,57 @@ public class WardrobeSettings { return wardrobeLocation.distanceSquared(location) <= staticRadius * staticRadius; } + public static void setWardrobeLocation(Location newLocation) { + wardrobeLocation = newLocation; + + HMCCosmeticsPlugin plugin = HMCCosmeticsPlugin.getInstance(); + + plugin.getConfig().set("wardrobe.wardrobe-location." + "world", newLocation.getWorld().getName()); + plugin.getConfig().set("wardrobe.wardrobe-location." + "x", newLocation.getX()); + plugin.getConfig().set("wardrobe.wardrobe-location." + "y", newLocation.getY()); + plugin.getConfig().set("wardrobe.wardrobe-location." + "z", newLocation.getZ()); + plugin.getConfig().set("wardrobe.wardrobe-location." + "yaw", newLocation.getYaw()); + plugin.getConfig().set("wardrobe.wardrobe-location." + "pitch", newLocation.getPitch()); + + /* Configuration sets suck + source.node(WORLD).set(loc.getWorld().getName()); + source.node(X).set(loc.getX()); + source.node(Y).set(loc.getY()); + source.node(Z).set(loc.getZ()); + source.node(YAW).set(loc.getYaw()); + source.node(PITCH).set(loc.getPitch()); + */ + + HMCCosmeticsPlugin.getInstance().saveConfig(); + } + + public static void setViewerLocation(Location newLocation) { + viewerLocation = newLocation; + + HMCCosmeticsPlugin plugin = HMCCosmeticsPlugin.getInstance(); + + plugin.getConfig().set("wardrobe.viewer-location." + "world", newLocation.getWorld().getName()); + plugin.getConfig().set("wardrobe.viewer-location." + "x", newLocation.getX()); + plugin.getConfig().set("wardrobe.viewer-location." + "y", newLocation.getY()); + plugin.getConfig().set("wardrobe.viewer-location." + "z", newLocation.getZ()); + plugin.getConfig().set("wardrobe.viewer-location." + "yaw", newLocation.getYaw()); + plugin.getConfig().set("wardrobe.viewer-location." + "pitch", newLocation.getPitch()); + + HMCCosmeticsPlugin.getInstance().saveConfig(); + } + + public static void setLeaveLocation(Location newLocation) { + leaveLocation = newLocation; + + HMCCosmeticsPlugin plugin = HMCCosmeticsPlugin.getInstance(); + + plugin.getConfig().set("wardrobe.leave-location." + "world", newLocation.getWorld().getName()); + plugin.getConfig().set("wardrobe.leave-location." + "x", newLocation.getX()); + plugin.getConfig().set("wardrobe.leave-location." + "y", newLocation.getY()); + plugin.getConfig().set("wardrobe.leave-location." + "z", newLocation.getZ()); + plugin.getConfig().set("wardrobe.leave-location." + "yaw", newLocation.getYaw()); + plugin.getConfig().set("wardrobe.leave-location." + "pitch", newLocation.getPitch()); + + HMCCosmeticsPlugin.getInstance().saveConfig(); + } } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/serializer/LocationSerializer.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/serializer/LocationSerializer.java index 4aac580f..741d0e16 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/serializer/LocationSerializer.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/serializer/LocationSerializer.java @@ -1,9 +1,11 @@ package com.hibiscusmc.hmccosmetics.config.serializer; +import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.checkerframework.checker.nullness.qual.Nullable; +import org.spongepowered.configurate.ConfigurateException; import org.spongepowered.configurate.ConfigurationNode; import org.spongepowered.configurate.serialize.SerializationException; import org.spongepowered.configurate.serialize.TypeSerializer; @@ -40,7 +42,7 @@ public class LocationSerializer implements TypeSerializer { } @Override - public void serialize(final Type type, @Nullable final Location obj, final ConfigurationNode node) throws SerializationException { - + public void serialize(final Type type, @Nullable final Location loc, final ConfigurationNode source) throws SerializationException { + // Empty } } diff --git a/common/src/main/resources/messages.yml b/common/src/main/resources/messages.yml index c2cdcfda..5935b17b 100644 --- a/common/src/main/resources/messages.yml +++ b/common/src/main/resources/messages.yml @@ -1,11 +1,18 @@ prefix: "HMCCosmetics > " reloaded: "%prefix% Config files reloaded!" +not-enough-args: "%prefix% Improper amount of arguments" no-permission: "%prefix% No Permission!" no-cosmetic-permission: "%prefix% You do not have permission for this cosmetic!" opened-wardrobe: "%prefix% Opened wardrobe!" closed-wardrobe: "%prefix% Closed wardrobe!" -not-near-wardrobe: "%prefix% You are not near the wardrobe!" +not-near-wardrobe: "%prefix% You are not near the wardrobe!" +set-wardrobe-location: "%prefix% Set new wardrobe location!" +set-wardrobe-viewing: "%prefix% Set new wardrobe viewing location!" +set-wardrobe-leaving: "%prefix% Set new wardrobe leaving location!" + +equip-cosmetic: "%prefix% You have equipped !" +unequip-cosmetic: "%prefix% You have unequipped !" invalid-slot: "%prefix% Invalid cosmetic slot!" invalid-player: "%prefix% Invalid Player!"