Open GUI

Discussion in 'Plugin Development' started by XFarwar, Jul 26, 2014.

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

    XFarwar

    Hi, my plugin is about to end, but, I want a simple help.
    First, this is my code:
    Code:java
    1. package PannelGui;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Effect;
    6. import org.bukkit.Material;
    7. import org.bukkit.Sound;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.inventory.InventoryClickEvent;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.event.player.PlayerJoinEvent;
    16. import org.bukkit.event.player.PlayerQuitEvent;
    17. import org.bukkit.inventory.Inventory;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.inventory.meta.ItemMeta;
    20.  
    21. public class AdminGUI implements Listener {
    22. Main plugin;
    23. public AdminGUI (Main plugin){
    24. this.plugin = plugin;
    25. }
    26.  
    27. public static String PREFIX = ChatColor.BLACK + "[" + ChatColor.DARK_BLUE + "MyPannel" + ChatColor.BLACK + "]";
    28.  
    29. private void openGUI(Player player) {
    30. Inventory inv = Bukkit.createInventory(null, 27, ChatColor.DARK_BLUE + "Admin Pannel");
    31.  
    32. ItemStack heal = new ItemStack(Material.POTION);
    33. ItemMeta healMeta = heal.getItemMeta();
    34. ItemStack vanish = new ItemStack(Material.QUARTZ);
    35. ItemMeta vanishMeta = vanish.getItemMeta();
    36. ItemStack fly = new ItemStack(Material.FEATHER);
    37. ItemMeta flyMeta = fly.getItemMeta();
    38. ItemStack gms = new ItemStack(Material.DIAMOND_SWORD);
    39. ItemMeta gmsMeta = gms.getItemMeta();
    40. ItemStack gmc = new ItemStack(Material.DIAMOND_BLOCK);
    41. ItemMeta gmcMeta = gmc.getItemMeta();
    42. ItemStack day = new ItemStack(Material.GOLD_BLOCK);
    43. ItemMeta dayMeta = day.getItemMeta();
    44. ItemStack night = new ItemStack(Material.OBSIDIAN);
    45. ItemMeta nightMeta = night.getItemMeta();
    46. ItemStack join = new ItemStack(Material.EYE_OF_ENDER);
    47. ItemMeta joinMeta = night.getItemMeta();
    48. ItemStack left = new ItemStack(Material.ENDER_PEARL);
    49. ItemMeta leftMeta = night.getItemMeta();
    50.  
    51. healMeta.setDisplayName(ChatColor.RED + "*" + ChatColor.BOLD + ChatColor.DARK_RED + "Heal Yourself" + ChatColor.RESET + ChatColor.RED + "*");
    52. heal.setItemMeta(healMeta);
    53. vanishMeta.setDisplayName(ChatColor.RED + "*" + ChatColor.BOLD + ChatColor.DARK_RED + "Hide you from other" + ChatColor.RESET + ChatColor.RED + "*");
    54. vanish.setItemMeta(vanishMeta);
    55. flyMeta.setDisplayName(ChatColor.RED + "*" + ChatColor.BOLD + ChatColor.DARK_RED + "Fly Mode On/Off" + ChatColor.RESET + ChatColor.RED + "*");
    56. fly.setItemMeta(flyMeta);
    57. gmsMeta.setDisplayName(ChatColor.RED + "*" + ChatColor.BOLD + ChatColor.DARK_RED + "Survival Mode" + ChatColor.RESET + ChatColor.RED + "*");
    58. gms.setItemMeta(gmsMeta);
    59. gmcMeta.setDisplayName(ChatColor.RED + "*" + ChatColor.BOLD + ChatColor.DARK_RED + "Creative Mode" + ChatColor.RESET + ChatColor.RED + "*");
    60. gmc.setItemMeta(gmcMeta);
    61. dayMeta.setDisplayName(ChatColor.RED + "*" + ChatColor.BOLD + ChatColor.DARK_RED + "Time Day" + ChatColor.RESET + ChatColor.RED + "*");
    62. day.setItemMeta(dayMeta);
    63. nightMeta.setDisplayName(ChatColor.RED + "*" + ChatColor.BOLD + ChatColor.DARK_RED + "Time Night" + ChatColor.RESET + ChatColor.RED + "*");
    64. night.setItemMeta(nightMeta);
    65. joinMeta.setDisplayName(ChatColor.RED + "*" + ChatColor.BOLD + ChatColor.DARK_RED + "Fake Join Message" + ChatColor.RESET + ChatColor.RED + "*");
    66. join.setItemMeta(joinMeta);
    67. leftMeta.setDisplayName(ChatColor.RED + "*" + ChatColor.BOLD + ChatColor.DARK_RED + "Fake Left Message" + ChatColor.RESET + ChatColor.RED + "*");
    68. left.setItemMeta(leftMeta);
    69.  
    70. inv.setItem(0, heal);
    71. inv.setItem(1, vanish);
    72. inv.setItem(2, fly);
    73. inv.setItem(3, join);
    74. inv.setItem(4, left);
    75. inv.setItem(9, gms);
    76. inv.setItem(10, gmc);
    77. inv.setItem(18, day);
    78. inv.setItem(19, night);
    79.  
    80. player.openInventory(inv);
    81. }
    82. @SuppressWarnings("deprecation")
    83. @EventHandler
    84. public void onInventoryClick(InventoryClickEvent event) {
    85. if(!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("Admin Pannel"))
    86. return;
    87. Player player = (Player) event.getWhoClicked();
    88. event.setCancelled(true);
    89.  
    90. if(event.getCurrentItem()==null || event.getCurrentItem().getType()==Material.AIR||!event.getCurrentItem().hasItemMeta()){
    91. player.closeInventory();
    92. return;
    93. }
    94. switch(event.getCurrentItem().getType()){
    95. case POTION:
    96. Bukkit.dispatchCommand((CommandSender) player, "heal");
    97. player.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 0);
    98. player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 20, 5);
    99. player.closeInventory();
    100. break;
    101. case QUARTZ:
    102. Bukkit.dispatchCommand((CommandSender) player, "vanish");
    103. player.playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 0);
    104. player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 20, 5);
    105. player.closeInventory();
    106. break;
    107. case FEATHER:
    108. Bukkit.dispatchCommand((CommandSender) player, "fly");
    109. player.playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 0);
    110. player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 20, 5);
    111. player.closeInventory();
    112. break;
    113. case DIAMOND_SWORD:
    114. Bukkit.dispatchCommand((CommandSender) player, "gms");
    115. player.playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 0);
    116. player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 20, 5);
    117. player.closeInventory();
    118. break;
    119. case DIAMOND_BLOCK:
    120. Bukkit.dispatchCommand((CommandSender) player, "gmc");
    121. player.playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 0);
    122. player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 20, 5);
    123. player.closeInventory();
    124. break;
    125. case GOLD_BLOCK:
    126. Bukkit.dispatchCommand((CommandSender) player, "day");
    127. player.playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 0);
    128. player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 20, 5);
    129. player.closeInventory();
    130. break;
    131. case OBSIDIAN:
    132. Bukkit.dispatchCommand((CommandSender) player, "night");
    133. player.playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 0);
    134. player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 20, 5);
    135. player.closeInventory();
    136. break;
    137. case EYE_OF_ENDER:
    138. Bukkit.dispatchCommand((CommandSender) player, "fjoin");
    139. player.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 0);
    140. player.playSound(player.getLocation(), Sound.CLICK, 20, 5);
    141. player.closeInventory();
    142. break;
    143. case ENDER_PEARL:
    144. Bukkit.dispatchCommand((CommandSender) player, "fleft");
    145. player.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 0);
    146. player.playSound(player.getLocation(), Sound.CLICK, 20, 5);
    147. player.closeInventory();
    148. break;
    149. default:
    150. player.closeInventory();
    151. break;
    152. }
    153. }
    154. @EventHandler
    155. public void onPlayerJoin(PlayerJoinEvent event) {
    156. event.getPlayer().getInventory().addItem(new ItemStack(Material.BLAZE_ROD));
    157. }
    158. @EventHandler
    159. public void onPlayerInteract(PlayerInteractEvent event){
    160. Action a = event.getAction();
    161. ItemStack is = event.getItem();
    162.  
    163. if(a == Action.PHYSICAL || is == null || is.getType() == Material.AIR)
    164. return;
    165. if (is.getType()==Material.BLAZE_ROD)
    166. openGUI(event.getPlayer());
    167. }
    168.  
    169. @EventHandler
    170. public void onPlayerQuit(PlayerQuitEvent event) {
    171. event.getPlayer().getInventory().remove(Material.BLAZE_ROD);
    172. }
    173. }


    This InventoryGUI is open by a BlazeRod, (it works at 100% ;D), but, now i want to open it by a command, how can I do?
    Helps?
     
  2. Offline

    Konkz

    Same way you open it when item BLAZE_ROD is used. Listen to the command being executed and if it matches the you want openGUI(bukkitPlayer);
     
  3. Offline

    XFarwar

    I don't understand what you mean..
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. Action a = event.getAction();
    4. ItemStack is = event.getItem();
    5.  
    6. if(a == Action.PHYSICAL || is == null || is.getType() == Material.AIR)
    7. return;
    8. if (is.getType()==Material.BLAZE_ROD)
    9. openGUI(event.getPlayer());
    10. }

    How should I modify it?
     
  4. Offline

    iMurder

    XFarwar
    So you know how to "create" all of that code yourself (assuming you did), yet don't know how to call a method from a command? kk then...
     
  5. Offline

    MrlolDestructo

  6. Offline

    XFarwar

    Code:java
    1. public static String PREFIX = ChatColor.BLACK + "[" + ChatColor.DARK_BLUE + "Test" + ChatColor.BLACK + "]";
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. if(cmd.getName().equalsIgnoreCase("gui")){
    4. Player p = (Player) sender;
    5. openGUI(p);
    6. }
    7. return false;
    8. }
    9.  
    10. private void openGUI(Player player) {
    11. Inventory inv = Bukkit.createInventory(null, 27, ChatColor.DARK_BLUE + "Test123");
    12.  
    13. ItemStack heal = new ItemStack(Material.POTION);
    14. ItemMeta healMeta = heal.getItemMeta();
    15.  
    16. healMeta.setDisplayName(ChatColor.RED + "*" + ChatColor.BOLD + ChatColor.DARK_RED + "Heal Yourself" + ChatColor.RESET + ChatColor.RED + "*");
    17. heal.setItemMeta(healMeta);
    18.  
    19. inv.setItem(0, heal);
    20.  
    21. player.openInventory(inv);
    22. }
    23. @SuppressWarnings("deprecation")
    24. @EventHandler
    25. public void onInventoryClick(InventoryClickEvent event) {
    26. if(!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("Test123"))
    27. return;
    28. Player player = (Player) event.getWhoClicked();
    29. event.setCancelled(true);
    30.  
    31. if(event.getCurrentItem()==null || event.getCurrentItem().getType()==Material.AIR||!event.getCurrentItem().hasItemMeta()){
    32. player.closeInventory();
    33. return;
    34. }
    35. switch(event.getCurrentItem().getType()){
    36. case POTION:
    37. Bukkit.dispatchCommand((CommandSender) player, "heal");
    38. player.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 0);
    39. player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 20, 5);
    40. player.closeInventory();
    41. break;
    42. default:
    43. player.closeInventory();
    44. break;
    45. }
    46. }


    Is it true? It doesn't works..
     
  7. Offline

    Gamesareme

    XFarwar No, I believe it would be
    Code:
    if(label.equalsIgnoreCase("gui")){
        Player p = (Player) sender;
        openGUI(p);
    }
     
  8. Offline

    XFarwar

    Ok works, thanks.
    Now, I've:
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("nightvision")){
    2. if(! sender.hasPermission("mypannel.utility.nightvision")) {
    3. sender.sendMessage(Main.PREFIX + ChatColor.RED + "You don't have plugin permission for this command.");
    4. return true;
    5. } if(! (sender instanceof Player)) {
    6. sender.sendMessage("You must be a player to perform this command.");
    7. return true;
    8. }
    9. Player p = (Player) sender;
    10. p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 10000, 10));
    11. p.sendMessage(ChatColor.BLUE + "NightVision is now " + ChatColor.RED + "On!");

    It is true, It works. I would a simple stuff...If Itype /nightvision plugin gives me nightvison, and if I type /nightvision plugin takes me nightvision effects. Do you understand? MrlolDestructo

    No helps? :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  9. Offline

    MrlolDestructo

    Have you put the command in your plugin.yml?
     
  10. Offline

    XFarwar

    Questions is differents... I would that /nightvision will turn on/off nightvision effect when used :D
     
  11. Offline

    Guichaguri

    Code:java
    1. if(p.hasPotionEffect(PotionEffectType.NIGHT_VISION)) {
    2. p.removePotionEffect(PotionEffectType.NIGHT_VISION);
    3. // DISABLED
    4. } else {
    5. p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 10000, 10));
    6. // ENABLED
    7. }
     
    Konkz likes this.
  12. Offline

    XFarwar

    It works! Thanks!
     
Thread Status:
Not open for further replies.

Share This Page