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

Added static wardrobe

This commit is contained in:
HeroBrineGoat
2022-02-06 14:18:00 -05:00
parent 4d632c612a
commit 4b8d2c1517
21 changed files with 275 additions and 40 deletions

View File

@@ -4,8 +4,8 @@
<facet type="minecraft" name="Minecraft">
<configuration>
<autoDetectTypes>
<platformType>MCP</platformType>
<platformType>SPIGOT</platformType>
<platformType>MCP</platformType>
</autoDetectTypes>
</configuration>
</facet>

View File

@@ -106,6 +106,10 @@ bukkit {
default = BukkitPluginDescription.Permission.Default.OP
description = "Permission to set other users' cosmetics."
}
register("hmccosmetics.cmd.wardrobe.portable") {
default = BukkitPluginDescription.Permission.Default.OP
description = "Permission to use a portable wardrobe"
}
register("hmccosmetics.cmd.wardrobe") {
default = BukkitPluginDescription.Permission.Default.OP
description = "Permission to view the wardrobe"

View File

@@ -141,14 +141,14 @@ public class HMCCosmetics extends JavaPlugin {
}
public void load() {
Bukkit.getScheduler().runTaskAsynchronously(this,
Bukkit.getScheduler().runTaskLaterAsynchronously(this,
() -> {
this.settings.load();
this.messageHandler.load();
this.cosmeticsMenu.load();
Translation.getInstance().load();
this.database.load();
});
}, 1);
}
public void reload() {

View File

@@ -1,6 +1,8 @@
package io.github.fisher2911.hmccosmetics.command;
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
import io.github.fisher2911.hmccosmetics.config.Settings;
import io.github.fisher2911.hmccosmetics.config.WardrobeSettings;
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
import io.github.fisher2911.hmccosmetics.gui.CosmeticsMenu;
import io.github.fisher2911.hmccosmetics.message.Message;
@@ -33,17 +35,35 @@ public class CosmeticsCommand extends CommandBase {
private final UserManager userManager;
private final MessageHandler messageHandler;
private final CosmeticsMenu cosmeticsMenu;
private final Settings settings;
public CosmeticsCommand(final HMCCosmetics plugin) {
this.plugin = plugin;
this.userManager = this.plugin.getUserManager();
this.messageHandler = this.plugin.getMessageHandler();
this.cosmeticsMenu = this.plugin.getCosmeticsMenu();
this.settings = this.plugin.getSettings();
}
@Default
@Permission(io.github.fisher2911.hmccosmetics.message.Permission.DEFAULT_COMMAND)
public void defaultCommand(final Player player) {
final Optional<User> optionalUser = this.userManager.get(player.getUniqueId());
if (optionalUser.isEmpty()) {
this.cosmeticsMenu.openDefault(player);
return;
}
final User user = optionalUser.get();
final Wardrobe wardrobe = user.getWardrobe();
if (wardrobe.isActive() &&
!this.settings.getWardrobeSettings().inDistanceOfWardrobe(wardrobe.getCurrentLocation(), player.getLocation())) {
wardrobe.setActive(false);
wardrobe.despawnFakePlayer(player);
this.messageHandler.sendMessage(
player,
Messages.CLOSED_WARDROBE
);
}
this.cosmeticsMenu.openDefault(player);
}
@@ -193,7 +213,7 @@ public class CosmeticsCommand extends CommandBase {
}
@SubCommand("wardrobe")
@Permission(io.github.fisher2911.hmccosmetics.message.Permission.VIEW_WARDROBE)
@Permission(io.github.fisher2911.hmccosmetics.message.Permission.WARDROBE)
public void openWardrobe(final Player player) {
final Optional<User> optionalUser = this.plugin.getUserManager().get(player.getUniqueId());
if (optionalUser.isEmpty()) return;
@@ -209,11 +229,36 @@ public class CosmeticsCommand extends CommandBase {
return;
}
final WardrobeSettings settings = this.settings.getWardrobeSettings();
final boolean inDistanceOfStatic = settings.inDistanceOfStatic(player.getLocation());
if (!settings.isPortable() && !inDistanceOfStatic) {
this.messageHandler.sendMessage(
player,
Messages.NOT_NEAR_WARDROBE
);
return;
}
if (settings.isPortable() && !inDistanceOfStatic) {
if (!player.hasPermission(io.github.fisher2911.hmccosmetics.message.Permission.PORTABLE_WARDROBE)) {
this.messageHandler.sendMessage(
player,
Messages.CANNOT_USE_PORTABLE_WARDROBE
);
return;
}
wardrobe.setCurrentLocation(null);
}
wardrobe.setActive(true);
Bukkit.getScheduler().runTaskAsynchronously(
this.plugin,
() -> wardrobe.spawnFakePlayer(player, this.plugin)
() -> wardrobe.spawnFakePlayer(player)
);
this.cosmeticsMenu.openDefault(player);
this.messageHandler.sendMessage(
player,

View File

@@ -5,21 +5,27 @@ import io.github.fisher2911.hmccosmetics.HMCCosmetics;
public class Settings {
private final HMCCosmetics plugin;
private final CosmeticSettings settings;
private final CosmeticSettings cosmeticSettings;
private final WardrobeSettings wardrobeSettings;
public Settings(final HMCCosmetics plugin) {
this.plugin = plugin;
this.settings = new CosmeticSettings();
this.cosmeticSettings = new CosmeticSettings();
this.wardrobeSettings = new WardrobeSettings(this.plugin);
}
public void load() {
this.plugin.saveDefaultConfig();
this.plugin.reloadConfig();
this.settings.load(this.plugin.getConfig());
this.cosmeticSettings.load(this.plugin.getConfig());
this.wardrobeSettings.load();
}
public CosmeticSettings getCosmeticSettings() {
return settings;
return cosmeticSettings;
}
public WardrobeSettings getWardrobeSettings() {
return wardrobeSettings;
}
}

View File

@@ -0,0 +1,104 @@
package io.github.fisher2911.hmccosmetics.config;
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.checkerframework.checker.units.qual.A;
import org.jetbrains.annotations.Nullable;
import org.bukkit.Location;
public class WardrobeSettings {
private static final String WARDROBE_PATH = "wardrobe";
private static final String DISABLE_ON_DAMAGE_PATH = WARDROBE_PATH + ".disable-on-damage";
private static final String DISPLAY_RADIUS_PATH = WARDROBE_PATH + ".display-radius";
private static final String PORTABLE_PATH = WARDROBE_PATH + ".portable";
private static final String ALWAYS_DISPLAY_PATH = WARDROBE_PATH + ".always-display";
private static final String STATIC_RADIUS_PATH = WARDROBE_PATH + ".static-radius";
private static final String STATIC_LOCATION_PATH = WARDROBE_PATH + ".wardrobe-location";
private static final String WORLD_PATH = "world";
private static final String X_PATH = "x";
private static final String Y_PATH = "y";
private static final String Z_PATH = "z";
private static final String YAW_PATH = "yaw";
private static final String PITCH_PATH = "pitch";
private final HMCCosmetics plugin;
private boolean disableOnDamage;
private int displayRadius;
private boolean portable;
private boolean alwaysDisplay;
private int staticRadius;
private Location location;
public WardrobeSettings(final HMCCosmetics plugin) {
this.plugin = plugin;
}
public void load() {
final FileConfiguration config = this.plugin.getConfig();
this.disableOnDamage = config.getBoolean(DISABLE_ON_DAMAGE_PATH);
this.displayRadius = config.getInt(DISPLAY_RADIUS_PATH);
this.portable = config.getBoolean(PORTABLE_PATH);
this.staticRadius = config.getInt(STATIC_RADIUS_PATH);
this.alwaysDisplay = config.getBoolean(ALWAYS_DISPLAY_PATH);
final ConfigurationSection locationSection = config.getConfigurationSection(STATIC_LOCATION_PATH);
if (locationSection == null) return;
this.location = this.loadLocation(locationSection);
}
@Nullable
private Location loadLocation(final ConfigurationSection section) {
final String worldName = section.getString(WORLD_PATH);
final int x = section.getInt(X_PATH);
final int y = section.getInt(Y_PATH);
final int z = section.getInt(Z_PATH);
final float yaw = (float) section.getDouble(YAW_PATH);
final float pitch = (float) section.getDouble(PITCH_PATH);
if (worldName == null || worldName.isBlank()) return null;
final World world = Bukkit.getWorld(worldName);
if (world == null) return null;
return new Location(world, x, y, z, yaw, pitch);
}
public boolean getDisableOnDamage() {
return disableOnDamage;
}
public int getDisplayRadius() {
return displayRadius;
}
public boolean isPortable() {
return portable;
}
public boolean isAlwaysDisplay() {
return alwaysDisplay;
}
public int getStaticRadius() {
return staticRadius;
}
public Location getLocation() {
return location.clone();
}
public boolean inDistanceOfWardrobe(final Location wardrobeLocation, final Location playerLocation) {
if (this.displayRadius == -1) return true;
return playerLocation.distanceSquared(wardrobeLocation) <= this.displayRadius * this.displayRadius;
}
public boolean inDistanceOfStatic(final Location location) {
if (this.location == null) return false;
if (this.staticRadius == -1) return false;
return this.location.distanceSquared(location) <= this.staticRadius * this.staticRadius;
}
}

View File

@@ -106,7 +106,6 @@ public class Database {
final User user = new User(uuid, PlayerArmor.empty(), wardrobe, armorStandId);
this.plugin.getUserManager().add(user);
onComplete.accept(user);
}
public void saveUser(final User user) {
@@ -158,6 +157,7 @@ public class Database {
public Wardrobe createNewWardrobe(final UUID ownerUUID) {
return new Wardrobe(
this.plugin,
UUID.randomUUID(),
ownerUUID,
PlayerArmor.empty(),

View File

@@ -107,12 +107,10 @@ public class CosmeticGui {
this.gui.updateItem(slot, guiItem);
}
public void open(final User user) {
final Player player = user.getPlayer();
if (player == null) return;
public void open(final User user, final Player player) {
this.gui = Gui.gui().
title(Adventure.MINI_MESSAGE.deserialize(
Placeholder.applyPapiPlaceholders(user.getPlayer(), this.title))).
Placeholder.applyPapiPlaceholders(player, this.title))).
rows(this.rows).
create();

View File

@@ -43,6 +43,7 @@ public class CosmeticsMenu {
}
public void openMenu(final String id, final HumanEntity humanEntity) {
if (!(humanEntity instanceof final Player player)) return;
final CosmeticGui cosmeticGui = this.getGui(id);
final Optional<User> optionalUser = this.plugin.getUserManager().get(humanEntity.getUniqueId());
@@ -59,9 +60,7 @@ public class CosmeticsMenu {
return;
}
if (cosmeticGui != null) {
cosmeticGui.open(user);
}
if (cosmeticGui != null) cosmeticGui.open(user, player);
}
public void openDefault(final HumanEntity humanEntity) {

View File

@@ -176,9 +176,7 @@ public class DyeSelectorGui extends CosmeticGui {
}
@Override
public void open(final User user) {
final Player player = user.getPlayer();
if (player == null) return;
public void open(final User user, final Player player) {
this.getGui(user, user.getLastSetItem().getType()).open(player);
}

View File

@@ -61,21 +61,21 @@ public class PAPIExpansion extends PlaceholderExpansion {
if (parts.length < 2) return null;
final String id = this.getId(parts, 1);
for (final ArmorItem item : user.getPlayerArmor().getArmorItems()) {
if (item.getId().equals(id)) return Translation.translate("true");
if (item.getId().equals(id)) return Translation.translate(Translation.TRUE);
}
return Translation.translate("false");
return Translation.translate(Translation.FALSE);
}
// %hmccosmetics_current_type%
if (parts[0].equals("current")) {
if (parts.length < 2) {
final String typeStr = parts[1];
if (parts.length >= 2) {
final String typeStr = getId(parts, 1);
try {
final ArmorItem.Type type = ArmorItem.Type.valueOf(typeStr);
final ArmorItem.Type type = ArmorItem.Type.valueOf(typeStr.toUpperCase());
for (final ArmorItem item : user.getPlayerArmor().getArmorItems()) {
if (item.getType().equals(type)) return item.getId();
}
return null;
return Translation.translate(Translation.NONE);
} catch (final IllegalArgumentException exception) {
return null;
}
@@ -86,9 +86,10 @@ public class PAPIExpansion extends PlaceholderExpansion {
}
private String getId(final String[] parts, final int fromIndex) {
final StringBuilder builder = new StringBuilder(parts[fromIndex]);
for (int i = fromIndex + 1; i < parts.length; i++) {
final StringBuilder builder = new StringBuilder();
for (int i = fromIndex; i < parts.length; i++) {
builder.append(parts[i]);
if (i < parts.length - 1) builder.append("_");
}
return builder.toString();

View File

@@ -62,4 +62,10 @@ public class PlayerArmor {
return new PlayerArmor(new HashMap<>(this.armorItems));
}
public void clear() {
for (final ArmorItem.Type type : ArmorItem.Type.values()) {
this.setItem(ArmorItem.empty(type));
}
}
}

View File

@@ -44,8 +44,7 @@ public class CosmeticFixListener implements Listener {
@EventHandler
public void onRightClick(final PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK
&& event.getHand() != EquipmentSlot.OFF_HAND) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
final Player player = event.getPlayer();

View File

@@ -1,6 +1,7 @@
package io.github.fisher2911.hmccosmetics.listener;
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
import io.github.fisher2911.hmccosmetics.config.WardrobeSettings;
import io.github.fisher2911.hmccosmetics.database.Database;
import io.github.fisher2911.hmccosmetics.user.User;
import io.github.fisher2911.hmccosmetics.user.UserManager;
@@ -31,7 +32,15 @@ public class JoinListener implements Listener {
final Player player = event.getPlayer();
this.database.loadUser(player.getUniqueId(),
user -> Bukkit.getScheduler().runTaskAsynchronously(this.plugin,
() -> this.userManager.resendCosmetics(player)));
() -> {
this.userManager.resendCosmetics(player);
final WardrobeSettings settings = this.plugin.getSettings().getWardrobeSettings();
if (settings.isAlwaysDisplay() && settings.getLocation() != null) {
final Wardrobe wardrobe = user.getWardrobe();
wardrobe.setCurrentLocation(settings.getLocation());
wardrobe.spawnFakePlayer(player);
}
}));
}
@EventHandler

View File

@@ -56,6 +56,10 @@ public class Messages {
new Message("closed-wardrobe", ChatColor.GREEN + "Closing wardrobe!");
public static final Message WARDROBE_ALREADY_OPEN =
new Message("wardrobe-already-open", ChatColor.RED + "The wardrobe is already open!");
public static final Message NOT_NEAR_WARDROBE =
new Message("not-near-wardrobe", ChatColor.RED + "You are not near the wardrobe!");
public static final Message CANNOT_USE_PORTABLE_WARDROBE =
new Message("cannot-use-portable-wardrobe", ChatColor.RED + "You cannot use the portable wardrobe!");
public static final Message SET_OTHER_BACKPACK = new Message(
"set-other-backpack", ChatColor.GREEN + "You have set the backpack of " +

View File

@@ -7,6 +7,7 @@ public class Permission {
public static final String RELOAD_COMMAND = "hmccosmetics.cmd.reload";
public static final String HELP_COMMAND = "hmccosmetics.cmd.help";
public static final String SET_COSMETIC_COMMAND = "hmccosmetics.cmd.set";
public static final String VIEW_WARDROBE = "hmccosmetics.cmd.wardrobe";
public static final String PORTABLE_WARDROBE = "hmccosmetics.cmd.wardrobe.portable";
public static final String WARDROBE = "hmccosmetics.cmd.wardrobe";
}

View File

@@ -10,6 +10,10 @@ import org.bukkit.configuration.file.YamlConfiguration;
public class Translation {
public static final String TRUE = "true";
public static final String FALSE = "false";
public static final String NONE = "none";
private static final Translation INSTANCE;
private static final String FILE_NAME = "translations.yml";
private static final String TRANSLATION_PATH = "translations";

View File

@@ -14,6 +14,7 @@ import io.github.fisher2911.hmccosmetics.inventory.PlayerArmor;
import io.github.fisher2911.hmccosmetics.message.Message;
import io.github.fisher2911.hmccosmetics.message.MessageHandler;
import io.github.fisher2911.hmccosmetics.message.Placeholder;
import io.github.fisher2911.hmccosmetics.message.Translation;
import io.github.fisher2911.hmccosmetics.packet.PacketManager;
import io.github.fisher2911.hmccosmetics.util.builder.ItemBuilder;
import org.bukkit.Bukkit;
@@ -167,8 +168,8 @@ public class UserManager {
if (hidden) return new ItemStack(Material.AIR);
final CosmeticSettings cosmeticSettings = this.settings.getCosmeticSettings();
final Map<String, String> placeholders = Map.of(Placeholder.ALLOWED, "true",
Placeholder.ENABLED, "true");
final Map<String, String> placeholders = Map.of(Placeholder.ALLOWED, Translation.TRUE,
Placeholder.ENABLED, Translation.TRUE);
ItemStack itemStack = ItemBuilder.from(armorItem.getColored()).
namePlaceholders(placeholders).

View File

@@ -3,6 +3,7 @@ package io.github.fisher2911.hmccosmetics.user;
import com.comphenix.protocol.events.PacketContainer;
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
import io.github.fisher2911.hmccosmetics.config.Settings;
import io.github.fisher2911.hmccosmetics.config.WardrobeSettings;
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
import io.github.fisher2911.hmccosmetics.inventory.PlayerArmor;
import io.github.fisher2911.hmccosmetics.packet.PacketManager;
@@ -11,17 +12,22 @@ import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import java.io.BufferedReader;
import java.util.UUID;
public class Wardrobe extends User {
private final HMCCosmetics plugin;
private final UUID ownerUUID;
private final int entityId;
private boolean active;
private boolean spawned;
private Location currentLocation;
public Wardrobe(
final HMCCosmetics plugin,
final UUID uuid,
final UUID ownerUUID,
final PlayerArmor playerArmor,
@@ -29,16 +35,25 @@ public class Wardrobe extends User {
final int entityId,
final boolean active) {
super(uuid, playerArmor, armorStandId);
this.plugin = plugin;
this.ownerUUID = ownerUUID;
this.entityId = entityId;
this.active = active;
this.wardrobe = this;
}
public void spawnFakePlayer(final Player viewer, final HMCCosmetics plugin) {
public void spawnFakePlayer(final Player viewer) {
final WardrobeSettings settings = this.plugin.getSettings().getWardrobeSettings();
if (settings.inDistanceOfStatic(viewer.getLocation())) {
this.currentLocation = settings.getLocation();
} else if (this.currentLocation == null) {
this.currentLocation = viewer.getLocation().clone();
this.currentLocation.setPitch(0);
this.currentLocation.setYaw(0);
} else if (this.spawned) {
return;
}
final PacketContainer playerSpawnPacket = PacketManager.getFakePlayerSpawnPacket(
this.currentLocation,
this.getUuid(),
@@ -51,6 +66,7 @@ public class Wardrobe extends User {
PacketManager.sendPacket(viewer, playerInfoPacket, playerSpawnPacket);
this.spawnArmorStand(viewer);
this.updateArmorStand(viewer, plugin.getSettings(), this.currentLocation);
this.spawned = true;
}
@Override
@@ -59,9 +75,19 @@ public class Wardrobe extends User {
}
public void despawnFakePlayer(final Player viewer) {
final WardrobeSettings settings = this.plugin.getSettings().getWardrobeSettings();
PacketManager.sendPacket(viewer, PacketManager.getEntityDestroyPacket(this.getEntityId()));
this.despawnAttached();
this.active = false;
this.spawned = false;
this.currentLocation = null;
this.getPlayerArmor().clear();
if (settings.isAlwaysDisplay()) {
this.currentLocation = settings.getLocation();
if (this.currentLocation == null) return;
this.spawnFakePlayer(viewer);
}
}
@Override
@@ -82,10 +108,19 @@ public class Wardrobe extends User {
this.active = active;
}
public void setCurrentLocation(final Location currentLocation) {
this.currentLocation = currentLocation;
}
@Nullable
public Location getCurrentLocation() {
return currentLocation;
}
@Override
@Nullable
public Player getPlayer() {
return null;
return Bukkit.getPlayer(this.ownerUUID);
}
}

View File

@@ -4,3 +4,22 @@ cosmetic-settings:
# The pitch the player must look down for the backpack to be removed
# Set to -1 for no removal
look-down-backpack-remove: 70
wardrobe:
# if true, the wardrobe will be removed when the player is damaged
disable-on-damage: true
# removed if player leaves this radius, set to -1 for infinite radius
display-radius: -1
# if the player can use the wardrobe in other locations
portable: false
# if the wardrobe should always be displayed in the location below
always-display: false
# spawn static wardrobe if in this radius of wardrobe-location
static-radius: 10
# location of static wardrobe, remove for none
wardrobe-location:
world: "World"
x: 0
y: 0
z: 0
yaw: 0
pitch: 0

View File

@@ -17,6 +17,8 @@ set-other-off-hand: "%prefix% <gradient:#6D9DC5:#45CDE9>You have set the off han
opened-wardrobe: "%prefix% <gradient:#6D9DC5:#45CDE9>Viewing wardrobe!"
closed-wardrobe: "%prefix% <gradient:#6D9DC5:#45CDE9>Closed wardrobe!"
wardrobe-already-open: "%prefix% <gradient:#6D9DC5:#45CDE9><red>The wardrobe is already open!"
not-near-wardrobe: "%prefix% <gradient:#6D9DC5:#45CDE9><red>You are not near the wardrobe!"
cannot-use-portable-wardrobe: "%prefix% <gradient:#6D9DC5:#45CDE9><red>You cannot use the portable wardrobe!"
help-command: "<#6D9DC5><st> </st> %prefix% <gradient:#40B7D6:#6D9DC5>HMCCosmetics - Help</gradient> %prefix% <#6D9DC5> <st> </st>