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 all commits
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 @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Player;
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 +104,19 @@ public void setCopySlot(byte slot){
public void editArmorStand(ArmorStand armorStand) {
if(!getPlayer().hasPermission("asedit.basic")) return;
armorStand = attemptTarget(armorStand);

switch(eMode){
case LOCK: toggleLock(armorStand);
return;
case COPY: copy(armorStand);
return;
}

if (plugin.isLocked(armorStand)) {
sendLockedMessage(getPlayer(), armorStand);
return;
}

switch(eMode){
case LEFTARM: armorStand.setLeftArmPose(subEulerAngle(armorStand.getLeftArmPose()));
break;
Expand All @@ -125,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);
Expand Down Expand Up @@ -160,6 +173,19 @@ private void openEquipment(ArmorStand armorStand) {
public void reverseEditArmorStand(ArmorStand armorStand){
if(!getPlayer().hasPermission("asedit.basic")) return;
armorStand = attemptTarget(armorStand);

switch(eMode){
case LOCK: toggleLock(armorStand);
return;
case COPY: copy(armorStand);
return;
}

if (plugin.isLocked(armorStand)) {
sendLockedMessage(getPlayer(), armorStand);
return;
}

switch(eMode){
case LEFTARM: armorStand.setLeftArmPose(addEulerAngle(armorStand.getLeftArmPose()));
break;
Expand Down Expand Up @@ -289,6 +315,19 @@ void cycleAxis(int i) {
setAxis(Axis.values()[index]);
}

void toggleLock(ArmorStand armorStand){
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);
}
}

private EulerAngle addEulerAngle(EulerAngle angle) {
switch(axis){
case X: angle = angle.setX(Util.addAngle(angle.getX(), eulerAngleChange));
Expand Down Expand Up @@ -377,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,18 @@ void onArmorStandInteract(PlayerInteractAtEntityEvent event){
} else {
name = null;
}


if (plugin.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
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