Crate Plugin Help!

Discussion in 'Plugin Development' started by 1337Pluginz, Jul 19, 2014.

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

    1337Pluginz

    Hi,
    I need some help with a crate plugin I am trying to make! This is my code so far
    Code:java
    1. package me.lambo.craft.crates;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Chest;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.Action;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Crates extends JavaPlugin implements Listener {
    14.  
    15. Logger log;
    16.  
    17. public void onEnable() {
    18. log = this.getLogger();
    19. log.info("LamboCraft-Crates v1.0 Has Been Enabled!");
    20. getServer().getPluginManager().registerEvents(this, this);
    21. }
    22.  
    23. public void onDisable() {
    24. log.info("LamboCraft-Crates v1.0 Has Been Disabled!");
    25. }
    26.  
    27. public void blockChestInterract(PlayerInteractEvent event) {
    28. Chest chest = (Chest) event.getClickedBlock().getState();
    29. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    30. if (chest.getInventory().getName().equals("Crate") && event.getItem().getType().equals(Material.TRIPWIRE_HOOK));
    31. event.getPlayer().getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
    32. }
    33. }
    34. }
    35.  

    I was wondering how I would be able to cancel the event of a chest opening if the name of the chest is "Crate". I understand how to cancel the event of a chest opening but not really sure how to if the name of the chest is "Crate". Second of all, if the name of the chest is "Crate" and the player is holding a tripwire hook with a certain color code and name "&6&l&nLambo-Key" when they right click the chest with the name "Crate" they will recieve a diamond. I am pretty stumped on what to do at the moment. If you could help that would be great!
     
  2. Offline

    JustinsCool15

    Forgot to add @EventHandler above the event
     
  3. Offline

    1337Pluginz

    Just forgot about that! Thanks :) Would you mind helping me with the other things JustinsCool15
     
  4. Offline

    JustinsCool15

    1337Pluginz
    For the trip hook thing you would use
    Code:java
    1. if(e.getItem().hasItemMeta() && e.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.BOLD + "" + ChatColor.UNDERLINE + "Labo-Key")){
    2. //code to give them diamond here.
    3. }


    Idk about the Crate name thing.

    Also if you don't know how to give a player an item I can help with that too.
     
  5. Offline

    1337Pluginz

    JustinsCool15 Thanks!

    Just stuck on the Chest as it is giving me a NullPointerException at line 30 in the code above. It gives you the exception when you sneak right click, you break ANY block, place ANY block! I am really stumped now :p
     
  6. Offline

    Niknea

    1337Pluginz How would you be naming the chest? Do you mean a GUI? If so get the inventory name, ex.
    Code:Java
    1. inventory.getName().equalsIgnoreCase("Chest"){ /*Code*/ }
     
  7. Offline

    1337Pluginz

    So basically, if the name of this chest is "Crate" the chest will not open but you will recieve a diamond and the tripwire hook (the key) will be removed from the inventory if you right click on the chest. Niknea Thanks for your help, just need some further understanding to make this work!
     
  8. Offline

    Niknea

    1337Pluginz Correct, use my code in an if statement, if it passes give the player the items and cancel the event.
     
  9. Offline

    1337Pluginz

    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. public void blockChestInterract(PlayerInteractEvent event) {
    3. Chest chest = (Chest) event.getClickedBlock().getState();
    4. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    5. if (((Logger) chest).getName().equalsIgnoreCase("Chest") && event.getItem().getType().equals(Material.TRIPWIRE_HOOK)) {
    6. if (event.getItem().hasItemMeta() && event.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GOLD + "" + ChatColor.BOLD + "" + ChatColor.UNDERLINE + "Lambo-Key")) {
    7. event.getPlayer().getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
    8. event.setCancelled(true);
    9. }
    10. }
    11. }
    12. }


    Only added the Cast since Eclipse told me to.
     
  10. Offline

    endoml

    1337Pluginz Niknea's method is for getting the name of the inventory, not the block itself.
     
  11. Offline

    JustinsCool15

    1337Pluginz
    Little tip, in Eclipse if you press Ctrl + A then Ctrl + I it will organize all your code's spacings.

    Also are you having any more issues?
     
  12. Offline

    1337Pluginz

    I really cant get this to work :( Its giving me NullPointerExceptions here and there! Would anyone mind reworking my code to get it to work please?
     
  13. Offline

    Drew1080

    1337Pluginz
    Having one of us just re-write your code to make it work isn't going to help you learn.
    Paste the stack trace with the NPE's and we will help you fix them.
    If you just want someone to code a plugin for you, you should of have made a request in the Plugin Requests forum.
     
  14. Offline

    JustinsCool15

    1337Pluginz

    Post the updated code and what line is making the Exception (In the updated code.)?
     
  15. Offline

    1337Pluginz

    Code:java
    1. package me.lambo.craft.crates;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Chest;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.EventPriority;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class Crates extends JavaPlugin implements Listener {
    17.  
    18. Logger log;
    19.  
    20. public void onEnable() {
    21. log = this.getLogger();
    22. log.info("LamboCraft-Crates v1.0 Has Been Enabled!");
    23. getServer().getPluginManager().registerEvents(this, this);
    24. }
    25.  
    26. public void onDisable() {
    27. log.info("LamboCraft-Crates v1.0 Has Been Disabled!");
    28. }
    29.  
    30. @EventHandler(priority = EventPriority.HIGHEST)
    31. public void blockChestInterract(PlayerInteractEvent event) {
    32. Chest chest = (Chest) event.getClickedBlock().getState();
    33. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    34. if (((Logger) chest).getName().equalsIgnoreCase("Chest") && event.getItem().getType().equals(Material.TRIPWIRE_HOOK)) {
    35. if (event.getItem().hasItemMeta() && event.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GOLD + "" + ChatColor.BOLD + "" + ChatColor.UNDERLINE + "Lambo-Key")) {
    36. event.getPlayer().getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
    37. event.setCancelled(true);
    38. }
    39. }
    40. }
    41. }
    42. }
    43.  


    Code:
    [Server thread/ERROR]: Could not pass event PlayerInteractEvent to LamboCraft-Crates v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:216) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PlayerInteractManager.interact(PlayerInteractManager.java:374) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:629) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.a(SourceFile:60) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.handle(SourceFile:9) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R3.block.CraftChest cannot be cast to java.util.logging.Logger
        at me.lambo.craft.crates.Crates.blockChestInterract(Crates.java:34) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_11]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_11]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_11]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_11]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        ... 15 more
    I hope we can work together to fix my plugin guys! Thanks for all the contributions and tips so far :)

    Anyone have a solution?

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

    JustinsCool15

    1337Pluginz
    Remove the ((logger) chest) and just use chest on line 34
     
  17. Offline

    1337Pluginz

    Method name is undefined for the type Chest

    Code:java
    1. if (chest.name().equalsIgnoreCase("Crate") && event.getItem().getType().equals(Material.TRIPWIRE_HOOK)) {
     
  18. Offline

    JustinsCool15

    1337Pluginz
    Try using (Chest) instead of ((logger) Chest)
     
  19. Offline

    1337Pluginz

    Method getName() is undefined for type Chest :p

    BEFORE
    Code:java
    1. if (((Logger) chest).getName().equalsIgnoreCase("Chest") && event.getItem().getType().equals(Material.TRIPWIRE_HOOK)) {


    AFTER
    Code:java
    1. if (chest.getName().equalsIgnoreCase("Chest") && event.getItem().getType().equals(Material.TRIPWIRE_HOOK)) {


    Code:java
    1. if (((Chest) chest).name().equalsIgnoreCase("Chest") && event.getItem().getType().equals(Material.TRIPWIRE_HOOK)) {


    Like this right? If yes, it still gives the Method name is undefined for the type Chest

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

    endoml

    1337Pluginz That's because there is no getName() method for the chest. Once you place a block, you lose all the ItemMeta from the ItemStack.
     
  21. Offline

    1337Pluginz

    So what can I do then endoml ?
     
  22. Offline

    endoml

    1337Pluginz You could make an arraylist of the chests that you want to give diamonds
     
  23. Offline

    1337Pluginz

  24. Offline

    Garris0n

    That's not what hasItemMeta() does.
     
  25. Offline

    FabeGabeMC

    1337Pluginz
    I don't see the reason for making that thread if what you want is your code to be fixed.
     
Thread Status:
Not open for further replies.

Share This Page