Skip to content
This repository was archived by the owner on Jul 19, 2021. It is now read-only.

Lock editing of individual armor stands #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see it being used or even needed?


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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -289,6 +319,27 @@ void cycleAxis(int i) {
setAxis(Axis.values()[index]);
}

void toggleLock(ArmorStand armorStand){
NamespacedKey key = new NamespacedKey(plugin, "locked");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't recreate NamespacedKey - it's good to keep it in one place

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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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);
Comment on lines +112 to +117
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this trying to do? It seems to do nothing, unless I'm missing some Bukkit quirk?

return;
}

if(name == null){
as.setCustomName(null);
as.setCustomNameVisible(false);
Expand Down Expand Up @@ -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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if duplicating this is needed. Can't the one used above be reused?


void applyLeftTool(Player player, ArmorStand as){
getPlayerEditor(player.getUniqueId()).cancelOpenMenu();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 11 additions & 1 deletion src/main/resources/lang/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ target:
msg: ArmorStand target locked.
notarget:
msg: ArmorStand target unlocked.
setlock:
msg: This Armor Stand is now <x>
locked: locked
unlocked: unlocked
help:
msg: "1. Hold the editing tool(<x>) in your main hand

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down