Help needed for simple chest event code.

Discussion in 'Plugin Development' started by Mixerman123, Apr 17, 2014.

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

    Mixerman123

    Hi!
    So I have been working on this plugin for ages, and this one, silly bug, over an hour. It seems ridiculous that I can make other much more advanced things work, yet this doesn't. I am making a chest protection plugin, and no matter what I code, nothing seems to fire! I originally made this with the newest 1.7.8 craftbukkit, but then I realised that may be causing the issue, so I ported back to both 1.7.5 and 1.7.2.

    I have tried the main two ways of seeing if a chest was opened, the player interact event, which, for some strange reason doesn't fire at all, even before it goes through check if you right clicked a chest. Really odd! Nothing comes up with the below code.
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. event.getPlayer().sendMessage("Interacted");
    4.  
    5. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    6. Block blk = event.getClickedBlock();
    7. event.getPlayer().sendMessage("Right clicked a block");
    8.  
    9. if(blk.getType().equals(Material.CHEST)) {
    10. event.getPlayer().sendMessage("that was a chest");
    11.  
    12. }
    13. }
    14. }


    I even tried the way of doing it with the onInventoryOpenEvent, same thing. The event isn't fired.

    Obviosly this makes you think. "Gee, Mixer doesn't know how to setup events :p" but all my other events work!?

    Any help I would love. I am sick of trying the same thing over and over :p
    Thanks Mixer.
     
  2. Offline

    IkBenHarm

    Mixerman123
    since you have other events you probably did, but did you register the event?
     
  3. Offline

    Mixerman123

    IkBenHarm
    Sorry, I am kind of new to this. I thought you just did @ EventHandler and that function would run. Sorry, still getting my head around event programming :) Here's the code I have so far. Abit messy, but works as usual so far.
    Code:java
    1. package com.mixerman123.eagleeconomylock;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.configuration.file.YamlConfiguration;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17.  
    18.  
    19. public final class EagleEconomyLock extends JavaPlugin implements Listener{
    20.  
    21. FileConfiguration ChestDataFile;
    22. File dfile;
    23.  
    24.  
    25.  
    26. @Override
    27. public void onEnable() {
    28. // TODO Insert logic to be performed when the plugin is enabled
    29. getLogger().info("EagleEconomyLock is up and running.");
    30.  
    31. //Creates a folder for the chest data file
    32. if (!this.getDataFolder().exists()) {
    33. getLogger().info(ChatColor.AQUA+"It looks like this is the first time EEL has been run. Setting up some files now...");
    34. this.getDataFolder().mkdir();
    35. }
    36.  
    37. //Then make the file
    38. dfile = new File(this.getDataFolder(), "chestData.yml");
    39. if (!dfile.exists()) {
    40. try {
    41. dfile.createNewFile();
    42. }
    43. catch (IOException e) {
    44. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create the chest save file! Contact Mixerman123"+ChatColor.RED+" ASAP!!");
    45. }
    46. }
    47.  
    48. //Now the file exists in the folder!
    49.  
    50.  
    51. ChestDataFile = YamlConfiguration.loadConfiguration(dfile);
    52. }
    53.  
    54. public FileConfiguration getData() {
    55. return ChestDataFile;
    56. }
    57.  
    58. public void saveData() {
    59. try {
    60. ChestDataFile.save(dfile);
    61. }
    62. catch (IOException e) {
    63. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save data.yml!");
    64. }
    65. }
    66.  
    67. public void reloadData() {
    68. ChestDataFile = YamlConfiguration.loadConfiguration(dfile);
    69. }
    70.  
    71. @Override
    72. public void onDisable() {
    73. // TODO Insert logic to be performed when the plugin is disabled
    74. getLogger().info("EagleEconomyLock is disabled! If this isn't because of a server shutdown/reload, contact Mixerman123");
    75. }
    76.  
    77.  
    78.  
    79.  
    80. // @EventHandler //ALWAYS use this before events
    81. // public void onInventoryOpenEvent(InventoryOpenEvent e){
    82. // Bukkit.broadcastMessage("Inv opeded");
    83. // if (e.getInventory().getHolder() instanceof Chest){
    84. // Bukkit.broadcastMessage("Inv opeded and was chest");
    85. // Chest c = (Chest) e.getInventory().getHolder();
    86. //
    87. // Chest lockedChest = c;
    88. // Player p = (Player) e.getPlayer();
    89. // p.sendMessage("The players who are aloud to open this chest are: "+getData().getInt("ChestDataForChestAt."+lockedChest.getX()+","+lockedChest.getY()+","+lockedChest.getZ()+".GroupPermitted:"));
    90. // }
    91. // }
    92.  
    93.  
    94.  
    95.  
    96.  
    97. @Override
    98. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    99. if (cmd.getName().equalsIgnoreCase("lock")) { // If the player typed /basic then do the following...
    100. if (!(sender instanceof Player)) {
    101. sender.sendMessage("This command locks the chest you're standing on, but you're "+ChatColor.RED+"not a player!");
    102. } else {
    103. Player player = (Player) sender;
    104.  
    105. if(player.getLocation().add(0, 0, 0).getBlock().getType().name()=="CHEST"){
    106. //We now know the block is a chest so we can turn that block into a chest for further use in code
    107. Block lockedChest = player.getLocation().add(0, 0, 0).getBlock();
    108. //A silly message to the locker :P
    109. player.sendMessage("Locking this chest with "+ChatColor.BOLD+"[E]agle [E]conomy [L]ock");
    110.  
    111. //Create an id for this chest that is based of its location
    112. String ThisChestsId = "ChestDataForChestAt."+lockedChest.getX()+","+lockedChest.getY()+","+lockedChest.getZ()+".";
    113. ChestDataFile.set(ThisChestsId+"X:", lockedChest.getX());
    114. ChestDataFile.set(ThisChestsId+"Y:", lockedChest.getY());
    115. ChestDataFile.set(ThisChestsId+"Z:", lockedChest.getZ());
    116. ChestDataFile.set(ThisChestsId+"GroupPermitted:", player.getName());//Players allowed
    117. saveData();
    118.  
    119. }else{
    120. player.sendMessage("To lock a chest, you need to stand on top of one.");
    121. }
    122.  
    123. }
    124.  
    125. return true;
    126. }
    127. return false;
    128. }
    129. }
    130.  
    Indentation got screwed.
     
  4. Offline

    IkBenHarm

    Mixerman123
    use in your onEnable():

    Bukkit.getServer().getPluginManager().registerEvents(*class where events are*, this);
     
  5. Offline

    Mixerman123

    Oh dang. Super Obvious. Thank you so, so, very much. You are a life saver. Why hadn't I thought of this?! *Clap clap* Now go build a time machine so you could have told me this 2 hours ago :D Thanks so much man. (I really need to look at the obvious more) THANKS!
     
  6. Offline

    IkBenHarm

Thread Status:
Not open for further replies.

Share This Page