From fea810a5d3a8c01250a7b80f78d12a96b5872c73 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Thu, 26 Mar 2015 23:36:53 +1100 Subject: [PATCH] Added command to set head block --- .../plugins/ArmorStandTools/Commands.java | 53 +++++++++++++++++++ .../plugins/ArmorStandTools/Config.java | 8 ++- .../plugins/ArmorStandTools/MainListener.java | 9 ++-- src/language.yml | 4 ++ src/plugin.yml | 2 +- 5 files changed, 69 insertions(+), 7 deletions(-) diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java index 9e20593..9837334 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java @@ -1,11 +1,18 @@ package com.gmail.St3venAU.plugins.ArmorStandTools; +import net.minecraft.server.v1_8_R1.Material; + import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.entity.ArmorStand; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; +import java.util.List; import java.util.UUID; class Commands implements CommandExecutor { @@ -41,6 +48,52 @@ class Commands implements CommandExecutor { return true; } } + if(args[0].equalsIgnoreCase("head")) { + // get the entity you are looking at + if(!Utils.hasPermissionNode(p, "astools.head")) { + p.sendMessage(ChatColor.RED + Config.noHeadPerm); + return true; + } + List nearby = p.getNearbyEntities(16, 16, 16); + if (nearby.size() == 0) { + p.sendMessage(ChatColor.RED + Config.notTarget); + return true; + } + double min = 0.5; + Entity closest = null; + Vector loc = p.getLocation().toVector(); + for (Entity entity : nearby) { + Vector eLoc = entity.getLocation().toVector().subtract(loc).normalize(); + Vector target = p.getLocation().getDirection().normalize(); + double diff = eLoc.subtract(target).length(); + if (diff < min) { + min = diff; + closest = entity; + } + } + if (closest == null || !(closest instanceof ArmorStand)) { + p.sendMessage(ChatColor.RED + Config.notTarget); + return true; + } + ArmorStand as = (ArmorStand) closest; + if (!MainListener.checkPermission(p, closest.getLocation().getBlock())) { + p.sendMessage(ChatColor.RED + Config.wgNoPerm); + return true; + } + ItemStack item = p.getItemInHand(); + if (item == null || item.getTypeId() == 0) { + as.setHelmet(new ItemStack(0)); + p.sendMessage(ChatColor.GREEN + Config.setHead); + return true; + } + if (!item.getType().isBlock()) { + p.sendMessage(ChatColor.RED + Config.notBlock); + return true; + } + as.setHelmet(item); + p.sendMessage(ChatColor.GREEN + Config.setHead); + return true; + } if(args[0].equalsIgnoreCase("reload")) { if(Utils.hasPermissionNode(p, "astools.reload")) { Config.reload(); diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java index 5c7b35b..7d18567 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java @@ -34,9 +34,9 @@ class Config { asCloned, carrying, noPerm, cbCreated, size, small, normal, basePlate, isOn, isOff, gravity, arms, invul, equip, locked, unLocked, notConsole, giveMsg1, - giveMsg2, conReload, noRelPerm, noAirError, + giveMsg2, conReload, noRelPerm, noHeadPerm, notTarget, notBlock, noAirError, pleaseWait, appliedHead, noHead, invalidName, - wgNoPerm; + wgNoPerm, setHead; public static void reload(Main main) { plugin = main; @@ -74,12 +74,16 @@ class Config { giveMsg2 = languageConfig.getString("giveMsg2"); conReload = languageConfig.getString("conReload"); noRelPerm = languageConfig.getString("noRelPerm"); + noHeadPerm = languageConfig.getString("noHeadPerm"); + notTarget = languageConfig.getString("notTarget"); + notBlock = languageConfig.getString("notBlock"); noAirError = languageConfig.getString("noAirError"); pleaseWait = languageConfig.getString("pleaseWait"); appliedHead = languageConfig.getString("appliedHead"); noHead = languageConfig.getString("noHead"); invalidName = languageConfig.getString("invalidName"); wgNoPerm = languageConfig.getString("wgNoPerm"); + setHead = languageConfig.getString("setHead"); } private static void reloadMainConfig() { diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java index 8b484da..651920d 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java @@ -212,8 +212,9 @@ public class MainListener implements Listener { p.sendMessage(ChatColor.GREEN + "Deletion Protection: Enabled"); } break; - default: + default: { cancel = tool == ArmorStandTool.SUMMON || tool == ArmorStandTool.SAVE || event.isCancelled(); + } } event.setCancelled(cancel); } @@ -570,7 +571,7 @@ public class MainListener implements Listener { b.setMetadata("armorStand", new FixedMetadataValue(plugin, as.getUniqueId())); b.setMetadata("setSkull", new FixedMetadataValue(plugin, true)); } - + private ArmorStand getArmorStand(Block b) { UUID uuid = null; for (MetadataValue value : b.getMetadata("armorStand")) { @@ -589,7 +590,7 @@ public class MainListener implements Listener { return null; } - public boolean checkPermission(Player player, Block block) { + public static boolean checkPermission(Player player, Block block) { // Check PlotSquared Location loc = block.getLocation(); @@ -623,7 +624,7 @@ public class MainListener implements Listener { return hasperm; } - boolean playerHasPermission(Player p, Block b, ArmorStandTool tool) { + public static boolean playerHasPermission(Player p, Block b, ArmorStandTool tool) { if(tool != null && !p.isOp() && (!Utils.hasPermissionNode(p, "astools.use") || (ArmorStandTool.SAVE == tool && !Utils.hasPermissionNode(p, "astools.cmdblock")) || (ArmorStandTool.CLONE == tool && !Utils.hasPermissionNode(p, "astools.clone")))) { diff --git a/src/language.yml b/src/language.yml index ad8561c..82763af 100644 --- a/src/language.yml +++ b/src/language.yml @@ -42,12 +42,16 @@ giveMsg1: 'Given armor stand tools. L-click any tool to cycle through tools.' giveMsg2: 'Run this command again to return your inventory.' conReload: 'Armor Stand Tools config reloaded' noRelPerm: 'You do not have permission to reload the plugin' +noHeadPerm: 'You do not have permission to set a head' +notTarget: 'You are not looking at an ArmorStand' noAirError: 'Error: Failed to find a near-by air block. Move and try again.' pleaseWait: 'Please wait, this may take a few seconds...' appliedHead: 'Applied the head of player' noHead: 'No player head found for player' invalidName: 'is not a valid minecraft username' wgNoPerm: 'You do not have permission to alter an armor stand in this region' +notBlock: 'You are not holding a block' +setHead: 'Set the head block' # ############################# # Tool names & descriptions # diff --git a/src/plugin.yml b/src/plugin.yml index 1132278..0cc163a 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -8,4 +8,4 @@ commands: astools: description: Give yourself all of the armor stand tools (Warning; clears your inventory). aliases: ast - usage: Usage /astools or /astools reload \ No newline at end of file + usage: Usage /astools or /astools reload or /astools head \ No newline at end of file