diff --git a/resources/config.yml b/resources/config.yml index b67b0ca7..c644930f 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -16,4 +16,7 @@ requireToolLore: false toolLore: Let's get dangerous #Name of the language file you wish to use -lang: en_US.yml \ No newline at end of file +lang: en_US.yml + +#Don't set to true unless you want players to see random messages or other undesirable behavior +debug: false \ No newline at end of file diff --git a/resources/lang/en_US.yml b/resources/lang/en_US.yml index fbb7f2a6..5a2f570a 100644 --- a/resources/lang/en_US.yml +++ b/resources/lang/en_US.yml @@ -29,7 +29,6 @@ setmode: baseplate: Toggle BasePlate placement: Placement rotate: Rotate - target: Target copy: Copy paste: Paste reset: Reset Pose @@ -54,6 +53,10 @@ copied: msg: ArmorStand state copied to slot . pasted: msg: ArmorStand state pasted from slot . +target: + msg: ArmorStand target locked. +notarget: + msg: ArmorStand target unlocked. #warn cantedit: @@ -153,8 +156,6 @@ rotate: msg: Rotate description: msg: Rotate the entire armorstand -target: - msg: Target description: msg: Coming soon! copy: diff --git a/src/io/github/rypofalem/armorstandeditor/ArmorStandEditorPlugin.java b/src/io/github/rypofalem/armorstandeditor/ArmorStandEditorPlugin.java index 0388a44c..230eec18 100644 --- a/src/io/github/rypofalem/armorstandeditor/ArmorStandEditorPlugin.java +++ b/src/io/github/rypofalem/armorstandeditor/ArmorStandEditorPlugin.java @@ -43,6 +43,7 @@ public void onEnable(){ if(requireToolData) editToolData = getConfig().getInt("toolData"); requireToolLore = getConfig().getBoolean("requireToolLore"); if(requireToolLore) editToolLore= getConfig().getString("toolLore"); + debug = getConfig().getBoolean("debug"); editorManager = new PlayerEditorManager(this); execute = new CommandEx(this); diff --git a/src/io/github/rypofalem/armorstandeditor/PlayerEditor.java b/src/io/github/rypofalem/armorstandeditor/PlayerEditor.java index 4e80109c..1bfaaaaa 100644 --- a/src/io/github/rypofalem/armorstandeditor/PlayerEditor.java +++ b/src/io/github/rypofalem/armorstandeditor/PlayerEditor.java @@ -8,12 +8,15 @@ import io.github.rypofalem.armorstandeditor.modes.CopySlots; import io.github.rypofalem.armorstandeditor.modes.EditMode; +import java.util.ArrayList; import java.util.UUID; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Player; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; import org.bukkit.util.EulerAngle; public class PlayerEditor { @@ -28,6 +31,8 @@ public class PlayerEditor { double movChange; Menu chestMenu; ArmorStand target; + ArrayList targetList = null; + int targetIndex = 0; EquipmentMenu equipMenu; long lastCancelled = 0; @@ -73,6 +78,7 @@ public void setCopySlot(byte slot){ } public void editArmorStand(ArmorStand armorStand) { + armorStand = attemptTarget(armorStand); switch(eMode){ case LEFTARM: armorStand.setLeftArmPose(subEulerAngle(armorStand.getLeftArmPose())); break; @@ -129,6 +135,7 @@ private void openEquipment(ArmorStand armorStand) { } public void reverseEditArmorStand(ArmorStand armorStand){ + armorStand = attemptTarget(armorStand); switch(eMode){ case LEFTARM: armorStand.setLeftArmPose(addEulerAngle(armorStand.getLeftArmPose())); break; @@ -285,8 +292,45 @@ private EulerAngle subEulerAngle(EulerAngle angle) { return angle; } - public void setTarget(ArmorStand armorstand){ - this.target = armorstand; + public void setTarget(ArrayList armorStands){ + if(armorStands == null || armorStands.isEmpty()){ + target = null; + targetList = null; + sendMessage("notarget", null); + return; + } + + if(targetList == null){ + targetList = armorStands; + targetIndex = 0; + sendMessage("target", null); + } else{ + boolean same = targetList.size() == armorStands.size(); + if(same) for(ArmorStand as : armorStands){ + same = targetList.contains(as); + if(!same) break; + } + + if(same){ + targetIndex = ++targetIndex % targetList.size(); + }else{ + targetList = armorStands; + targetIndex = 0; + sendMessage("target", null); + } + } + plugin.print(targetIndex + ""); + target = targetList.get(targetIndex); + glow(target); + } + + ArmorStand attemptTarget(ArmorStand armorStand){ + if(target == null) return armorStand; + if(target.getWorld() != getPlayer().getWorld()) return armorStand; + if(target.getLocation().distanceSquared(getPlayer().getLocation()) > 100) return armorStand; + armorStand = target; + glow(armorStand); + return armorStand; } void sendMessage(String path, String option){ @@ -294,6 +338,11 @@ void sendMessage(String path, String option){ plugin.getServer().getPlayer(getUUID()).sendMessage(message); } + private void glow(ArmorStand armorStand){ + armorStand.removePotionEffect(PotionEffectType.GLOWING); + armorStand.addPotionEffect(new PotionEffect(PotionEffectType.GLOWING, 15, 1, false, false)); + } + public PlayerEditorManager getManager(){ return plugin.editorManager; } @@ -306,17 +355,6 @@ public UUID getUUID() { return uuid; } - // boolean canBuild(ArmorStand armorstand) { - // for(ASEProtection prot : plugin.getProtections()){ - // if(!prot.canEdit(getPlayer(), armorstand)) return false; - // } - // return true; - // } - -// private void cannotBuildMessage(){ -// getPlayer().sendMessage(plugin.getLang().getMessage("cantedit", "warn")); -// } - public void openMenu() { if(!isMenuCancelled()){ plugin.getServer().getScheduler().runTaskLater(plugin, new OpenMenuTask(), 1).getTaskId(); diff --git a/src/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java b/src/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java index b710e96a..db9be4f2 100644 --- a/src/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java +++ b/src/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java @@ -6,14 +6,18 @@ import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.UUID; import net.md_5.bungee.api.ChatColor; import org.bukkit.Bukkit; import org.bukkit.GameMode; +import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.entity.ArmorStand; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; @@ -24,13 +28,10 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.event.player.PlayerInteractAtEntityEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerItemHeldEvent; -import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.*; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; //Manages PlayerEditors and Player Events related to editing armorstands public class PlayerEditorManager implements Listener{ @@ -114,6 +115,48 @@ void onArmorStandInteract(PlayerInteractAtEntityEvent event){ }// end rename } + @EventHandler (priority = EventPriority.LOW, ignoreCancelled=true) + public void onSwitchHands(PlayerSwapHandItemsEvent event){ + if(!plugin.isEditTool(event.getOffHandItem())) return; //event assumes they are already switched + event.setCancelled(true); + Player player = event.getPlayer(); + getPlayerEditor(event.getPlayer().getUniqueId()).setTarget(getTargets(player)); + } + + ArrayList getTargets(Player player){ + Location eyeLaser = player.getEyeLocation(); + Vector direction = player.getLocation().getDirection(); + ArrayList armorStands = new ArrayList<>(); + + final double STEPSIZE = .5; + final Vector STEP = direction.multiply(STEPSIZE); + final double RANGE = 10; + final double LASERRADIUS = .3; + List nearbyEntities = player.getNearbyEntities(RANGE, RANGE, RANGE); + if(nearbyEntities == null || nearbyEntities.isEmpty()) return null; + + for(double i = 0; i nearby = (List) player.getWorld().getNearbyEntities(eyeLaser, LASERRADIUS, LASERRADIUS, LASERRADIUS); + if(!nearby.isEmpty()){ + boolean endLoop = false; + for(Entity e : nearby){ + if(e instanceof ArmorStand){ + if(canEdit(player, (ArmorStand)e)){ + armorStands.add((ArmorStand)e); + endLoop = true; + } + } + } + if(endLoop) break; + + } + if(eyeLaser.getBlock().getType().isSolid()) break; + eyeLaser.add(STEP); + } + + return armorStands; + } + boolean canEdit(Player player, ArmorStand as){ ignoreNextInteract = true; ArrayList events = new ArrayList(); diff --git a/src/io/github/rypofalem/armorstandeditor/language/Language.java b/src/io/github/rypofalem/armorstandeditor/language/Language.java index 4b0e4177..8b8f8ee7 100644 --- a/src/io/github/rypofalem/armorstandeditor/language/Language.java +++ b/src/io/github/rypofalem/armorstandeditor/language/Language.java @@ -21,7 +21,6 @@ public class Language { ArmorStandEditorPlugin plugin; public Language(String langFileName, ArmorStandEditorPlugin plugin) { - if (langFileName == null) langFileName = DEFAULTLANG; this.plugin = plugin; reloadLang(langFileName); } diff --git a/src/io/github/rypofalem/armorstandeditor/menu/Menu.java b/src/io/github/rypofalem/armorstandeditor/menu/Menu.java index b9fb7271..925c8ddc 100644 --- a/src/io/github/rypofalem/armorstandeditor/menu/Menu.java +++ b/src/io/github/rypofalem/armorstandeditor/menu/Menu.java @@ -178,7 +178,7 @@ private void fillInventory() { } ItemStack[] items = {xAxis, yAxis, zAxis, null, coarseAdj, fineAdj, null, rotate, place, - null, headPos, null, null, null, null, null, null, + 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, null, null, null, null, copy, paste, null, null, null,