How do you give players custom items when they execute a command?

Discussion in 'Plugin Development' started by STTL, Dec 21, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    STTL

    What I am trying to do is make a custom inventory that can be reached by using the "God" Compass. I created the GodCompass itemstack and set it's meta to the one i created.
    I made my onCommand() method and made my command "/GodInv" to get the compass. Although, it says my variable is not recognized... is this because onCommand is a boolean and not a void? If so, is there a way that I can still add the custom itemstack to the player's inventory (with a command).
    Code:java
    1. package com.gmail.sttlmodder.ItemEssentials;
    2. import org.bukkit.Bukkit;
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.enchantments.Enchantment;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.*;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class ItemEssentials extends JavaPlugin implements Listener{
    19. public void onEnable() {
    20. getLogger().info("ITEM ESSENTIALS HAS BEEN ACTIVATED!");
    21. }
    22. public Player p = Bukkit.getServer().getPlayer("");
    23.  
    24. public Inventory God(Inventory G)
    25. {
    26. final ItemStack GodHelm = new ItemStack (Material.DIAMOND_HELMET, 1);
    27. final ItemMeta GodHelmMeta = GodHelm.getItemMeta();
    28.  
    29. GodHelmMeta.setDisplayName(ChatColor.RED + "God Helm");
    30. GodHelmMeta.addEnchant(Enchantment.OXYGEN, 10, true);
    31. GodHelmMeta.addEnchant(Enchantment.DURABILITY, 10, true);
    32. GodHelmMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
    33. GodHelmMeta.addEnchant(Enchantment.THORNS, 10, true);
    34.  
    35. GodHelm.setItemMeta(GodHelmMeta);
    36.  
    37. final ItemStack GodChest = new ItemStack (Material.DIAMOND_CHESTPLATE, 1);
    38. final ItemMeta GodChestMeta = GodChest.getItemMeta();
    39.  
    40. GodChestMeta.setDisplayName(ChatColor.RED + "God Chestplate");
    41. GodChestMeta.addEnchant(Enchantment.DURABILITY, 10, true);
    42. GodChestMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
    43. GodChestMeta.addEnchant(Enchantment.THORNS, 10, true);
    44.  
    45. GodChest.setItemMeta(GodChestMeta);
    46.  
    47. final ItemStack GodLegs = new ItemStack (Material.DIAMOND_LEGGINGS, 1);
    48. final ItemMeta GodLegMeta = GodLegs.getItemMeta();
    49.  
    50. GodLegMeta.setDisplayName(ChatColor.RED + "God Leggings");
    51. GodLegMeta.addEnchant(Enchantment.DURABILITY, 10, true);
    52. GodLegMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
    53. GodLegMeta.addEnchant(Enchantment.THORNS, 10, true);
    54.  
    55. GodLegs.setItemMeta(GodLegMeta);
    56.  
    57. final ItemStack GodBoots = new ItemStack (Material.DIAMOND_BOOTS, 1);
    58. final ItemMeta GodBootsMeta = GodBoots.getItemMeta();
    59.  
    60. GodBootsMeta.setDisplayName(ChatColor.RED + "God Boots");
    61. GodBootsMeta.addEnchant(Enchantment.DURABILITY, 10, true);
    62. GodBootsMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
    63. GodBootsMeta.addEnchant(Enchantment.THORNS, 10, true);
    64. GodBootsMeta.addEnchant(Enchantment.PROTECTION_FALL, 10, true);
    65.  
    66. GodBoots.setItemMeta(GodBootsMeta);
    67.  
    68. G.addItem(GodHelm);
    69. G.addItem(GodChest);
    70. G.addItem(GodLegs);
    71. G.addItem(GodBoots);
    72. return G;
    73. }
    74.  
    75. //public static Inventory GodInv;
    76.  
    77. @EventHandler
    78. public void LeftClick(PlayerInteractEvent event)
    79. {
    80. Player player = event.getPlayer();
    81.  
    82. ItemStack GodCompass = new ItemStack (Material.COMPASS);
    83. ItemMeta GodCompassMeta = GodCompass.getItemMeta();
    84. GodCompassMeta.setDisplayName(ChatColor.RED + "God");
    85. GodCompass.setItemMeta(GodCompassMeta);
    86.  
    87. if(event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
    88.  
    89. if(event.getMaterial().equals(Material.COMPASS)) {
    90. if (player.hasPermission("ItemEssentials.GodInv") && (player.getItemInHand().getItemMeta().getDisplayName() == ChatColor.RED + "God")) {
    91. Inventory God = Bukkit.getServer().createInventory(null, 9, "\bGod Items\b");
    92. God = God(God);
    93. player.openInventory(God);
    94. }else if (!player.hasPermission("ItemEssentials.GodInv")) {
    95. player.sendMessage(ChatColor.RED + "You do not have permission to access the inventory of the Gods!");
    96. event.getPlayer().setHealth(0);
    97. Bukkit.getServer().broadcastMessage(ChatColor.RED + event.getPlayer().getDisplayName() + "did not have permission to access the inventory of the Gods!");
    98. }
    99. }
    100. }
    101. }
    102. public boolean onCommand(CommandSender sender, Command Cmd, String label, String[] args) {
    103. Player player = Bukkit.getServer().getPlayer("");
    104. if (label.equalsIgnoreCase("GodInv")) {
    105. if (sender.hasPermission("ItemEssentials.GodInv")) {
    106. player.getInventory().addItem(GodCompass);
    107. }
    108. }
    109. return false;
    110. }
    111. }
     
  2. Offline

    XvBaseballkidvX

    STTL This is what you onCommand should look like:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command Cmd, String label, String[] args) {
    2. if(sender instanceof Player){ //Checking to make sure that the Sender is a Player
    3. Player player = (Player) sender;
    4. if(cmd.getName().equalsIgnoreCase("GodInv"){
    5. if(player.hasPermission("ItemEssentials.GodInv"){
    6. player.getInventory().addItem(GodCompass);
    7. }
    8. }
    9. return false;
    10. }
     
    danjb2000 likes this.
  3. Offline

    STTL

    XvBaseballkidvX
    I tried doing that but it still says that GodCompass cannot be resolved to a variable :(
     
  4. Offline

    XvBaseballkidvX

    STTL Can you post your plugin.yml to make sure everything in there is done correctly?

    EDIT: I re-read your question, the reason it says that is because you put GodCompass in the InteractEvent so your onCommand method can't access it. You either need to make an Items Class for all of your custom items (Which is very helpful when doing stuff with inventories) or copy and paste your GodCompass ItemStack and ItemMeta into your onCommand method.
     
  5. Offline

    STTL

    XvBaseballkidvX
    Here is my plugin.yml:
    Code:
    name: Item Essentials
    main: com.gmail.sttlmodder.ItemEssentials.ItemEssentials
    version: 0.1.2
    author: STTL
    description: Adds many new items to the game!
     
    commands:
      GodInv:
          usage: /<command>
          description: Opens the inventory of the gods! Can be opened once every 3 minutes, you take 2 damage every use... and you die if you dont have permission for it.
          permission: ItemEssentials.GodInv
          permission-message: You do not have permission for the God Inv and you will pay for it!
     
    permissions:
      ItemEssentials.*:
          children:
            ItemEssentials.GodInv: true
      ItemEssentials.GodInv:
          description: Gives player access to the God inventory!
          default: false
    Here is my new code:
    Code:java
    1. package com.gmail.sttlmodder.ItemEssentials;
    2. import org.bukkit.Bukkit;
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.enchantments.Enchantment;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.*;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class ItemEssentials extends JavaPlugin implements Listener{
    19. public void onEnable() {
    20. getLogger().info("ITEM ESSENTIALS HAS BEEN ACTIVATED!");
    21. }
    22. public Player p = Bukkit.getServer().getPlayer("");
    23.  
    24. public Inventory God(Inventory G)
    25. {
    26. final ItemStack GodHelm = new ItemStack (Material.DIAMOND_HELMET, 1);
    27. final ItemMeta GodHelmMeta = GodHelm.getItemMeta();
    28.  
    29. GodHelmMeta.setDisplayName(ChatColor.RED + "God Helm");
    30. GodHelmMeta.addEnchant(Enchantment.OXYGEN, 10, true);
    31. GodHelmMeta.addEnchant(Enchantment.DURABILITY, 10, true);
    32. GodHelmMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
    33. GodHelmMeta.addEnchant(Enchantment.THORNS, 10, true);
    34.  
    35. GodHelm.setItemMeta(GodHelmMeta);
    36.  
    37. final ItemStack GodChest = new ItemStack (Material.DIAMOND_CHESTPLATE, 1);
    38. final ItemMeta GodChestMeta = GodChest.getItemMeta();
    39.  
    40. GodChestMeta.setDisplayName(ChatColor.RED + "God Chestplate");
    41. GodChestMeta.addEnchant(Enchantment.DURABILITY, 10, true);
    42. GodChestMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
    43. GodChestMeta.addEnchant(Enchantment.THORNS, 10, true);
    44.  
    45. GodChest.setItemMeta(GodChestMeta);
    46.  
    47. final ItemStack GodLegs = new ItemStack (Material.DIAMOND_LEGGINGS, 1);
    48. final ItemMeta GodLegMeta = GodLegs.getItemMeta();
    49.  
    50. GodLegMeta.setDisplayName(ChatColor.RED + "God Leggings");
    51. GodLegMeta.addEnchant(Enchantment.DURABILITY, 10, true);
    52. GodLegMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
    53. GodLegMeta.addEnchant(Enchantment.THORNS, 10, true);
    54.  
    55. GodLegs.setItemMeta(GodLegMeta);
    56.  
    57. final ItemStack GodBoots = new ItemStack (Material.DIAMOND_BOOTS, 1);
    58. final ItemMeta GodBootsMeta = GodBoots.getItemMeta();
    59.  
    60. GodBootsMeta.setDisplayName(ChatColor.RED + "God Boots");
    61. GodBootsMeta.addEnchant(Enchantment.DURABILITY, 10, true);
    62. GodBootsMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
    63. GodBootsMeta.addEnchant(Enchantment.THORNS, 10, true);
    64. GodBootsMeta.addEnchant(Enchantment.PROTECTION_FALL, 10, true);
    65.  
    66. GodBoots.setItemMeta(GodBootsMeta);
    67.  
    68. G.addItem(GodHelm);
    69. G.addItem(GodChest);
    70. G.addItem(GodLegs);
    71. G.addItem(GodBoots);
    72. return G;
    73. }
    74.  
    75. //public static Inventory GodInv;
    76.  
    77. @EventHandler
    78. public void LeftClick(PlayerInteractEvent event)
    79. {
    80. Player player = event.getPlayer();
    81.  
    82. if(event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
    83.  
    84. if(event.getMaterial().equals(Material.COMPASS)) {
    85. if (player.hasPermission("ItemEssentials.GodInv") && (player.getItemInHand().getItemMeta().getDisplayName() == ChatColor.RED + "God")) {
    86. Inventory God = Bukkit.getServer().createInventory(null, 9, "\bGod Items\b");
    87. God = God(God);
    88. player.openInventory(God);
    89. }else if (!player.hasPermission("ItemEssentials.GodInv")) {
    90. player.sendMessage(ChatColor.RED + "You do not have permission to access the inventory of the Gods!");
    91. event.getPlayer().setHealth(0);
    92. Bukkit.getServer().broadcastMessage(ChatColor.RED + event.getPlayer().getDisplayName() + "did not have permission to access the inventory of the Gods!");
    93. }
    94. }
    95. }
    96. }
    97. public boolean onCommand(CommandSender sender, Command Cmd, String label, String[] args) {
    98. if(sender instanceof Player){ //Checking to make sure that the Sender is a Player
    99. Player player = (Player) sender;
    100. ItemStack GodCompass = new ItemStack (Material.COMPASS);
    101. ItemMeta GodCompassMeta = GodCompass.getItemMeta();
    102. GodCompassMeta.setDisplayName(ChatColor.RED + "God");
    103. GodCompass.setItemMeta(GodCompassMeta);
    104.  
    105. if(Cmd.getName().equalsIgnoreCase("GodInv")){
    106. if(player.hasPermission("ItemEssentials.GodInv")){
    107. player.getInventory().addItem(GodCompass);
    108. }
    109. if(!player.hasPermission("ItemEssentials.GodInv")){
    110. sender.sendMessage(ChatColor.RED + "You do not have permission to open the inventory of the gods!");
    111. ((Player) sender).setHealth(0);
    112. Bukkit.getServer().broadcastMessage(ChatColor.RED + "[Item Essentials]" + sender.getName() + "did not have permission to access the inventory of the gods!");
    113. }
    114. ((Player) sender).damage(2);;
    115. try {
    116. //Does the line under this one make you wait 3 minutes before being able to execute the command again?
    117. ((Player) sender).wait(180);
    118. } catch (InterruptedException e) {
    119. e.printStackTrace();
    120. };
    121. }
    122. return false;
    123. }
    124. if(Cmd.getName().equalsIgnoreCase("GInv")){
    125. }
    126. return false;
    127. }
    128. }
     
  6. Offline

    XvBaseballkidvX

    STTL No it does not. You need to make a cooldown method and compare time stamps, or use a runnable. The time stamps method is much better to use in my opinion.
     
  7. Offline

    STTL

    XvBaseballkidvX
    Thanks :)
    There is one problem though,I tried using my custom compass but it is being used by a different plugin for teleporting and I dont know which one, I am using:
    Skyolympus
    PlayerLink
    PermissionsEX
    Multiverse-Core
    mcore
    mcMMO
    Item Essentials
    InactiveLockette
    Factions
    Essentials (All)
    Clearlag
    Citizens
    AuthMe

    People are saying that WorldEdit is causing it, but I turned that off in the config file and it is still happening.
    Will my compass work properly if I add @Override to it?
     
  8. Offline

    XvBaseballkidvX

    STTL I am pretty sure this is causing the problem:
    Code:java
    1. (player.getItemInHand().getItemMeta().getDisplayName() == ChatColor.RED + "God"))

    When seeing if strings are equal to each other you need to use:
    Code:java
    1. .equalsIgnoreCase("String")

    So this is what you should have:
    Code:java
    1. player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "God"))

    If this doesn't resolve your problem tag me again :3
     
Thread Status:
Not open for further replies.

Share This Page