Solved EventHandler in Main class

Discussion in 'Plugin Development' started by BurnerDiamond, Dec 2, 2014.

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

    BurnerDiamond

    Is it possible to get my Player player = (Player) event.getWhoClicked(); from my eventhandler class to my main class?

    Code:java
    1. package me.bd.chest;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.inventory.InventoryClickEvent;
    7. import org.bukkit.inventory.Inventory;
    8. import org.bukkit.inventory.ItemStack;
    9.  
    10. public class ChestEvent implements Listener {
    11. public ChestEvent(Chest plugin) {
    12. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    13.  
    14. }
    15.  
    16. @EventHandler
    17. public static void onInventoryClick(InventoryClickEvent event) {
    18. Player player = (Player) event.getWhoClicked();
    19. ItemStack clicked = event.getCurrentItem();
    20. Inventory inventory = event.getInventory();
    21.  
    22. }
    23.  
    24.  
    25. }
    26.  


    Code:java
    1. package me.bd.chest;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.inventory.Inventory;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Chest extends JavaPlugin {
    14. public static Chest plugin;
    15.  
    16. @Override
    17. public void onDisable() {
    18.  
    19. }
    20.  
    21. @Override
    22. public void onEnable() {
    23. new ChestEvent(this);
    24.  
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    28.  
    29. if(!(sender instanceof Player)) {
    30. sender.sendMessage(ChatColor.YELLOW + "The console cannot use this command!");
    31. return true;
    32. }
    33.  
    34. Inventory Chest = Bukkit.createInventory(null, 9, "Getting Wood Achievement Prize!");
    35.  
    36. Chest.setItem(2, new ItemStack(Material.COOKED_BEEF))
    37. Chest.setItem(5, new ItemStack(Material.WOOD_SWORD));
    38. Chest.setItem(8, new ItemStack(Material.FISHING_ROD));
    39.  
    40. if(cmd.getName().equalsIgnoreCase("wood")) {
    41.  
    42.  
    43. }
    44. return false;
    45.  
    46. }
    47. }
    48.  
     
  2. Offline

    StaticJava

    What exactly are you trying to do? Additionally, why are you registering your event in your Listener class?
     
  3. Offline

    BurnerDiamond

    I'm trying to do when the player write wood! It will open my custom chest! With the three items inside! When he clicks one of them it will close the inventory... give him the items I specify and done!
     
  4. Offline

    teej107

    Why not? You can register events anywhere in the code if you provide the method with the correct arguments.

    BurnerDiamond It's best to handle the event in the method (or methods) that it is called in. To answer your question, you'll need to use setters and/or getters to put the player who clicked in another class.
     
  5. Offline

    StaticJava

    So what is the issue?

    I would try using this:

    Code:java
    1. package me.bd.chest;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.inventory.Inventory;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Chest extends JavaPlugin {
    14. public static Chest plugin;
    15.  
    16. @Override
    17. public void onDisable() {
    18.  
    19. }
    20.  
    21. @Override
    22. public void onEnable() {
    23. new ChestEvent(this);
    24.  
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    28.  
    29. if (commandLabel.equalsIgnoreCase("wood") {
    30. if(!(sender instanceof Player)) {
    31. sender.sendMessage(ChatColor.YELLOW + "The console cannot use this command!");
    32. return true;
    33. }
    34.  
    35. Inventory Chest = Bukkit.createInventory(null, 9, "Getting Wood Achievement Prize!");
    36.  
    37. Chest.setItem(2, new ItemStack(Material.COOKED_BEEF))
    38. Chest.setItem(5, new ItemStack(Material.WOOD_SWORD));
    39. Chest.setItem(8, new ItemStack(Material.FISHING_ROD));
    40. return false;
    41.  
    42. }
    43. }
    44. }
    45.  
     
  6. Offline

    BurnerDiamond

    Can I have two player kinds in the same class?

    Player p = (Player) sender;
    Player player =(Player) event.getWhoClicked();
     
  7. Offline

    StaticJava

    It is generally a good habit to register events in the onEnable(). teej107
     
  8. Offline

    BurnerDiamond

    StaticJava

    The problem is I need a player sender and a player event!
     
  9. BurnerDiamond Your Player player = (Player) sender is for your command, if you want to check the player from the event then you can do things such as running a method to give them something, you can add to a list and check it later ect.
     
  10. Offline

    mythbusterma

    StaticJava

    It's also a bad habit to have a public static reference to your plugin in your JavaPlugin class. I don't see how you could go around telling anyone about habits.

    teej107 is right, it absolutely does not matter where you put it, as long as you're consistent and organized with what you do.
     
  11. Offline

    BurnerDiamond

    Could I have an example? Im failing to understand bwfcwalshy
     
  12. Offline

    StaticJava

    I never had a public static reference...? I copied his code and just rearranged things. mythbusterma
     
  13. BurnerDiamond Well check if a player clicks x then run a method such as giveADiamond(player) then your code for that could be
    Code:java
    1. public void giveADiamond(Player player){
    2. player.getInventory().addItem(new ItemStack(Material.DIAMOND));
    3. }


    So it would add a diamond to the player who clicked x.
     
  14. Offline

    fireblast709

    And what makes it a good habit?
    BurnerDiamond What exactly are you attempting to create (your description thus far is a tad vague)
     
  15. Offline

    mythbusterma

  16. Offline

    BurnerDiamond

    I want to...

    If a guy writes the command wood!
    Open my personal chest called ".........."
    With my personal items
    On the event he clicks wood sword
    I want to close the inventory
    Give him items
    Done
     
  17. Offline

    StaticJava

    I'm not exactly sure what you're trying to do, but for starters, this may improve the code:

    Code:java
    1. package me.bd.chest;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.inventory.Inventory;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Chest extends JavaPlugin {
    14. public static Chest plugin;
    15.  
    16. @Override
    17. public void onEnable() {
    18. getServer().getPluginManager().registerEvents(new ChestEvent(), this);
    19. }
    20.  
    21. @Override
    22. public void onDisable() {
    23.  
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    27.  
    28. if (commandLabel.equalsIgnoreCase("wood")) {
    29. if(!(sender instanceof Player)) {
    30. sender.sendMessage(ChatColor.YELLOW + "The console cannot use this command!");
    31. return true;
    32. }
    33.  
    34. Inventory Chest = Bukkit.createInventory(null, 9, "Getting Wood Achievement Prize!");
    35.  
    36. Chest.setItem(2, new ItemStack(Material.COOKED_BEEF))
    37. Chest.setItem(5, new ItemStack(Material.WOOD_SWORD));
    38. Chest.setItem(8, new ItemStack(Material.FISHING_ROD));
    39. return false;
    40. }
    41. }
    42. }
    43.  


    Code:java
    1. package me.bd.chest;
    2.  
    3. import org.bukkit.entity.HumanEntity;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.inventory.InventoryClickEvent;
    8. import org.bukkit.inventory.Inventory;
    9. import org.bukkit.inventory.ItemStack;
    10.  
    11. public class ChestEvent implements Listener {
    12.  
    13. @EventHandler
    14. public void onInventoryClick(InventoryClickEvent event) {
    15. HumanEntity he = event.getWhoClicked();
    16. if (he instanceof Player) {
    17. Player player = (Player) he;
    18. }
    19.  
    20. ItemStack clicked = event.getCurrentItem();
    21. Inventory inventory = event.getInventory();
    22.  
    23. //finish your method
    24. }
    25.  
    26.  
    27. }


    EDIT: Additionally, why are you creating a new instance of Inventory every time /wood is called? I would make one universal inventory in the onEnable and open that.
     
  18. StaticJava You never opened the inventory there you just made it.
     
  19. Offline

    BurnerDiamond

    When the inventory is opened!!!
    AND THE PLAYER CLICKS ONE OF THE ITEMS IN IT!
    Give him a certain set of items!

    I KNOW THAT but once I opened the inventory how would I check the play who clicked the sword?

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>

    Sorry!

    Okay I now have a p.openInventory!

    I now want to check what he clicks!

    If he clicks the wood sword I want to close the inventory and give him a wood sword!

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

    StaticJava

    Ok, I won't code the whole thing for you, but this is what you want to do:

    Code:java
    1. package me.bd.chest;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.inventory.Inventory;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Chest extends JavaPlugin {
    14. private Inventory chest;
    15.  
    16. @Override
    17. public void onEnable() {
    18. getServer().getPluginManager().registerEvents(new ChestEvent(), this);
    19. chest = Bukkit.createInventory(null, 9, "Getting Wood Achievement Prize!");
    20. chest.setItem(2, new ItemStack(Material.COOKED_BEEF));
    21. chest.setItem(5, new ItemStack(Material.WOOD_SWORD));
    22. chest.setItem(8, new ItemStack(Material.FISHING_ROD));
    23. }
    24.  
    25. @Override
    26. public void onDisable() {
    27.  
    28. }
    29.  
    30. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    31. if (commandLabel.equalsIgnoreCase("wood")) {
    32. if(!(sender instanceof Player)) {
    33. sender.sendMessage(ChatColor.YELLOW + "The console cannot use this command!");
    34. return true;
    35. } else {
    36. Player player = (Player) sender;
    37. player.openInventory(chest);
    38. return true;
    39. }
    40. }
    41. return false;
    42. }
    43. }
    44.  


    Code:java
    1. package me.bd.chest;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.entity.HumanEntity;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.inventory.InventoryClickEvent;
    9. import org.bukkit.inventory.Inventory;
    10. import org.bukkit.inventory.ItemStack;
    11.  
    12. public class ChestEvent implements Listener {
    13.  
    14. @EventHandler
    15. public void onInventoryClick(InventoryClickEvent event) {
    16. HumanEntity he = event.getWhoClicked();
    17. if (he instanceof Player) {
    18. Player player = (Player) he;
    19. Inventory inv = event.getInventory();
    20. if (inv != null) {
    21. if (inv.getName().equals("Getting Wood Achievement Prize!")) {
    22. if (event.getCurrentItem() != null) {
    23. ItemStack is = event.getCurrentItem();
    24. if (is.getType() == Material.WOOD_SWORD) {
    25. player.closeInventory();
    26. //finish your code
    27. }
    28. }
    29. }
    30. }
    31. }
    32. }
    33.  
    34.  
    35. }
     
  21. Offline

    BurnerDiamond

    Okay but can I ask you one thing? it opens the player chest right? And then it automatically goes on to the chestevent to check if any of the things I write are true?? StaticJava
     
  22. Offline

    StaticJava

    In the event class I coded above, where I have the comment, you will want to add the item to the player's inventory. BurnerDiamond Yes, that is what is happening.
     
  23. Offline

    BurnerDiamond

    Got to love coding! Sorry but why but the chest in the onEnable?
     
  24. Offline

    StaticJava

    BurnerDiamond You were creating a new Inventory instance EVERY time a player used the command, which isn't a good practice and can cause memory problems. I just created one universal inventory in the onEnable.
     
  25. Offline

    BurnerDiamond

    GEnIUS!!!
     
  26. Offline

    SuperOriginal

    BurnerDiamond On a side note, you may want to use the period punctuation mark (.) instead of the exclamation point(!) if you don't want to come across as yelling or exclaiming something :p
     
    StaticJava likes this.
  27. Offline

    StaticJava

Thread Status:
Not open for further replies.

Share This Page