mirror of
https://github.com/YouHaveTrouble/Stand-in.git
synced 2026-05-11 22:16:55 +00:00
armor stand to mannequin and back
This commit is contained in:
@@ -4,13 +4,21 @@ import io.papermc.paper.dialog.Dialog;
|
||||
import io.papermc.paper.registry.data.dialog.ActionButton;
|
||||
import io.papermc.paper.registry.data.dialog.DialogBase;
|
||||
import io.papermc.paper.registry.data.dialog.action.DialogAction;
|
||||
import io.papermc.paper.registry.data.dialog.body.DialogBody;
|
||||
import io.papermc.paper.registry.data.dialog.input.DialogInput;
|
||||
import io.papermc.paper.registry.data.dialog.input.SingleOptionDialogInput;
|
||||
import io.papermc.paper.registry.data.dialog.type.DialogType;
|
||||
import me.youhavetrouble.standin.converter.ArmorStandToMannequinConverter;
|
||||
import me.youhavetrouble.standin.converter.EntityConverter;
|
||||
import me.youhavetrouble.standin.converter.MannequinToArmorStandConverter;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.event.ClickCallback;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
import org.bukkit.entity.ArmorStand;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Mannequin;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -19,27 +27,123 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
// TODO generify this mess
|
||||
|
||||
@SuppressWarnings("UnstableApiUsage")
|
||||
public class StandinDialog {
|
||||
|
||||
public static void openArmorStandDialog(@NotNull Player player, @NotNull ArmorStand armorStand) {
|
||||
|
||||
public static void openConversionDialog(@NotNull Player player, @NotNull ArmorStand armorStand) {
|
||||
if (armorStand.isDead()) return;
|
||||
|
||||
UUID armorStandId = armorStand.getUniqueId();
|
||||
UUID playerId = player.getUniqueId();
|
||||
|
||||
List<DialogInput> inputs = List.of(
|
||||
DialogInput.singleOption("entity_type", Component.text("New entity type"), List.of(
|
||||
SingleOptionDialogInput.OptionEntry.create("armor_stand", Component.text("Armor Stand"), true),
|
||||
SingleOptionDialogInput.OptionEntry.create("mannequin", Component.text("Mannequin"), false)
|
||||
)).build()
|
||||
);
|
||||
|
||||
ActionButton saveButton = ActionButton.builder(Component.text("Change type")).action(
|
||||
DialogAction.customClick((view, audience) -> {
|
||||
if (!(audience instanceof Player callbackPlayer)) return;
|
||||
Entity entity = callbackPlayer.getWorld().getEntity(armorStandId);
|
||||
if (playerId != callbackPlayer.getUniqueId()) return;
|
||||
if (!(entity instanceof ArmorStand stand)) return;
|
||||
if (stand.isDead()) return;
|
||||
String newEntityType = view.getText("entity_type");
|
||||
switch (newEntityType) {
|
||||
case "mannequin" -> {
|
||||
EntityConverter<ArmorStand, Mannequin> mannequinConverter = new ArmorStandToMannequinConverter();
|
||||
Mannequin mannequin = mannequinConverter.spawn(stand);
|
||||
if (mannequin == null) {
|
||||
audience.sendMessage(Component.text("Error spawning new entity. Ensure new entity can spawn in this spot.").color(NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
stand.remove();
|
||||
}
|
||||
case null, default -> {
|
||||
}
|
||||
}
|
||||
}, ClickCallback.Options.builder().lifetime(Duration.ofHours(1)).uses(1).build())
|
||||
).build();
|
||||
|
||||
Dialog dialog = Dialog.create(builder -> builder.empty()
|
||||
.base(
|
||||
DialogBase.builder(Component.text("Armor Stand Conversion"))
|
||||
.body(List.of(
|
||||
DialogBody.plainMessage(
|
||||
Component.text("Some settings might not persist between changing entity type").color(NamedTextColor.RED)
|
||||
)
|
||||
)).inputs(inputs)
|
||||
.build())
|
||||
.type(DialogType.confirmation(saveButton, ActionButton.builder(Component.text("Cancel")).build()))
|
||||
);
|
||||
|
||||
player.showDialog(dialog);
|
||||
}
|
||||
|
||||
public static void openConversionDialog(@NotNull Player player, @NotNull Mannequin mannequin) {
|
||||
if (mannequin.isDead()) return;
|
||||
|
||||
UUID MannequinId = mannequin.getUniqueId();
|
||||
UUID playerId = player.getUniqueId();
|
||||
|
||||
List<DialogInput> inputs = List.of(
|
||||
DialogInput.singleOption("entity_type", Component.text("New entity type"), List.of(
|
||||
SingleOptionDialogInput.OptionEntry.create("mannequin", Component.text("Mannequin"), true),
|
||||
SingleOptionDialogInput.OptionEntry.create("armor_stand", Component.text("Armor Stand"), false)
|
||||
)).build()
|
||||
);
|
||||
|
||||
ActionButton saveButton = ActionButton.builder(Component.text("Change type")).action(
|
||||
DialogAction.customClick((view, audience) -> {
|
||||
if (!(audience instanceof Player callbackPlayer)) return;
|
||||
if (playerId != callbackPlayer.getUniqueId()) return;
|
||||
Entity entity = callbackPlayer.getWorld().getEntity(MannequinId);
|
||||
if (!(entity instanceof Mannequin cMannequin)) return;
|
||||
if (cMannequin.isDead()) return;
|
||||
String newEntityType = view.getText("entity_type");
|
||||
switch (newEntityType) {
|
||||
case "armor_stand" -> {
|
||||
EntityConverter<Mannequin, ArmorStand> armorStandConverter = new MannequinToArmorStandConverter();
|
||||
ArmorStand armorStand = armorStandConverter.spawn(cMannequin);
|
||||
if (armorStand == null) {
|
||||
audience.sendMessage(Component.text("Error spawning new entity. Ensure new entity can spawn in this spot.").color(NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
cMannequin.remove();
|
||||
}
|
||||
case null, default -> {
|
||||
}
|
||||
}
|
||||
}, ClickCallback.Options.builder().lifetime(Duration.ofMinutes(5)).uses(1).build())
|
||||
).build();
|
||||
|
||||
Dialog dialog = Dialog.create(builder -> builder.empty()
|
||||
.base(
|
||||
DialogBase.builder(Component.text("Mannequin Conversion"))
|
||||
.body(List.of(
|
||||
DialogBody.plainMessage(
|
||||
Component.text("Some settings might not persist between changing entity type").color(NamedTextColor.RED)
|
||||
)
|
||||
)).inputs(inputs)
|
||||
.build())
|
||||
.type(DialogType.confirmation(saveButton, ActionButton.builder(Component.text("Cancel")).build()))
|
||||
);
|
||||
|
||||
player.showDialog(dialog);
|
||||
}
|
||||
|
||||
public static void openArmorStandDialog(@NotNull Player player, @NotNull ArmorStand armorStand) {
|
||||
if (armorStand.isDead()) return;
|
||||
|
||||
UUID armorStandId = armorStand.getUniqueId();
|
||||
UUID playerId = player.getUniqueId();
|
||||
|
||||
List<DialogInput> inputs = new ArrayList<>();
|
||||
|
||||
String currentName = "";
|
||||
if (armorStand.customName() != null) {
|
||||
currentName = PlainTextComponentSerializer.plainText().serialize(armorStand.name());
|
||||
}
|
||||
|
||||
inputs.add(
|
||||
DialogInput.text("name", Component.text("Display name"))
|
||||
.initial(currentName)
|
||||
.build()
|
||||
);
|
||||
inputs.add(
|
||||
DialogInput.bool("invisible", Component.text("Invisible"))
|
||||
.initial(armorStand.isInvisible())
|
||||
@@ -67,28 +171,106 @@ public class StandinDialog {
|
||||
ActionButton saveButton = ActionButton.builder(Component.text("Save")).action(
|
||||
DialogAction.customClick((view, audience) -> {
|
||||
if (!(audience instanceof Player callbackPlayer)) return;
|
||||
if (playerId != callbackPlayer.getUniqueId()) return;
|
||||
Entity entity = callbackPlayer.getWorld().getEntity(armorStandId);
|
||||
if (!(entity instanceof ArmorStand stand)) return;
|
||||
if (stand.isDead()) return;
|
||||
String customName = view.getText("name");
|
||||
if (customName == null || customName.isEmpty()) {
|
||||
stand.customName(null);
|
||||
} else {
|
||||
stand.customName(Component.text(customName));
|
||||
}
|
||||
stand.setInvisible(Boolean.TRUE.equals(view.getBoolean("invisible")));
|
||||
stand.setBasePlate(Boolean.TRUE.equals(view.getBoolean("basePlate")));
|
||||
stand.setArms(Boolean.TRUE.equals(view.getBoolean("arms")));
|
||||
stand.setSmall(Boolean.TRUE.equals(view.getBoolean("small")));
|
||||
}, ClickCallback.Options.builder().lifetime(Duration.ofHours(1)).uses(1).build())
|
||||
}, ClickCallback.Options.builder().lifetime(Duration.ofMinutes(5)).uses(1).build())
|
||||
).build();
|
||||
|
||||
ActionButton changeTypeButton = ActionButton.builder(Component.text("Change type"))
|
||||
.action(
|
||||
DialogAction.customClick((view, audience) -> {
|
||||
if (!(audience instanceof Player callbackPlayer)) return;
|
||||
if (playerId != callbackPlayer.getUniqueId()) return;
|
||||
Entity entity = callbackPlayer.getWorld().getEntity(armorStandId);
|
||||
if (!(entity instanceof ArmorStand stand)) return;
|
||||
if (stand.isDead()) return;
|
||||
StandinDialog.openConversionDialog(callbackPlayer, armorStand);
|
||||
}, ClickCallback.Options.builder().lifetime(Duration.ofMinutes(5)).uses(1).build()
|
||||
)
|
||||
).build();
|
||||
|
||||
Dialog dialog = Dialog.create(builder -> builder.empty()
|
||||
.base(
|
||||
DialogBase.builder(Component.text("Armor Stand Editor"))
|
||||
.inputs(inputs)
|
||||
.build())
|
||||
.type(DialogType.confirmation(saveButton, ActionButton.builder(Component.text("Cancel")).build()))
|
||||
.build()
|
||||
).type(
|
||||
DialogType.multiAction(
|
||||
List.of(saveButton, changeTypeButton),
|
||||
ActionButton.builder(Component.text("Cancel")).build(),
|
||||
1)
|
||||
)
|
||||
);
|
||||
|
||||
player.showDialog(dialog);
|
||||
}
|
||||
|
||||
public static void openMannequinDialog(@NotNull Player player, @NotNull Mannequin mannequin) {
|
||||
if (mannequin.isDead()) return;
|
||||
|
||||
UUID mannequinId = mannequin.getUniqueId();
|
||||
UUID playerId = player.getUniqueId();
|
||||
List<DialogInput> inputs = new ArrayList<>();
|
||||
|
||||
String name = "";
|
||||
Component customName = mannequin.customName();
|
||||
if (customName != null) {
|
||||
name = PlainTextComponentSerializer.plainText().serialize(customName);
|
||||
}
|
||||
|
||||
inputs.add(
|
||||
DialogInput.text("name", Component.text("Name"))
|
||||
.initial(name)
|
||||
.build()
|
||||
);
|
||||
|
||||
ActionButton saveButton = ActionButton.builder(Component.text("Save")).action(
|
||||
DialogAction.customClick((view, audience) -> {
|
||||
if (!(audience instanceof Player callbackPlayer)) return;
|
||||
if (playerId != callbackPlayer.getUniqueId()) return;
|
||||
Entity entity = callbackPlayer.getWorld().getEntity(mannequinId);
|
||||
if (!(entity instanceof Mannequin cMannequin)) return;
|
||||
if (cMannequin.isDead()) return;
|
||||
String newName = view.getText("name");
|
||||
Component displayName = null;
|
||||
if (newName != null) {
|
||||
displayName = MiniMessage.miniMessage().deserialize(newName);
|
||||
}
|
||||
cMannequin.customName(displayName);
|
||||
|
||||
}, ClickCallback.Options.builder().lifetime(Duration.ofMinutes(5)).uses(1).build())
|
||||
).build();
|
||||
|
||||
ActionButton changeTypeButton = ActionButton.builder(Component.text("Change type"))
|
||||
.action(
|
||||
DialogAction.customClick((view, audience) -> {
|
||||
if (!(audience instanceof Player callbackPlayer)) return;
|
||||
if (playerId != callbackPlayer.getUniqueId()) return;
|
||||
Entity entity = callbackPlayer.getWorld().getEntity(mannequinId);
|
||||
if (!(entity instanceof Mannequin cMannequin)) return;
|
||||
if (cMannequin.isDead()) return;
|
||||
StandinDialog.openConversionDialog(callbackPlayer, cMannequin);
|
||||
}, ClickCallback.Options.builder().lifetime(Duration.ofMinutes(5)).uses(1).build()
|
||||
)
|
||||
).build();
|
||||
|
||||
Dialog dialog = Dialog.create(builder -> builder.empty()
|
||||
.base(
|
||||
DialogBase.builder(Component.text("Armor Stand Editor"))
|
||||
.inputs(inputs)
|
||||
.build()
|
||||
).type(
|
||||
DialogType.multiAction(
|
||||
List.of(saveButton, changeTypeButton),
|
||||
ActionButton.builder(Component.text("Cancel")).build(),
|
||||
1)
|
||||
)
|
||||
);
|
||||
|
||||
player.showDialog(dialog);
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package me.youhavetrouble.standin.converter;
|
||||
|
||||
import me.youhavetrouble.standin.StandIn;
|
||||
import org.bukkit.entity.ArmorStand;
|
||||
import org.bukkit.entity.Mannequin;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ArmorStandToMannequinConverter implements EntityConverter<ArmorStand, Mannequin> {
|
||||
|
||||
@Override
|
||||
public Class<ArmorStand> entityFrom() {
|
||||
return ArmorStand.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Mannequin> entityTo() {
|
||||
return Mannequin.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mannequin spawn(@NotNull ArmorStand from) {
|
||||
try {
|
||||
return from.getWorld().spawn(from.getLocation(), entityTo(), (mannequin -> {
|
||||
for (EquipmentSlot slot : EquipmentSlot.values()) {
|
||||
try {
|
||||
mannequin.getEquipment().setItem(slot, from.getItem(slot));
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
}
|
||||
}));
|
||||
} catch (IllegalArgumentException e) {
|
||||
StandIn.getPlugin(StandIn.class).getSLF4JLogger().warn("Failed to spawn entity", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package me.youhavetrouble.standin.converter;
|
||||
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface EntityConverter<F extends Entity, T extends Entity> {
|
||||
|
||||
Class<F> entityFrom();
|
||||
|
||||
Class<T> entityTo();
|
||||
|
||||
/**
|
||||
* Spawn the new entity in the old entity's spot.
|
||||
* @param from Entity to base the new one from
|
||||
* @return The spawned entity or null if entity cannot be spawned
|
||||
*/
|
||||
T spawn(@NotNull F from);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package me.youhavetrouble.standin.converter;
|
||||
|
||||
import me.youhavetrouble.standin.StandIn;
|
||||
import org.bukkit.entity.ArmorStand;
|
||||
import org.bukkit.entity.Mannequin;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class MannequinToArmorStandConverter implements EntityConverter<Mannequin, ArmorStand> {
|
||||
|
||||
@Override
|
||||
public Class<Mannequin> entityFrom() {
|
||||
return Mannequin.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<ArmorStand> entityTo() {
|
||||
return ArmorStand.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArmorStand spawn(@NotNull Mannequin from) {
|
||||
try {
|
||||
return from.getWorld().spawn(from.getLocation(), entityTo(), (armorStand -> {
|
||||
for (EquipmentSlot slot : EquipmentSlot.values()) {
|
||||
try {
|
||||
armorStand.getEquipment().setItem(slot, from.getEquipment().getItem(slot));
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
}
|
||||
}));
|
||||
} catch (IllegalArgumentException e) {
|
||||
StandIn.getPlugin(StandIn.class).getSLF4JLogger().warn("Failed to spawn entity", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
package me.youhavetrouble.standin.stand;
|
||||
|
||||
import io.papermc.paper.event.player.PlayerPickEntityEvent;
|
||||
import me.youhavetrouble.standin.StandinDialog;
|
||||
import org.bukkit.attribute.Attribute;
|
||||
import org.bukkit.attribute.AttributeInstance;
|
||||
import org.bukkit.entity.ArmorStand;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Mannequin;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
@@ -26,6 +28,11 @@ public class StandinInteractionListener implements Listener {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (entity instanceof Mannequin mannequin && player.hasPermission("standin.edit.mannequin")) {
|
||||
StandinDialog.openMannequinDialog(player, mannequin);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -74,4 +81,22 @@ public class StandinInteractionListener implements Listener {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onInteractWithStands(PlayerPickEntityEvent event) {
|
||||
if (!event.getPlayer().isSneaking()) return;
|
||||
if (event.getEntity() instanceof ArmorStand armorStand) {
|
||||
StandinDialog.openConversionDialog(event.getPlayer(), armorStand);
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// This currently does not work since pick entity does not fire for mannequins
|
||||
// https://github.com/PaperMC/Paper/issues/13340
|
||||
if (event.getEntity() instanceof Mannequin mannequin) {
|
||||
StandinDialog.openConversionDialog(event.getPlayer(), mannequin);
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user