allow editing mannequin description

This commit is contained in:
2025-12-14 18:35:30 +01:00
parent 33e9ed3a5a
commit b4c4386fe4
3 changed files with 45 additions and 0 deletions
@@ -2,6 +2,7 @@ package me.youhavetrouble.standin.converter;
import io.papermc.paper.datacomponent.item.ResolvableProfile; import io.papermc.paper.datacomponent.item.ResolvableProfile;
import me.youhavetrouble.standin.StandIn; import me.youhavetrouble.standin.StandIn;
import me.youhavetrouble.standin.entity.MannequinHandler;
import org.bukkit.entity.ArmorStand; import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Mannequin; import org.bukkit.entity.Mannequin;
@@ -29,6 +30,7 @@ public class ArmorStandToMannequinConverter implements EntityConverter<ArmorStan
return from.getWorld().spawn(from.getLocation(), Mannequin.class, (mannequin -> { return from.getWorld().spawn(from.getLocation(), Mannequin.class, (mannequin -> {
markAsTransformed(mannequin); markAsTransformed(mannequin);
EntityConverter.saveRawEntityName(mannequin, EntityConverter.getRawEntityName(from)); EntityConverter.saveRawEntityName(mannequin, EntityConverter.getRawEntityName(from));
MannequinHandler.saveRawDescription(mannequin, MannequinHandler.getRawDescription(from));
mannequin.customName(from.customName()); mannequin.customName(from.customName());
mannequin.setImmovable(!from.canMove()); mannequin.setImmovable(!from.canMove());
mannequin.setGravity(from.hasGravity()); mannequin.setGravity(from.hasGravity());
@@ -1,6 +1,8 @@
package me.youhavetrouble.standin.converter; package me.youhavetrouble.standin.converter;
import me.youhavetrouble.standin.StandIn; import me.youhavetrouble.standin.StandIn;
import me.youhavetrouble.standin.entity.ArmorStandHandler;
import me.youhavetrouble.standin.entity.MannequinHandler;
import org.bukkit.entity.ArmorStand; import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Mannequin; import org.bukkit.entity.Mannequin;
@@ -42,6 +44,10 @@ public class MannequinToArmorStandConverter implements EntityConverter<Mannequin
if (profileName != null && !profileName.isEmpty()) { if (profileName != null && !profileName.isEmpty()) {
pdc.set(EntityConverter.PLAYER_PROFILE_KEY, PersistentDataType.STRING, profileName); pdc.set(EntityConverter.PLAYER_PROFILE_KEY, PersistentDataType.STRING, profileName);
} }
String description = MannequinHandler.getRawDescription(from);
if (description != null) {
pdc.set(MannequinHandler.DESCRIPTION_KEY, PersistentDataType.STRING, description);
}
})); }));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
StandIn.getPlugin(StandIn.class).getSLF4JLogger().warn("Failed to spawn entity", e); StandIn.getPlugin(StandIn.class).getSLF4JLogger().warn("Failed to spawn entity", e);
@@ -13,11 +13,14 @@ import me.youhavetrouble.standin.converter.MannequinToArmorStandConverter;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickCallback; import net.kyori.adventure.text.event.ClickCallback;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Mannequin; import org.bukkit.entity.Mannequin;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Pose; import org.bukkit.entity.Pose;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.time.Duration; import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
@@ -28,6 +31,8 @@ import java.util.UUID;
@SuppressWarnings("UnstableApiUsage") @SuppressWarnings("UnstableApiUsage")
public class MannequinHandler extends EntityHandler<Mannequin> { public class MannequinHandler extends EntityHandler<Mannequin> {
public static final NamespacedKey DESCRIPTION_KEY = new NamespacedKey("standin", "mannequin_description");
public MannequinHandler() { public MannequinHandler() {
super(Mannequin.class); super(Mannequin.class);
addConverter(new MannequinToArmorStandConverter()); addConverter(new MannequinToArmorStandConverter());
@@ -52,6 +57,18 @@ public class MannequinHandler extends EntityHandler<Mannequin> {
.maxLength(1024) .maxLength(1024)
.build() .build()
); );
String description = getRawDescription(mannequin);
if (description == null) {
description = "";
}
inputs.add(
DialogInput.text("description", Component.text("Description"))
.initial(description)
.maxLength(1024)
.build()
);
String profileName = ""; String profileName = "";
if (mannequin.getProfile().name() != null) { if (mannequin.getProfile().name() != null) {
profileName = mannequin.getProfile().name(); profileName = mannequin.getProfile().name();
@@ -104,6 +121,14 @@ public class MannequinHandler extends EntityHandler<Mannequin> {
mann.customName(null); mann.customName(null);
} }
EntityConverter.saveRawEntityName(mann, newName); EntityConverter.saveRawEntityName(mann, newName);
String newDescription = view.getText("description");
if (newDescription != null && !newDescription.isBlank()) {
mann.setDescription(MiniMessage.miniMessage().deserialize(newDescription));
} else {
mann.setDescription(null);
}
saveRawDescription(mann, newDescription);
mann.setImmovable(Boolean.TRUE.equals(view.getBoolean("immovable"))); mann.setImmovable(Boolean.TRUE.equals(view.getBoolean("immovable")));
mann.setVelocity(mann.getVelocity().zero()); mann.setVelocity(mann.getVelocity().zero());
mann.setGravity(Boolean.TRUE.equals(view.getBoolean("gravity"))); mann.setGravity(Boolean.TRUE.equals(view.getBoolean("gravity")));
@@ -160,4 +185,16 @@ public class MannequinHandler extends EntityHandler<Mannequin> {
); );
} }
public static String getRawDescription(@NotNull Entity entity) {
return entity.getPersistentDataContainer().get(DESCRIPTION_KEY, PersistentDataType.STRING);
}
public static void saveRawDescription(@NotNull Mannequin mannequin, @Nullable String description) {
if (description == null) {
mannequin.getPersistentDataContainer().remove(DESCRIPTION_KEY);
return;
}
mannequin.getPersistentDataContainer().set(DESCRIPTION_KEY, PersistentDataType.STRING, description);
}
} }