Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

new npc equip command, works great, code is awful

  • Loading branch information...
commit 5603904529328be0bbabca84133b9ae3eab5d168 1 parent 9c6d10c
Alex Puncochar authored
View
4 CHANGELOG
@@ -64,7 +64,7 @@ By fullwall and aPunch
--- v1.1 ---
-Citizens is now modular! You can download the full package or a customized ZIP file at
www.citizensnpcs.net/download
--added new NPC types: Guards, Questers, Alchemists; see wiki for detailed documentation
+-added 2 new NPC types: Guards and Questers; see wiki for detailed documentation
-item economy removed; now if 'economy.use-economy' is true and an economy plugin is detected, Citizens will use
that plugin, otherwise everything is free
-now supports MultiCurrency, BOSEconomy7, and iConomy6
@@ -87,6 +87,8 @@ By fullwall and aPunch
-'/npc remove all' command no longer requires an NPC to be selected to execute
-added 'citizens.admin.override.setowner' and 'citizens.admin.override.remove' nodes, see wiki for more info
-fixed '/npc moveto' command requiring all arguments, now only requires x, y, and z coords to work
+-removed '/npc item' and '/npc armor' commands in favor of new '/npc equip' command that toggles item-edit
+ mode
API CHANGES (for developers, the API page on our Wiki for more information):
-added CitizensEnableEvent and CitizensDisableEvent for when Citizens is enabled/disabled
-added NPCRemoveEvent, called when an NPC is manually or naturally removed from a world
View
3  plugin.yml
@@ -138,8 +138,7 @@ permissions:
citizens.basic.modify.settext: true
citizens.basic.modify.addtext: true
citizens.basic.modify.resettext: true
- citizens.basic.modify.armor: true
- citizens.basic.modify.item: true
+ citizens.basic.modify.equip: true
citizens.basic.modify.lookat: true
citizens.basic.modify.talkclose: true
citizens.basic.modify.setowner: true
View
99 src/core/net/citizensnpcs/commands/BasicCommands.java
@@ -1,7 +1,6 @@
package net.citizensnpcs.commands;
import java.util.ArrayDeque;
-import java.util.List;
import net.citizensnpcs.Citizens;
import net.citizensnpcs.PermissionManager;
@@ -26,7 +25,6 @@
import net.citizensnpcs.resources.sk89q.CommandRequirements;
import net.citizensnpcs.resources.sk89q.ServerCommand;
import net.citizensnpcs.utils.HelpUtils;
-import net.citizensnpcs.utils.InventoryUtils;
import net.citizensnpcs.utils.MessageUtils;
import net.citizensnpcs.utils.Messaging;
import net.citizensnpcs.utils.ServerUtils;
@@ -35,13 +33,11 @@
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
-import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.entity.CraftLivingEntity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
-import org.bukkit.inventory.ItemStack;
@CommandRequirements(requireSelected = true, requireOwnership = true)
public class BasicCommands extends CommandHandler {
@@ -123,6 +119,7 @@ public static void reload(CommandContext args, CommandSender sender,
+ "] Reloading....");
UtilityProperties.initialize();
+ PropertyManager.saveState();
PropertyManager.loadAll();
SettingsManager.setupVariables();
@@ -550,71 +547,38 @@ public static void reset(CommandContext args, Player player, HumanNPC npc) {
@Command(
aliases = "npc",
- usage = "item [item]",
- desc = "set the item in an NPC's hand",
- modifiers = "item",
- min = 2,
- max = 2)
- @CommandPermissions("basic.modify.item")
- public static void item(CommandContext args, Player player, HumanNPC npc) {
- NPCDataManager.setItemInHand(player, npc, args.getString(1));
- }
-
- @Command(
- aliases = "npc",
- usage = "armor [armor] [item]",
- desc = "set the armor of an NPC",
- modifiers = "armor",
- min = 3,
- max = 3)
- @CommandPermissions("basic.modify.armor")
- public static void armor(CommandContext args, Player player, HumanNPC npc) {
- Material mat = StringUtils.parseMaterial(args.getString(2));
- if (mat == null) {
- player.sendMessage(ChatColor.RED + "Invalid item.");
- return;
- }
- if (mat != Material.AIR && !player.getInventory().contains(mat)) {
- player.sendMessage(ChatColor.RED
- + "You need to have the item in your inventory to add it to the NPC.");
- return;
- }
- if ((mat.getId() < 298 || mat.getId() > 317)
- && (mat.getId() != 86 && mat.getId() != 91)) {
- player.sendMessage(ChatColor.GRAY
- + "That can't be used as an armor material.");
- return;
- }
- int slot = player.getInventory().first(mat);
- ItemStack item = InventoryUtils.decreaseItemStack(player.getInventory()
- .getItem(slot));
- player.getInventory().setItem(slot, item);
- List<Integer> items = npc.getNPCData().getItems();
-
- if (args.getString(1).contains("helm")) {
- items.set(1, mat.getId());
- } else if (args.getString(1).equalsIgnoreCase("torso")) {
- items.set(2, mat.getId());
- } else if (args.getString(1).contains("leg")) {
- items.set(3, mat.getId());
- } else if (args.getString(1).contains("boot")) {
- items.set(4, mat.getId());
+ usage = "equip",
+ desc = "toggle equip mode",
+ modifiers = "equip",
+ min = 1,
+ max = 1)
+ @CommandPermissions("basic.modify.equip")
+ public static void equip(CommandContext args, Player player, HumanNPC npc) {
+ Integer editing = NPCDataManager.armorEditors.get(player.getName());
+ int UID = npc.getUID();
+ if (editing == null) {
+ player.sendMessage(ChatColor.GREEN
+ + StringUtils.listify(StringUtils.wrap("Now Editing "
+ + npc.getStrippedName() + "'s Items")));
+ player.sendMessage(StringUtils.wrap("Right-click")
+ + " to set an NPC's armor to the item in your hand.");
+ player.sendMessage(ChatColor.GREEN
+ + "Hold nothing in your hand to remove "
+ + StringUtils.wrap("all") + " items.");
+ player.sendMessage(StringUtils.wrap("Repeat")
+ + " the command to exit item-edit mode.");
+ editing = UID;
+ } else if (editing == UID) {
+ player.sendMessage(StringUtils.wrap("Exited") + " item-edit mode.");
+ editing = null;
+ } else if (editing != UID) {
+ player.sendMessage(ChatColor.GRAY + "Now editing "
+ + StringUtils.wrap(npc.getStrippedName()) + "'s items.");
+ editing = UID;
}
- npc.getNPCData().setItems(items);
- NPCDataManager.addItems(npc, items);
-
- // Despawn the old NPC, register our new one.
- NPCManager.removeForRespawn(npc.getUID());
- NPCManager.register(npc.getUID(), npc.getOwner(),
- NPCCreateReason.RESPAWN);
-
- player.sendMessage(StringUtils.wrap(npc.getStrippedName())
- + "'s armor was set to "
- + StringUtils.wrap(MessageUtils.getMaterialName(mat.getId()))
- + ".");
+ NPCDataManager.armorEditors.put(player.getName(), editing);
}
- @CommandRequirements(requireSelected = true, requireOwnership = true)
@Command(
aliases = "npc",
usage = "tp",
@@ -815,8 +779,7 @@ public void addPermissions() {
CitizensManager.addPermission("basic.modify.addtext");
CitizensManager.addPermission("basic.modify.resettext");
CitizensManager.addPermission("basic.modify.settext");
- CitizensManager.addPermission("basic.modify.item");
- CitizensManager.addPermission("basic.modify.armor");
+ CitizensManager.addPermission("basic.modify.equip");
CitizensManager.addPermission("basic.use.teleport");
CitizensManager.addPermission("basic.modify.talkclose");
CitizensManager.addPermission("basic.modify.lookat");
View
1  src/core/net/citizensnpcs/listeners/EntityListen.java
@@ -102,6 +102,7 @@ public void onEntityTarget(EntityTargetEvent event) {
player);
Bukkit.getServer().getPluginManager().callEvent(rightClickEvent);
if (!rightClickEvent.isCancelled()) {
+ NPCDataManager.handleEquip(rightClickEvent);
if (npc.getWaypoints().isStarted()
&& npc.getWaypoints().current() != null) {
npc.getWaypoints().scheduleDelay(npc,
View
184 src/core/net/citizensnpcs/npcs/NPCDataManager.java
@@ -7,9 +7,11 @@
import net.citizensnpcs.SettingsManager;
import net.citizensnpcs.api.event.npc.NPCCreateEvent.NPCCreateReason;
+import net.citizensnpcs.api.event.npc.NPCRightClickEvent;
import net.citizensnpcs.resources.npclib.HumanNPC;
import net.citizensnpcs.utils.InventoryUtils;
import net.citizensnpcs.utils.MessageUtils;
+import net.citizensnpcs.utils.Messaging;
import net.citizensnpcs.utils.StringUtils;
import net.citizensnpcs.waypoints.Waypoint;
@@ -19,17 +21,132 @@
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.PlayerInventory;
+import com.google.common.collect.Lists;
import com.google.common.collect.MapMaker;
import com.google.common.collect.Maps;
public class NPCDataManager {
public static final Map<String, Integer> pathEditors = Maps.newHashMap();
+ public static final Map<String, Integer> armorEditors = Maps.newHashMap();
public static final Map<Integer, Deque<String>> NPCTexts = new MapMaker()
.makeMap();
public static final Map<String, Integer> selectedNPCs = new MapMaker()
.makeMap();
+ public static void handleEquip(NPCRightClickEvent event) {
+ Player player = event.getPlayer();
+ if (armorEditors.get(player.getName()) != null) {
+ HumanNPC npc = NPCManager.get(armorEditors.get(player.getName()));
+ if (npc == null) {
+ armorEditors.remove(player.getName());
+ player.sendMessage(ChatColor.GRAY
+ + "Something went wrong (NPC is dead?).");
+ return;
+ }
+ equip(player, npc);
+ }
+ }
+
+ // equip an NPC based on a player's item-in-hand
+ // TODO needs to be cleaned up badly
+ @SuppressWarnings("deprecation")
+ private static void equip(Player player, HumanNPC npc) {
+ ItemStack hand = player.getItemInHand();
+ PlayerInventory inv = player.getInventory();
+ PlayerInventory npcInv = npc.getInventory();
+ ItemStack npcHelmet = npcInv.getHelmet();
+ ItemStack npcChestplate = npcInv.getChestplate();
+ ItemStack npcLeggings = npcInv.getLeggings();
+ ItemStack npcBoots = npcInv.getBoots();
+ ItemStack npcHand = npcInv.getItemInHand();
+ if (player.getItemInHand().getType() == Material.AIR) {
+ if (npcHelmet.getType() != Material.AIR) {
+ inv.addItem(npcHelmet);
+ }
+ if (npcChestplate.getType() != Material.AIR) {
+ inv.addItem(npcChestplate);
+ }
+ if (npcLeggings.getType() != Material.AIR) {
+ inv.addItem(npcLeggings);
+ }
+ if (npcBoots.getType() != Material.AIR) {
+ inv.addItem(npcBoots);
+ }
+ if (npcHand.getType() != Material.AIR) {
+ inv.addItem(npcHand);
+ }
+ player.updateInventory();
+ npcInv.setItemInHand(null);
+ npcInv.setArmorContents(null);
+ npc.getNPCData().setItems(Lists.newArrayList(0, 0, 0, 0, 0));
+
+ player.sendMessage(StringUtils.wrap(npc.getStrippedName())
+ + " is now naked. Here are the items!");
+
+ NPCManager.removeForRespawn(npc.getUID());
+ NPCManager.register(npc.getUID(), npc.getOwner(),
+ NPCCreateReason.RESPAWN);
+ return;
+ }
+ int itemID = hand.getTypeId();
+ ItemStack equip = new ItemStack(itemID, 1);
+ String slot = "";
+ String error = npc.getStrippedName() + " is already holding "
+ + MessageUtils.getMaterialName(itemID) + ".";
+ if (InventoryUtils.isHelmet(itemID)) {
+ if (npcHelmet.getType() == Material.getMaterial(itemID)) {
+ Messaging.sendError(player, error);
+ return;
+ }
+ slot = "helmet";
+ npcInv.setHelmet(equip);
+ } else if (InventoryUtils.isChestplate(itemID)) {
+ if (npcChestplate.getType() == Material.getMaterial(itemID)) {
+ Messaging.sendError(player, error);
+ return;
+ }
+ slot = "chestplate";
+ npcInv.setChestplate(equip);
+ } else if (InventoryUtils.isLeggings(itemID)) {
+ if (npcLeggings.getType() == Material.getMaterial(itemID)) {
+ Messaging.sendError(player, error);
+ return;
+ }
+ slot = "leggings";
+ npcInv.setLeggings(equip);
+ } else if (InventoryUtils.isBoots(itemID)) {
+ if (npcBoots.getType() == Material.getMaterial(itemID)) {
+ Messaging.sendError(player, error);
+ return;
+ }
+ slot = "boots";
+ npcInv.setBoots(equip);
+ } else {
+ if (npcHand.getType() == Material.getMaterial(itemID)) {
+ Messaging.sendError(player, error);
+ return;
+ }
+ npcInv.setItemInHand(equip);
+ slot = "item-in-hand";
+ }
+ InventoryUtils.decreaseItemInHand(player);
+ npc.getNPCData().setItems(
+ Lists.newArrayList(npcInv.getItemInHand().getTypeId(), npcInv
+ .getHelmet().getTypeId(), npcInv.getChestplate()
+ .getTypeId(), npcInv.getLeggings().getTypeId(), npcInv
+ .getBoots().getTypeId()));
+
+ NPCManager.removeForRespawn(npc.getUID());
+ NPCManager.register(npc.getUID(), npc.getOwner(),
+ NPCCreateReason.RESPAWN);
+
+ player.sendMessage(StringUtils.wrap(npc.getStrippedName() + "'s ")
+ + slot + " was set to "
+ + StringUtils.wrap(MessageUtils.getMaterialName(itemID)) + ".");
+ }
+
public static void handlePathEditor(PlayerInteractEvent event) {
String name = event.getPlayer().getName();
if (pathEditors.get(name) != null) {
@@ -107,80 +224,33 @@ public static void addItems(HumanNPC npc, List<Integer> items) {
Material matLegs = Material.getMaterial(items.get(3));
Material matBoots = Material.getMaterial(items.get(4));
+ PlayerInventory npcInv = npc.getInventory();
+
// TODO: reduce the long if-tree.
if (matHelm != null && matHelm != Material.AIR) {
- npc.getInventory().setHelmet(new ItemStack(matHelm, 1));
+ npcInv.setHelmet(new ItemStack(matHelm, 1));
} else {
- npc.getInventory().setHelmet(null);
+ npcInv.setHelmet(null);
}
if (matBoots != null && matBoots != Material.AIR) {
- npc.getInventory().setBoots(new ItemStack(matBoots, 1));
+ npcInv.setBoots(new ItemStack(matBoots, 1));
} else {
- npc.getInventory().setBoots(null);
+ npcInv.setBoots(null);
}
if (matLegs != null && matLegs != Material.AIR) {
- npc.getInventory().setLeggings(new ItemStack(matLegs, 1));
+ npcInv.setLeggings(new ItemStack(matLegs, 1));
} else {
- npc.getInventory().setLeggings(null);
+ npcInv.setLeggings(null);
}
if (matTorso != null && matTorso != Material.AIR) {
- npc.getInventory().setChestplate(new ItemStack(matTorso, 1));
+ npcInv.setChestplate(new ItemStack(matTorso, 1));
} else {
- npc.getInventory().setChestplate(null);
+ npcInv.setChestplate(null);
}
npc.getNPCData().setItems(items);
}
}
- // Sets the in-hand item of an npc.
- public static void setItemInHand(Player player, HumanNPC npc,
- String material) {
- Material mat = StringUtils.parseMaterial(material);
- if (mat == null) {
- player.sendMessage(ChatColor.RED + "Incorrect item name.");
- return;
- }
- if (mat != Material.AIR && !player.getInventory().contains(mat)) {
- player.sendMessage(ChatColor.RED
- + "You need to have at least 1 of the item in your inventory to add it to the NPC.");
- return;
- }
- if (npc.isType("trader")) {
- player.sendMessage(ChatColor.GRAY
- + "That NPC is a trader. Please put the item manually in the first slot of the trader's inventory instead.");
- return;
- }
- int slot = player.getInventory().first(mat);
- ItemStack item = InventoryUtils.decreaseItemStack(player.getInventory()
- .getItem(slot));
- player.getInventory().setItem(slot, item);
-
- List<Integer> items = npc.getNPCData().getItems();
-
- int olditem = items.get(0);
- items.set(0, mat.getId());
-
- npc.getNPCData().setItems(items);
-
- if (mat != null && mat != Material.AIR) {
- npc.getInventory().setItem(0, new ItemStack(mat, 1));
- } else {
- npc.getInventory().setItem(0, null);
- }
-
- NPCDataManager.addItems(npc, items);
-
- if ((olditem != 0 && items.get(0) == 0)) {
- NPCManager.removeForRespawn(npc.getUID());
- NPCManager.register(npc.getUID(), npc.getOwner(),
- NPCCreateReason.RESPAWN);
- }
- player.sendMessage(StringUtils.wrap(npc.getStrippedName())
- + "'s in-hand item was set to "
- + StringUtils.wrap(MessageUtils.getMaterialName(mat.getId()))
- + ".");
- }
-
// Adds to an npc's text.
public static void addText(int UID, String text) {
Deque<String> texts = NPCDataManager.getText(UID);
View
49 src/core/net/citizensnpcs/utils/InventoryUtils.java
@@ -10,12 +10,7 @@
public class InventoryUtils {
- /**
- * Uses craftbukkit methods to show a player an npc's inventory screen.
- *
- * @param npc
- * @param player
- */
+ // Uses craftbukkit methods to show a player an npc's inventory screen.
public static void showInventory(HumanNPC npc, Player player) {
NPCInventoryOpenEvent inventoryOpenEvent = new NPCInventoryOpenEvent(
npc, player);
@@ -25,16 +20,15 @@ public static void showInventory(HumanNPC npc, Player player) {
((CraftPlayer) player).getHandle().a(npc.getHandle().inventory);
}
- /**
- * Remove items from a player's current held slot
- *
- * @param player
- */
+ // Remove items from a player's current held slot
public static void decreaseItemInHand(Player player) {
player.setItemInHand(decreaseItemStack(player.getItemInHand()));
}
public static ItemStack decreaseItemStack(ItemStack stack) {
+ if (stack.getTypeId() == 0) {
+ return null;
+ }
int amount = stack.getAmount() - 1;
if (amount == 0) {
stack = null;
@@ -44,12 +38,7 @@ public static ItemStack decreaseItemStack(ItemStack stack) {
return stack;
}
- /**
- * Validate that an item has a durability and can be repaired
- *
- * @param item
- * @return
- */
+ // Get that an item is a tool
public static boolean isTool(ItemStack item) {
int id = item.getTypeId();
return (id >= 256 && id <= 259) || (id >= 267 && id <= 279)
@@ -57,12 +46,7 @@ public static boolean isTool(ItemStack item) {
|| id == 346 || id == 359;
}
- /**
- * Validate that the item to repair is armor
- *
- * @param armor
- * @return
- */
+ // Get if an item is armor
public static boolean isArmor(ItemStack armor) {
int id = armor.getTypeId();
return id >= 298 && id <= 317;
@@ -147,4 +131,23 @@ public static void removeItems(Player player, ItemStack buying, int slot) {
removeItems(player, buying.getType(), buying.getAmount(), slot);
}
+ public static boolean isHelmet(int itemID) {
+ return itemID == 298 || itemID == 302 || itemID == 306 || itemID == 310
+ || itemID == 314 || itemID == 86 || itemID == 91;
+ }
+
+ public static boolean isChestplate(int itemID) {
+ return itemID == 299 || itemID == 303 || itemID == 307 || itemID == 311
+ || itemID == 315;
+ }
+
+ public static boolean isLeggings(int itemID) {
+ return itemID == 300 || itemID == 304 || itemID == 308 || itemID == 312
+ || itemID == 316;
+ }
+
+ public static boolean isBoots(int itemID) {
+ return itemID == 301 || itemID == 305 || itemID == 309 || itemID == 313
+ || itemID == 317;
+ }
}
View
14 src/core/net/citizensnpcs/utils/ServerUtils.java
@@ -12,11 +12,7 @@
public class ServerUtils {
- /**
- * Get a player object from the provided name
- *
- * @param name
- */
+ // Get a player object from the provided name
public static Player matchPlayer(String name) {
List<Player> players = Bukkit.getServer().matchPlayer(name);
if (!players.isEmpty()) {
@@ -27,12 +23,8 @@ public static Player matchPlayer(String name) {
return null;
}
- /**
- * Check the Citizens thread on the Bukkit forums if there is a new version
- * available
- *
- * @param player
- */
+ // Check the Citizens thread on the Bukkit forums if there is a new version
+ // available
public static void checkForUpdates(Player player) {
try {
URI baseURI = new URI("http://forums.bukkit.org/threads/7173/");
View
4 src/guard/net/citizensnpcs/guards/flags/FlagList.java
@@ -107,9 +107,7 @@ public void clear() {
}
public enum FlagType {
- GROUP('g'),
- MOB('m'),
- PLAYER('p');
+ GROUP('g'), MOB('m'), PLAYER('p');
private final char flag;
FlagType(char flag) {
View
10 src/trader/net/citizensnpcs/traders/TraderCommands.java
@@ -393,13 +393,14 @@ public void addPermissions() {
CitizensManager.addPermission("trader.modify.stock");
CitizensManager.addPermission("trader.use.trade");
}
-
+
@Override
public void sendHelpPage(CommandSender sender, int page) {
HelpUtils.header(sender, "Trader", 1, 1);
HelpUtils.format(sender, "trader", "list [buy|sell] (page)",
"list a trader's buy/sell list");
- HelpUtils.format(sender, "trader", "[buy|sell] [itemID(:amount:data)] [price]",
+ HelpUtils.format(sender, "trader",
+ "[buy|sell] [itemID(:amount:data)] [price]",
"add an item to a trader's stock");
HelpUtils.format(sender, "trader", "[buy|sell] remove [itemID:data]",
"remove item from a trader's stock");
@@ -408,9 +409,10 @@ public void sendHelpPage(CommandSender sender, int page) {
"edit a trader's stock");
HelpUtils.format(sender, "trader", "unlimited",
"set whether a trader has unlimited stock");
- HelpUtils.format(sender, "trader", "money [give|take] (amount)",
+ HelpUtils.format(sender, "trader", "money (give|take) (amount)",
"control a trader's money");
- HelpUtils.format(sender, "trader", "clear [buy|sell]", "clear a trader's stock");
+ HelpUtils.format(sender, "trader", "clear [buy|sell]",
+ "clear a trader's stock");
HelpUtils.footer(sender);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.