9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2025-12-30 04:19:28 +00:00

Cosmetic wardrobe through commands #24

This commit is contained in:
LoJoSho
2022-12-29 11:57:31 -06:00
parent df8450c4e6
commit 20c8407a18
6 changed files with 143 additions and 4 deletions

View File

@@ -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);
}
}
}

View File

@@ -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;

View File

@@ -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");
}
}

View File

@@ -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();
}
}

View File

@@ -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<Location> {
}
@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
}
}