From f54a9ec7165409cb0d4bc330d9eb5d90e1695b4c Mon Sep 17 00:00:00 2001 From: daashton Date: Fri, 18 Jun 2021 09:01:14 -0400 Subject: [PATCH 1/2] Added basic functionality for locking the armor stand from being edited. --- .../armorstandeditor/PlayerEditor.java | 51 +++++++++++++++++++ .../armorstandeditor/PlayerEditorManager.java | 21 +++++++- .../rypofalem/armorstandeditor/menu/Menu.java | 6 ++- .../armorstandeditor/modes/EditMode.java | 2 +- src/main/resources/lang/en_US.yml | 12 ++++- 5 files changed, 87 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditor.java b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditor.java index 82b565dd..416a60a9 100644 --- a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditor.java +++ b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditor.java @@ -28,15 +28,21 @@ import io.github.rypofalem.armorstandeditor.modes.EditMode; import java.util.ArrayList; +import java.util.Set; import java.util.UUID; +import static jdk.nashorn.internal.objects.NativeObject.keys; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; +import org.bukkit.NamespacedKey; +import org.bukkit.Sound; import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Player; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.bukkit.util.EulerAngle; @@ -102,6 +108,18 @@ public void setCopySlot(byte slot){ public void editArmorStand(ArmorStand armorStand) { if(!getPlayer().hasPermission("asedit.basic")) return; armorStand = attemptTarget(armorStand); + + if (eMode == EditMode.LOCK) { + toggleLock(armorStand); + return; + } + + if (isLocked(armorStand)) { + sendMessage("locked", null); + getPlayer().playSound(getPlayer().getLocation(), Sound.BLOCK_CHEST_LOCKED, 1, 1); + return; + } + switch(eMode){ case LEFTARM: armorStand.setLeftArmPose(subEulerAngle(armorStand.getLeftArmPose())); break; @@ -160,6 +178,18 @@ private void openEquipment(ArmorStand armorStand) { public void reverseEditArmorStand(ArmorStand armorStand){ if(!getPlayer().hasPermission("asedit.basic")) return; armorStand = attemptTarget(armorStand); + + if (eMode == EditMode.LOCK) { + toggleLock(armorStand); + return; + } + + if (isLocked(armorStand)) { + sendMessage("locked", null); + getPlayer().playSound(getPlayer().getLocation(), Sound.BLOCK_CHEST_LOCKED, 1, 1); + return; + } + switch(eMode){ case LEFTARM: armorStand.setLeftArmPose(addEulerAngle(armorStand.getLeftArmPose())); break; @@ -289,6 +319,27 @@ void cycleAxis(int i) { setAxis(Axis.values()[index]); } + void toggleLock(ArmorStand armorStand){ + NamespacedKey key = new NamespacedKey(plugin, "locked"); + PersistentDataContainer container = armorStand.getPersistentDataContainer(); + if (container.has(key, PersistentDataType.BYTE)) { + container.remove(key); + sendMessage("setlock", "unlocked"); + getPlayer().playSound(getPlayer().getLocation(), Sound.BLOCK_CHEST_OPEN, 1, 1); + } + else { + container.set(key, PersistentDataType.BYTE, (byte)1); + sendMessage("setlock", "locked"); + getPlayer().playSound(getPlayer().getLocation(), Sound.BLOCK_CHEST_CLOSE, 1, 1); + } + } + + boolean isLocked(ArmorStand armorStand) { + NamespacedKey key = new NamespacedKey(plugin, "locked"); + PersistentDataContainer container = armorStand.getPersistentDataContainer(); + return container.has(key, PersistentDataType.BYTE); + } + private EulerAngle addEulerAngle(EulerAngle angle) { switch(axis){ case X: angle = angle.setX(Util.addAngle(angle.getX(), eulerAngleChange)); diff --git a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java index 27b2e4ca..d7d3042e 100644 --- a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java +++ b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java @@ -39,6 +39,8 @@ import java.util.HashMap; import java.util.List; import java.util.UUID; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.plugin.Plugin; //Manages PlayerEditors and Player Events related to editing armorstands public class PlayerEditorManager implements Listener{ @@ -106,7 +108,18 @@ void onArmorStandInteract(PlayerInteractAtEntityEvent event){ } else { name = null; } - + + if (isLocked(as)) { + event.setCancelled(true); + String asName = as.getCustomName(); + boolean asNameVisible = as.isCustomNameVisible(); + Bukkit.getScheduler().runTaskLater(plugin, () -> { + as.setCustomName(asName); + as.setCustomNameVisible(asNameVisible); + }, 1); + return; + } + if(name == null){ as.setCustomName(null); as.setCustomNameVisible(false); @@ -196,6 +209,12 @@ boolean canEdit(Player player, ArmorStand as){ ignoreNextInteract = false; return true; } + + boolean isLocked(ArmorStand armorStand) { + NamespacedKey key = new NamespacedKey(plugin, "locked"); + PersistentDataContainer container = armorStand.getPersistentDataContainer(); + return container.has(key, PersistentDataType.BYTE); + } void applyLeftTool(Player player, ArmorStand as){ getPlayerEditor(player.getUniqueId()).cancelOpenMenu(); diff --git a/src/main/java/io/github/rypofalem/armorstandeditor/menu/Menu.java b/src/main/java/io/github/rypofalem/armorstandeditor/menu/Menu.java index d20a48c3..007649b9 100644 --- a/src/main/java/io/github/rypofalem/armorstandeditor/menu/Menu.java +++ b/src/main/java/io/github/rypofalem/armorstandeditor/menu/Menu.java @@ -52,7 +52,7 @@ private void fillInventory() { ItemStack xAxis= null, yAxis= null, zAxis= null, coarseAdj= null, fineAdj= null, rotate = null, place = null, headPos= null, rightArmPos= null, bodyPos= null, leftArmPos= null, reset = null, showArms= null, visibility= null, size= null, rightLegPos= null, equipment = null, leftLegPos= null, disableSlots = null, gravity= null, plate= null, copy= null, paste= null, - slot1= null, slot2= null, slot3= null, slot4= null, help= null; + slot1= null, slot2= null, slot3= null, slot4= null, help= null, lock = null; xAxis = createIcon(new ItemStack(Material.RED_WOOL, 1), "xaxis", "axis x"); @@ -112,6 +112,8 @@ private void fillInventory() { disableSlots = createIcon(new ItemStack(Material.BARRIER), "disableslots", "mode disableslots"); } + lock = createIcon( new ItemStack(Material.TRIPWIRE_HOOK), "lock", "mode lock"); + gravity = createIcon( new ItemStack(Material.SAND), "gravity", "mode gravity"); plate = createIcon( new ItemStack(Material.STONE_SLAB, 1), @@ -151,7 +153,7 @@ private void fillInventory() { xAxis, yAxis, zAxis, null, coarseAdj, fineAdj, null, rotate, place, null, headPos, null, null, null, null, null, null, null, rightArmPos, bodyPos, leftArmPos, reset, null, null, showArms, visibility, size, - rightLegPos, equipment, leftLegPos, null, null, null, null, gravity, plate, + rightLegPos, equipment, leftLegPos, null, null, null, lock, gravity, plate, null, copy, paste, null, null, null, null, null, null, slot1, slot2, slot3, slot4, null, null, null, null, help }; diff --git a/src/main/java/io/github/rypofalem/armorstandeditor/modes/EditMode.java b/src/main/java/io/github/rypofalem/armorstandeditor/modes/EditMode.java index da6d38c2..aadbfacb 100644 --- a/src/main/java/io/github/rypofalem/armorstandeditor/modes/EditMode.java +++ b/src/main/java/io/github/rypofalem/armorstandeditor/modes/EditMode.java @@ -22,7 +22,7 @@ public enum EditMode { NONE("None"), INVISIBLE("Invisible"), SHOWARMS("ShowArms"), GRAVITY("Gravity"), BASEPLATE("BasePlate"), SIZE("Size"), COPY("Copy"), PASTE("Paste"), HEAD("Head"), BODY("Body"), LEFTARM("LeftArm"), RIGHTARM("RightArm"), LEFTLEG("LeftLeg"), RIGHTLEG("RightLeg"), - PLACEMENT("Placement"), DISABLESLOTS("DisableSlots"), ROTATE("Rotate"), EQUIPMENT("Equipment"), RESET("Reset"); + PLACEMENT("Placement"), DISABLESLOTS("DisableSlots"), ROTATE("Rotate"), EQUIPMENT("Equipment"), RESET("Reset"), LOCK("Lock"); private String name; diff --git a/src/main/resources/lang/en_US.yml b/src/main/resources/lang/en_US.yml index 1fffc8bd..e099905c 100644 --- a/src/main/resources/lang/en_US.yml +++ b/src/main/resources/lang/en_US.yml @@ -57,6 +57,10 @@ target: msg: ArmorStand target locked. notarget: msg: ArmorStand target unlocked. +setlock: + msg: This Armor Stand is now + locked: locked + unlocked: unlocked help: msg: "1. Hold the editing tool() in your main hand @@ -91,7 +95,9 @@ noaxiscom: msg: You must specify an Axis! nomodecom: msg: You must specify a Mode! - +locked: + msg: This armor stand is locked! + #menutitle mainmenutitle: msg: Armor Stand Editor Menu @@ -199,6 +205,10 @@ helpgui: msg: Help! description: msg: Click here to get help! +lock: + msg: Lock + description: + msg: Prevent the armorstand from being edited #icons (equipment menu) disabled: From 8f32424601caeb5e2f70a92d13301d08d05fdc20 Mon Sep 17 00:00:00 2001 From: David Ashton Date: Mon, 19 Jul 2021 09:16:29 -0400 Subject: [PATCH 2/2] Refactored, allowed copying while locked --- .../ArmorStandEditorPlugin.java | 13 ++++ .../armorstandeditor/PlayerEditor.java | 65 +++++++++---------- .../armorstandeditor/PlayerEditorManager.java | 10 +-- 3 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/main/java/io/github/rypofalem/armorstandeditor/ArmorStandEditorPlugin.java b/src/main/java/io/github/rypofalem/armorstandeditor/ArmorStandEditorPlugin.java index 1727343e..9f611382 100644 --- a/src/main/java/io/github/rypofalem/armorstandeditor/ArmorStandEditorPlugin.java +++ b/src/main/java/io/github/rypofalem/armorstandeditor/ArmorStandEditorPlugin.java @@ -27,6 +27,8 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.entity.ArmorStand; +import org.bukkit.persistence.PersistentDataType; import java.io.File; @@ -46,6 +48,7 @@ public class ArmorStandEditorPlugin extends JavaPlugin{ boolean debug = false; //weather or not to broadcast messages via print(String message) double coarseRot; double fineRot; + private NamespacedKey lockedKey; public ArmorStandEditorPlugin(){ instance = this; @@ -146,11 +149,21 @@ public boolean isEditTool(ItemStack item){ } return true; } + + public boolean isLocked(ArmorStand armorStand) { + return armorStand.getPersistentDataContainer().has(this.getLockedKey(), PersistentDataType.BYTE); + } public NamespacedKey getIconKey() { if(iconKey == null) iconKey = new NamespacedKey(this, "command_icon"); return iconKey; } + + public NamespacedKey getLockedKey() { + if (lockedKey == null) lockedKey = new NamespacedKey(this, "locked"); + return lockedKey; + } + } //todo: //Access to "DisabledSlots" data (probably simplified just a toggle enable/disable) diff --git a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditor.java b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditor.java index 416a60a9..bbdf7f2f 100644 --- a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditor.java +++ b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditor.java @@ -28,20 +28,16 @@ import io.github.rypofalem.armorstandeditor.modes.EditMode; import java.util.ArrayList; -import java.util.Set; import java.util.UUID; -import static jdk.nashorn.internal.objects.NativeObject.keys; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; -import org.bukkit.NamespacedKey; import org.bukkit.Sound; import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Player; -import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; @@ -109,14 +105,15 @@ public void editArmorStand(ArmorStand armorStand) { if(!getPlayer().hasPermission("asedit.basic")) return; armorStand = attemptTarget(armorStand); - if (eMode == EditMode.LOCK) { - toggleLock(armorStand); - return; + switch(eMode){ + case LOCK: toggleLock(armorStand); + return; + case COPY: copy(armorStand); + return; } - if (isLocked(armorStand)) { - sendMessage("locked", null); - getPlayer().playSound(getPlayer().getLocation(), Sound.BLOCK_CHEST_LOCKED, 1, 1); + if (plugin.isLocked(armorStand)) { + sendLockedMessage(getPlayer(), armorStand); return; } @@ -143,8 +140,6 @@ public void editArmorStand(ArmorStand armorStand) { break; case GRAVITY: toggleGravity(armorStand); break; - case COPY: copy(armorStand); - break; case PASTE: paste(armorStand); break; case PLACEMENT: move(armorStand); @@ -179,14 +174,15 @@ public void reverseEditArmorStand(ArmorStand armorStand){ if(!getPlayer().hasPermission("asedit.basic")) return; armorStand = attemptTarget(armorStand); - if (eMode == EditMode.LOCK) { - toggleLock(armorStand); - return; + switch(eMode){ + case LOCK: toggleLock(armorStand); + return; + case COPY: copy(armorStand); + return; } - if (isLocked(armorStand)) { - sendMessage("locked", null); - getPlayer().playSound(getPlayer().getLocation(), Sound.BLOCK_CHEST_LOCKED, 1, 1); + if (plugin.isLocked(armorStand)) { + sendLockedMessage(getPlayer(), armorStand); return; } @@ -320,25 +316,17 @@ void cycleAxis(int i) { } void toggleLock(ArmorStand armorStand){ - NamespacedKey key = new NamespacedKey(plugin, "locked"); - PersistentDataContainer container = armorStand.getPersistentDataContainer(); - if (container.has(key, PersistentDataType.BYTE)) { - container.remove(key); - sendMessage("setlock", "unlocked"); - getPlayer().playSound(getPlayer().getLocation(), Sound.BLOCK_CHEST_OPEN, 1, 1); - } - else { - container.set(key, PersistentDataType.BYTE, (byte)1); - sendMessage("setlock", "locked"); - getPlayer().playSound(getPlayer().getLocation(), Sound.BLOCK_CHEST_CLOSE, 1, 1); - } + if (plugin.isLocked(armorStand)) { + armorStand.getPersistentDataContainer().remove(plugin.getLockedKey()); + sendMessage("setlock", "unlocked"); + getPlayer().playSound(armorStand.getLocation(), Sound.BLOCK_CHEST_OPEN, 1, 1); + } + else { + armorStand.getPersistentDataContainer().set(plugin.getLockedKey(), PersistentDataType.BYTE, (byte)1); + sendMessage("setlock", "locked"); + getPlayer().playSound(armorStand.getLocation(), Sound.BLOCK_CHEST_CLOSE, 1, 1); + } } - - boolean isLocked(ArmorStand armorStand) { - NamespacedKey key = new NamespacedKey(plugin, "locked"); - PersistentDataContainer container = armorStand.getPersistentDataContainer(); - return container.has(key, PersistentDataType.BYTE); - } private EulerAngle addEulerAngle(EulerAngle angle) { switch(axis){ @@ -428,6 +416,11 @@ void sendMessage(String path, String format, String option){ void sendMessage(String path, String option){ sendMessage(path, "info", option); } + + void sendLockedMessage (Player player, ArmorStand armorStand) { + sendMessage("locked", null); + getPlayer().playSound(armorStand.getLocation(), Sound.BLOCK_CHEST_LOCKED, 1, 1); + } private void highlight(ArmorStand armorStand){ armorStand.removePotionEffect(PotionEffectType.GLOWING); diff --git a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java index d7d3042e..c25f4a47 100644 --- a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java +++ b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java @@ -39,8 +39,6 @@ import java.util.HashMap; import java.util.List; import java.util.UUID; -import org.bukkit.persistence.PersistentDataContainer; -import org.bukkit.plugin.Plugin; //Manages PlayerEditors and Player Events related to editing armorstands public class PlayerEditorManager implements Listener{ @@ -109,7 +107,7 @@ void onArmorStandInteract(PlayerInteractAtEntityEvent event){ name = null; } - if (isLocked(as)) { + if (plugin.isLocked(as)) { event.setCancelled(true); String asName = as.getCustomName(); boolean asNameVisible = as.isCustomNameVisible(); @@ -209,12 +207,6 @@ boolean canEdit(Player player, ArmorStand as){ ignoreNextInteract = false; return true; } - - boolean isLocked(ArmorStand armorStand) { - NamespacedKey key = new NamespacedKey(plugin, "locked"); - PersistentDataContainer container = armorStand.getPersistentDataContainer(); - return container.has(key, PersistentDataType.BYTE); - } void applyLeftTool(Player player, ArmorStand as){ getPlayerEditor(player.getUniqueId()).cancelOpenMenu();