Public Variable

Discussion in 'Plugin Development' started by david123718, Oct 11, 2014.

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

    david123718

    Hello im trying to make a punishment GUI plugin and I dont know how to make a Variable global like for instance
    I want to do:
    Code:java
    1. switch (event.getCurrentItem().getType()) {
    2. case REDSTONE_BLOCK:
    3. player.chat("/tempban " + args[0]); // im getting a error by args
    4. player.closeInventory();

    And here is my targeting player thingy
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2. Player player = (Player) sender;
    3. Player target = Bukkit.getServer().getPlayer(args[0]);
    4. if(commandLabel.equalsIgnoreCase("punish")) {
    5. if (args.length == 0) {
    6. player.sendMessage(ChatColor.RED + "Please specify player!");
    7. return true;
    8.  
    9. }
    10.  
    11. if (target == null) {
    12. player.sendMessage(ChatColor.RED + "Could Not Find Player " + args[0] + "!");
    13. return true;
    14.  
    15. }
    16. openGUI(player);
    17. }
    18.  
    19. return false;


    Just incase the entire code:

    Code:java
    1. package me.David.PrickledPvP;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.inventory.InventoryClickEvent;
    14. import org.bukkit.inventory.Inventory;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17.  
    18. public class PunishmentGUI implements Listener, CommandExecutor {
    19.  
    20.  
    21. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    22. Player player = (Player) sender;
    23. Player target = Bukkit.getServer().getPlayer(args[0]);
    24. if(commandLabel.equalsIgnoreCase("punish")) {
    25. if (args.length == 0) {
    26. player.sendMessage(ChatColor.RED + "Please specify player!");
    27. return true;
    28.  
    29. }
    30.  
    31. if (target == null) {
    32. player.sendMessage(ChatColor.RED + "Could Not Find Player " + args[0] + "!");
    33. return true;
    34.  
    35. }
    36. openGUI(player);
    37. }
    38.  
    39. return false;
    40. }
    41. private void openGUI(Player player) {
    42. Inventory inv = Bukkit.createInventory(null, 54, ChatColor.DARK_RED
    43. + "Ban Selector");
    44.  
    45.  
    46. ItemStack tempban = new ItemStack(Material.REDSTONE_BLOCK);
    47. ItemMeta tempbanMeta = tempban.getItemMeta();
    48. ItemStack creative = new ItemStack(Material.GOLD_BLOCK);
    49. ItemMeta creativeMeta = creative.getItemMeta();
    50. ItemStack survival = new ItemStack(Material.LOG);
    51. ItemMeta survivalMeta = survival.getItemMeta();
    52. ItemStack spleef = new ItemStack(Material.DIAMOND_SPADE);
    53. ItemMeta spleefMeta = spleef.getItemMeta();
    54.  
    55. tempbanMeta.setDisplayName(ChatColor.AQUA + "Temporary Ban For 1 Day");
    56. tempban.setItemMeta(tempbanMeta);
    57.  
    58. creativeMeta.setDisplayName(ChatColor.AQUA + "Creative");
    59. creative.setItemMeta(creativeMeta);
    60.  
    61. survivalMeta.setDisplayName(ChatColor.RED + "Survival");
    62. survival.setItemMeta(survivalMeta);
    63.  
    64. spleefMeta.setDisplayName(ChatColor.BLUE + "Spleef");
    65. spleef.setItemMeta(spleefMeta);
    66.  
    67. inv.setItem(0, tempban);
    68. inv.setItem(1, creative);
    69. inv.setItem(2, survival);
    70. inv.setItem(3, spleef);
    71.  
    72. player.openInventory(inv);
    73.  
    74. }
    75.  
    76. @EventHandler
    77. public void onInventoryClick(InventoryClickEvent event) {
    78. if (!ChatColor.stripColor(event.getInventory().getName())
    79. .equalsIgnoreCase("Ban Selector"))
    80. return;
    81. Player player = (Player) event.getWhoClicked();
    82. System.out.println("Event Triggered!");
    83. event.setCancelled(true);
    84.  
    85. if(event.getCurrentItem()==null || event.getCurrentItem().getType()==Material.AIR||!event.getCurrentItem().hasItemMeta()){
    86. player.closeInventory();
    87. return;
    88. }
    89. switch (event.getCurrentItem().getType()) {
    90. case REDSTONE_BLOCK:
    91. player.chat("/tempban " + args[0]);
    92. player.closeInventory();
    93. }
    94. switch (event.getCurrentItem().getType()) {
    95. case GOLD_BLOCK:
    96. Location plots = new Location(Bukkit.getWorld("plots"), -163, 65, 375, -90, 0);
    97. player.teleport(plots);
    98. player.closeInventory();
    99.  
    100. }
    101. switch (event.getCurrentItem().getType()) {
    102. case LOG:
    103. Location survival = new Location(Bukkit.getWorld("survival"), 25, 67, -76, 180, 0);
    104. player.teleport(survival);
    105. player.closeInventory();
    106. }
    107. switch (event.getCurrentItem().getType()) {
    108.  
    109. case DIAMOND_SPADE:
    110. Location spleef = new Location(Bukkit.getWorld("spawn"), 1757, 4, -887, 90, 0);
    111. player.chat("/warp mine");
    112. player.closeInventory(); }
    113.  
    114. }
    115. }
    116.  
    117.  
    118.  
    119.  
    120.  
    121.  
    122.  
    123.  
    124.  


    please note This is not my Main class.
     
  2. Offline

    SmooshCakez

  3. Offline

    david123718

    Its not a error just I wanted to know a way I could do it.
     
  4. Offline

    SmooshCakez

    Outside of a method.
    Code:java
    1. public String[] args;

    Done.
     
  5. Offline

    david123718

    I want it to put the user when I click the GUI of a Redstone Block to do /tempban (target) but I dont know how. And I did your Method and now Im getting a Stack Trace.
    [00:59:31] [Server thread/INFO]: Starting minecraft server version 1.7.9
    [00:59:31] [Server thread/INFO]: Loading properties
    [00:59:31] [Server thread/INFO]: Default game type: SURVIVAL
    [00:59:31] [Server thread/INFO]: Generating keypair
    [00:59:31] [Server thread/INFO]: Starting Minecraft server on *:25565
    [00:59:32] [Server thread/INFO]: This server is running CraftBukkit version git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks (MC: 1.7.9) (Implementing API version 1.7.9-R0.3-SNAPSHOT)
    [00:59:32] [Server thread/INFO]: [PrickledPvP] Loading PrickledPvP v1.0
    [00:59:32] [Server thread/INFO]: [Vault] Loading Vault v1.4.1-b436
    [00:59:32] [Server thread/INFO]: [Vault] Enabling Vault v1.4.1-b436
    [00:59:32] [Server thread/INFO]: [Vault] [Permission] SuperPermissions loaded as backup permission system.
    [00:59:32] [Thread-7/WARN]: Could not get information about this CraftBukkit version; perhaps you are running a custom one?: IOException
    [00:59:32] [Server thread/INFO]: [Vault] Enabled Version 1.4.1-b436
    [00:59:32] [Server thread/INFO]: Preparing level "world"
    [00:59:32] [Thread-7/WARN]: Could not get latest artifact information: IOException
    [00:59:32] [Server thread/INFO]: Preparing start region for level 0 (Seed: -1797287879248858028)
    [00:59:33] [Server thread/INFO]: Preparing spawn area: 10%
    [00:59:35] [Server thread/INFO]: Preparing spawn area: 72%
    [00:59:35] [Server thread/INFO]: Preparing start region for level 1 (Seed: -1797287879248858028)
    [00:59:36] [Server thread/INFO]: Preparing spawn area: 60%
    [00:59:36] [Server thread/INFO]: Preparing start region for level 2 (Seed: -1797287879248858028)
    [00:59:37] [Server thread/INFO]: [PrickledPvP] Enabling PrickledPvP v1.0
    [00:59:37] [Server thread/INFO]: Server permissions file permissions.yml is empty, ignoring it
    [00:59:37] [Server thread/INFO]: Done (4.799s)! For help, type "help" or "?"
    [00:59:39] [pool-3-thread-2/INFO]: [Vault] Checking for Updates:
    [00:59:39] [pool-3-thread-2/INFO]: [Vault] No new version available
    [01:00:05] [User Authenticator #1/INFO]: UUID of player david123718 is b864c847-9592-4eb9-a3e6-91708c70503b
    [01:00:05] [Server thread/INFO]: david123718[/127.0.0.1:55267] logged in with entity id 343 at ([world] -406.0475775973503, 77.22718576268721, -297.84530909984585)
    [01:00:13] [Server thread/INFO]: david123718 issued server command: /punish
    [01:00:13] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'punish' in plugin PrickledPvP v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:740) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    at me.David.PrickledPvP.PunishmentGUI.onCommand(PunishmentGUI.java:23) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    ... 13 more
    [01:00:26] [Server thread/INFO]: david123718 issued server command: /punish
    [01:00:26] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'punish' in plugin PrickledPvP v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:740) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    at me.David.PrickledPvP.PunishmentGUI.onCommand(PunishmentGUI.java:23) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    ... 13 more
    [01:00:32] [Server thread/INFO]: CONSOLE: Stopping the server..
    [01:00:32] [Server thread/INFO]: Stopping server
    [01:00:32] [Server thread/INFO]: [Vault] Disabling Vault v1.4.1-b436
    [01:00:32] [Server thread/INFO]: [PrickledPvP] Disabling PrickledPvP v1.0
    [01:00:32] [Server thread/INFO]: Saving players
    [01:00:32] [Server thread/INFO]: david123718 lost connection: Server closed
    [01:00:32] [Server thread/INFO]: david123718 left the game.
    [01:00:32] [Server thread/INFO]: Saving worlds
    [01:00:32] [Server thread/INFO]: Saving chunks for level 'world'/Overworld
    [01:00:33] [Server thread/INFO]: Saving chunks for level 'world_nether'/Nether
    [01:00:33] [Server thread/INFO]: Saving chunks for level 'world_the_end'/The End
    [01:00:33] [Thread-4/INFO]: Stopping server
    [01:00:33] [Thread-4/INFO]: Saving players
    [01:00:33] [Thread-4/INFO]: Saving worlds
    [01:00:33] [Thread-4/INFO]: Saving chunks for level 'world'/Overworld
     
  6. Offline

    coasterman10

    It appears you don't fully understand how Java works.

    The onCommand method represents the code that will run when a command is executed. You need all of the code to be in there, or if you are making method calls outside of it, to pass the necessary variables there.

    You are getting an error with args[] because it doesn't exist in that context. I should not you most certainly do NOT want to create a global variable. What if two people open a GUI at once?

    Seeing as you have opened a GUI that pertains to a specific player, you want to save that player for when the appropriate item is selected in the GUI. The simplest way for you to do this would be to save a Map<UUID, String> for the player's name, or optimally get the player first and get their UUID, then use a Map<UUID, UUID>. When the GUI item is selected, retrieve the name/UUID of the player in question from the Map, then remove it from the map to save memory.

    Really?
     
  7. Offline

    david123718

    Im new can you make it a little simpler?
     
  8. Offline

    teej107

  9. Offline

    david123718

    Last edited by a moderator: Jun 14, 2016
  10. Offline

    teej107

    david123718
    Code:java
    1. Map<UUID, String> theUuidInThisMapRepresentsThePlayerAndTheStringRepresentsTheArgument = new HashMap<UUID, String>();
     
Thread Status:
Not open for further replies.

Share This Page