Trouble creating a GUI method.

Discussion in 'Plugin Development' started by iCancer, Jan 14, 2017.

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

    iCancer

    Code:JAVA
    1.  
    2.  
    3. public void inventoryAction(InventoryClickEvent clickEvent, Inventory clickedInventory, ItemStack clickedItem, Player whoClicked, Inventory inventoryToOpen) {
    4.  
    5. if (clickEvent == null) return;
    6.  
    7. if (clickedInventory == null) return;
    8.  
    9. if (clickedItem == null) return;
    10.  
    11. if (whoClicked == null) return;
    12.  
    13. if (inventoryToOpen == null) return;
    14.  
    15. if (clickEvent.getInventory() != clickedInventory) clickEvent.setCancelled(true); return;
    16.  
    17. if (clickEvent.getCurrentItem().getItemMeta().getDisplayName().equals(clickedItem.getItemMeta().getDisplayName())) {
    18.  
    19. whoClicked.openInventory(inventoryToOpen);
    20.  
    21. return;
    22.  
    23. }
    24.  
    25. }
    26.  


    I am trying to use the above code to quickly open an inventory depending on the clickedInventory, clickedItem, the event, and the inventory that is supposed to open. the last if statement is giving me an error, that it is "unreachable".


    EDIT: Another issue I am having is Listening to the Click Event. I have the code at the bottom so that you can see. When I try to open up another inventory by using another method, It just closes the Inventory with no errors.

    Code:JAVA
    1.  
    2.  
    3.  
    4. package ProjectX.Punish.Events;
    5.  
    6.  
    7.  
    8. import org.bukkit.Bukkit;
    9.  
    10. import org.bukkit.entity.Player;
    11.  
    12. import org.bukkit.event.EventHandler;
    13.  
    14. import org.bukkit.event.Listener;
    15.  
    16. import org.bukkit.event.inventory.InventoryClickEvent;
    17.  
    18. import org.bukkit.plugin.Plugin;
    19.  
    20.  
    21.  
    22. import ProjectX.Punish.Inventory.PunishGUI;
    23.  
    24. import ProjectX.Punish.Util.InventoryActions;
    25.  
    26.  
    27.  
    28. public class ClickEvent extends InventoryActions implements Listener {
    29.  
    30.  
    31.  
    32. public ClickEvent(Plugin plugin) {
    33.  
    34. Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
    35.  
    36. }
    37.  
    38.  
    39. @EventHandler
    40.  
    41. public void onInventoryClick(InventoryClickEvent ICE) {
    42.  
    43. Player clicker = (Player) ICE.getWhoClicked();
    44.  
    45. PunishGUI inv = new PunishGUI();
    46.  
    47. inventoryAction(ICE, inv.getMajorInv(), inv.getMajorItem(), clicker, inv.getMajorInv());
    48.  
    49. }
    50.  
    51.  
    52.  
    53. }
    54.  
    55.  
    56.  
    57.  
    58.  
    59. And the other class
    60.  
    61.  
    62.  
    63.  
    64. package ProjectX.Punish.Util;
    65.  
    66.  
    67.  
    68.  
    69.  
    70. import org.bukkit.entity.Player;
    71.  
    72. import org.bukkit.event.inventory.InventoryClickEvent;
    73.  
    74. import org.bukkit.inventory.Inventory;
    75.  
    76. import org.bukkit.inventory.ItemStack;
    77.  
    78.  
    79.  
    80. import ProjectX.Punish.Core.Misc;
    81.  
    82.  
    83.  
    84. public class InventoryActions implements Misc {
    85.  
    86.  
    87.  
    88.  
    89. public void inventoryAction(InventoryClickEvent clickEvent, Inventory clickedInventory, ItemStack clickedItem, Player whoClicked, Inventory inventoryToOpen) {
    90.  
    91. if (clickEvent == null) return;
    92.  
    93. if (clickedInventory == null) return;
    94.  
    95. if (clickedItem == null) return;
    96.  
    97. if (whoClicked == null) return;
    98.  
    99. if (inventoryToOpen == null) return;
    100.  
    101. if (clickEvent.getInventory().getName() != clickedInventory.getName()) { clickEvent.setCancelled(true); whoClicked.closeInventory(); return; }
    102.  
    103. if (clickEvent.getCurrentItem().getItemMeta().getDisplayName().equals(clickedItem.getItemMeta().getDisplayName())) { whoClicked.openInventory(inventoryToOpen); return; }
    104.  
    105. }
    106.  
    107.  
    108.  
    109.  
    110.  
    111.  
    112. }
    113.  
    114.  


    @Zombie_Striker & @JanTuck
     
    Last edited: Jan 14, 2017
  2. Offline

    Zombie_Striker

    @iCancer
    That is because you forgot about encapsulation. Either remove the return or use
     
  3. Offline

    iCancer

    @Zombie_Striker So, if I just used a return, it would be fine to use only one line of code instead of using encapsulation??
     
  4. Offline

    JanTuck

    Yes, if you just used return; you could.

    Code:java
    1.  
    2. boolean trues = true;
    3. if ( trues )
    4. return;
    5.  

    Normal if statements without encapsulation runs the next line but not any other.
    Example
    Code:java
    1.  
    2. boolean trues = true;
    3. if ( trues )
    4. trues = false; // < This gets run if the if statement is true
    5. return; // < This always gets run
    6. trues = true; // < This is unreachable
    7.  


    Code:java
    1.  
    2. bolean trues = true;
    3. if ( trues ){
    4. trues = false; // < This gets run INSIDE of the if statement
    5. return; // < This gets run INSIDE of the if statement
    6. }
    7. trues = true; // < Now this can get reached.
    8.  
     
    Last edited: Jan 14, 2017
    Zombie_Striker likes this.
  5. Offline

    iCancer

    Okay, thanks! Appreciate the help!
     
Thread Status:
Not open for further replies.

Share This Page