How do get a string from another method?

Discussion in 'Plugin Development' started by TheRealItsMike, Aug 24, 2016.

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

    TheRealItsMike

    I'm new to java and i'm trying to create a plugin that give the user a gui to perform commands like kick but i'm having problems getting the argument from the onCommand method to onInventoryClick method.

    My code:
    Code:java
    1.  
    2. package me.ItsMike.GuiAdmin;
    3.  
    4.  
    5.  
    6.  
    7.  
    8.  
    9.  
    10.  
    11.  
    12.  
    13. import java.util.ArrayList;
    14.  
    15. import org.bukkit.Bukkit;
    16. import org.bukkit.Material;
    17. import org.bukkit.command.Command;
    18. import org.bukkit.command.CommandSender;
    19. import org.bukkit.entity.Player;
    20. import org.bukkit.event.EventHandler;
    21. import org.bukkit.event.Listener;
    22. import org.bukkit.event.inventory.InventoryClickEvent;
    23. import org.bukkit.event.inventory.InventoryType;
    24. import org.bukkit.inventory.Inventory;
    25. import org.bukkit.inventory.ItemStack;
    26. import org.bukkit.inventory.meta.ItemMeta;
    27. import org.bukkit.plugin.java.JavaPlugin;
    28.  
    29.  
    30.  
    31.  
    32.  
    33. public class Main extends JavaPlugin implements Listener{
    34. public Inventory inv = Bukkit.createInventory(null, 9, "MeMe");
    35. public Inventory test = Bukkit.createInventory(null, 54, "Test");
    36. public Inventory anvil = Bukkit.createInventory(null, InventoryType.ANVIL);
    37.  
    38.  
    39.  
    40.  
    41. public boolean onCommand(CommandSender sender, Command cmd, String commandLabael, String[] args){
    42. Player p = (Player) sender;
    43. if(cmd.getName().equalsIgnoreCase("punish")){
    44. if(args.length == 0){
    45. p.sendMessage("Please specify a player!");
    46. } else {
    47. // the argument i want to get
    48. String theguy = args[0];
    49.  
    50. ItemStack Guard = new ItemStack(Material.DIAMOND_PICKAXE);
    51. ItemMeta dmeta = Guard.getItemMeta();
    52. ArrayList<String> lore1 = new ArrayList<String>();
    53. lore1.add("§7Permanent strength II");
    54. dmeta.setLore(lore1);
    55. dmeta.setDisplayName("§dGuard");
    56. Guard.setItemMeta(dmeta);
    57. //
    58. inv.setItem(0, Guard);
    59. p.openInventory(inv);
    60.  
    61. }
    62. }
    63.  
    64.  
    65.  
    66.  
    67. return true;
    68.  
    69.  
    70. }
    71. @EventHandler
    72. public void onInventoryClick(InventoryClickEvent event) {
    73. Player player = (Player) event.getWhoClicked();
    74. ItemStack clicked = event.getCurrentItem();
    75. Inventory inventory = event.getInventory();
    76. if (inventory.getName().equals(inv.getName())) {
    77. if (clicked.getType() == Material.DIAMOND_PICKAXE) {
    78. event.setCancelled(true);
    79. // i want to display the argument here
    80. player.sendMessage(theguy);
    81.  
    82.  
    83.  
    84.  
    85. }
    86.  
    87. }
    88. }
    89.  
    90. public void onEnable(){
    91.  
    92. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    93.  
    94. }
    95. }
    96.  
    97.  
     
  2. Offline

    Tecno_Wizard

    @TheRealItsMike, could you be more specific? What are you trying to hold on to, because we're not telepathic. Lol
     
  3. Offline

    TheRealItsMike

    I'm trying to get the args[0] from the command "punish" to be sent as a message when the diamond pickaxe is clicked(the onInventoryClick event) what i need to know is how to get the args[0] string(theguy) onto the onInventoryClick method.
     
  4. Offline

    Zombie_Striker

    @TheRealItsMike
    1. Create a hashmap. The keys will be the player's UUID and the values will be a String. The UUID will represent the player, and the String will be the message that will be sent.
    2. When a player sends the command, store the args[0] inside the hashmap.
    3. When a player mines a block, send the message from the map.
     
  5. Offline

    TheRealItsMike

    Won't work as i will somehow have to transfer the uuid of the player from one method to another. which is what i'm trying to achieve anyway :/
     
    Last edited: Aug 24, 2016
  6. Offline

    Lordloss

    Thats the point where the mysterious, so called "variables" come in. No really, please read on google about "java variable scope".
     
  7. Offline

    Tecno_Wizard

    @TheRealItsMike I hate when it comes this, but it's obvious you don't know enough java to work with bukkit. Come back when you've learned some more.
     
Thread Status:
Not open for further replies.

Share This Page