Help with plugin dealing with a sign click event

Discussion in 'Plugin Development' started by Whomp54, Jul 6, 2013.

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

    Whomp54

    Here is my code. I get no errors and the console looks fine. It may be internal but i dont know how to fix it.

    Code:java
    1. import org.bukkit.ChatColor;
    2. import org.bukkit.Location;
    3. import org.bukkit.Material;
    4. import org.bukkit.World;
    5. import org.bukkit.block.Sign;
    6. import org.bukkit.entity.Creeper;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class MobSigns extends JavaPlugin{
    14.  
    15. @EventHandler
    16. public void mobsigns(PlayerInteractEvent e){
    17. Sign sign = (Sign) e.getClickedBlock().getState();
    18. Player player = e.getPlayer();
    19. Location location = player.getLocation();
    20. World w = player.getWorld();
    21. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    22. if(e.getClickedBlock().getType() == Material.SIGN || e.getClickedBlock().getType() == Material.SIGN_POST || e.getClickedBlock().getType() == Material.WALL_SIGN){
    23. if(sign.getLine(0).equalsIgnoreCase("[MobSigns]") & sign.getLine(1).equalsIgnoreCase("Creeper")){
    24. w.spawn(location, Creeper.class);
    25. player.sendMessage(ChatColor.GREEN+"The sign worked!");
    26. }
    27. }
    28. }
    29. }
    30. }
     
    xTrollxDudex likes this.
  2. Offline

    adam753

    Code:
    Sign sign = (Sign) e.getClickedBlock().getState();
    It looks like you're casting everything the player interacts with to a sign, that is, casting something to a sign every time PlayerInteractEvent gets fired. I'm surprised that's not giving runtime errors by the bucketful. Declare that variable after checking if the block is a sign.
     
  3. Offline

    Whomp54

    I tried that and it still doesnt work. Anything else you see?
     
  4. Offline

    xTrollxDudex

    Whomp54
    The listener class implements Listener, not extends JavaPlugin.
    Also make sure that the listener class is registered in the JavaPlugin class. Ask me if you have questions
     
  5. Offline

    Whomp54

    How do i exactly do that? lol
     
  6. Offline

    xTrollxDudex

    Whomp54
    Seriously? wiki.bukkit.org/Plugin_Tutorial
    You have 2 classes. The first the main one. The second the listener.
    The main class has 2 methods for a command less plugin, which seems to be the level you are at right now.
    Code:java
    1. //the main class extends JavaPlugin
    2. public class Main extends JavaPlugin{
    3. //see? ------------------------ that's extends JavaPlugin
    4.  
    5. //method 1: the onEnable()
    6. public void onEnable(){
    7. //no matter what, all plugins will have this method
    8. //register your listener class
    9. getServer().getPluginManager().registerEvents(new Listener(), this);
    10. //the new Listener() creates a new class called Listener. Name Listener whatever you want. Keep track of them tho
    11. }
    12.  
    13. //method 2: the onDisable()
    14. public void onDisable(){
    15. //all plugins will also have this method
    16. //when you get more advanced, you'll put stuff in here
    17. //but for now leave these blank
    18. }
    19. }
    20.  
    21. //THE LISTENER CLASS
    22. //the listener class implements Listener.
    23. public class Listener implements Listener{
    24. //the name of the class must be the name of the name when you registered the class in the main class
    25. //notice after the name there is implements Listener
    26.  
    27. //this will be the EventHandler
    28. @EventHandler
    29. //and the listening part
    30. public void mobsigns(PlayerInteractEvent e){
    31. Sign sign = (Sign) e.getClickedBlock().getState();
    32. Player player = e.getPlayer();
    33. Location location = player.getLocation();
    34. World w = player.getWorld();
    35. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    36. if(e.getClickedBlock().getType() == Material.SIGN || e.getClickedBlock().getType() == Material.SIGN_POST || e.getClickedBlock().getType() == Material.WALL_SIGN){
    37. if(sign.getLine(0).equalsIgnoreCase("[MobSigns]") && sign.getLine(1).equalsIgnoreCase("Creeper")){
    38. w.spawn(location, Creeper.class);
    39. player.sendMessage(ChatColor.GREEN+"The sign worked!");
    40. //close off brackets
    41. }
    42. }
    43. }
    44. }
    45. }

    If you have any further more questions, just ask! I'll be happy to help

    EDIT: line 37 MUST have 2 && before sign.getLine()...... I'll correct it.
     
  7. Offline

    Whomp54

    WOOHOO it worked! thanks xTrollxDudex you rock!

    Only one problem, otherwise everything is fine. I get an error like this:

    [SEVERE] Could not pass event PlayerInteractEvent to MobSigns v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at org.bukkit.craftbukkit.v1_6_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191)
    at net.minecraft.server.v1_6_R1.PlayerInteractManager.interact(PlayerInteractManager.java:373)
    at net.minecraft.server.v1_6_R1.PlayerConnection.a(PlayerConnection.java:649)
    at net.minecraft.server.v1_6_R1.Packet15Place.handle(SourceFile:58)
    at net.minecraft.server.v1_6_R1.NetworkManager.b(NetworkManager.java:293)
    at net.minecraft.server.v1_6_R1.PlayerConnection.d(PlayerConnection.java:118)
    at net.minecraft.server.v1_6_R1.ServerConnection.b(SourceFile:37)
    at net.minecraft.server.v1_6_R1.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_6_R1.MinecraftServer.t(MinecraftServer.java:590)
    at net.minecraft.server.v1_6_R1.DedicatedServer.t(DedicatedServer.java:226)
    at net.minecraft.server.v1_6_R1.MinecraftServer.s(MinecraftServer.java:486)
    at net.minecraft.server.v1_6_R1.MinecraftServer.run(MinecraftServer.java:419)
    at net.minecraft.server.v1_6_R1.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_6_R1.block.CraftBlockState cannot be cast to org.bukkit.block.Sign
    at com.gmail.thetechwiz123.msigns.SignListener.MobSigns(SignListener.java:19)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 16 more

    Any idea?

    Edit: Fixed it myself lol

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

    thomasb454

  9. Offline

    Whomp54

  10. Offline

    thomasb454

    I've got this code:
    Code:java
    1. package com.firenation.signs;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Sign;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Main extends JavaPlugin implements Listener {
    14.  
    15. public void onEnable() {
    16. Bukkit.getPluginManager().registerEvents(this, this);
    17. }
    18.  
    19. public void onDisable() {
    20. }
    21.  
    22. public void PlayerInteractEvent(PlayerInteractEvent e) {
    23. Sign sign = (Sign) e.getClickedBlock().getState();
    24. Player player = e.getPlayer();
    25. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    26. if (e.getClickedBlock().getType() == Material.SIGN
    27. || e.getClickedBlock().getType() == Material.SIGN_POST
    28. || e.getClickedBlock().getType() == Material.WALL_SIGN) {
    29. if (sign.getLine(0).equalsIgnoreCase("[SINFO]")
    30. && sign.getLine(1).equalsIgnoreCase("Factions")) {
    31. player.sendMessage(ChatColor.GREEN + "The sign worked!");
    32. }
    33. }
    34. }
    35. }
    36.  
    37. }


    but it don't work? (the plugin don't seem to be loading?)
     
  11. Offline

    xTrollxDudex

    thomasb454
    You cast (Sign) to clickedblock after you make sure it is a sign. Same for you Whomp54
    Also Thomas, add @EventHandler and import it above public void onPlayerInterctEvent(pl.....
     
  12. Offline

    thomasb454

    xTrollxDudex
    I added @EventHandler just as I posted it, lol.

    I've not done casting so I don't really know you what you mean?
     
  13. Offline

    xTrollxDudex

    thomasb454
    Move line 23 down to 29
    Move everything 29 and down down 1
     
  14. Offline

    thomasb454

    xTrollxDudex

    I've done this, but it still don't load.

    Plugin.yml:
    Code:
    main: com.firenation.signs.Main
    version: 0.0.1
    name: Signs
    author: Thomasb454
    Actual code:
    Code:java
    1. package com.firenation.signs;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Sign;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Main extends JavaPlugin implements Listener {
    15.  
    16. public void onEnable() {
    17. Bukkit.getPluginManager().registerEvents(this, this);
    18. }
    19.  
    20. public void onDisable() {
    21. }
    22.  
    23. @EventHandler
    24. public void PlayerInteractEvent(PlayerInteractEvent e) {
    25. Player player = e.getPlayer();
    26. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    27. if (e.getClickedBlock().getType() == Material.SIGN
    28. || e.getClickedBlock().getType() == Material.SIGN_POST
    29. || e.getClickedBlock().getType() == Material.WALL_SIGN) {
    30. Sign sign = (Sign) e.getClickedBlock().getState();
    31. if (sign.getLine(0).equalsIgnoreCase("[SINFO]")
    32. && sign.getLine(1).equalsIgnoreCase("Factions")) {
    33. player.sendMessage(ChatColor.GREEN + "The sign worked!");
    34. }
    35. }
    36. }
    37. }
    38. }
     
  15. Offline

    xTrollxDudex

    thomasb454
    Nvm use event.getLine(<line number>) to get the line
     
  16. Offline

    thomasb454

    xTrollxDudex
    Now it wants me to cast again?




    * edit*
    Since I'm new to Bukkit I tried to debug it, I changed the 'e' to 'sign'.
    I'm going to try this out.

    *edit*
    Nope, still won't load.
    http://prntscr.com/1ef2z0

    xTrollxDudex Any ideas? (It's not loading, if you need my plugin.yml just ask0

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

    xTrollxDudex

    thomasb454
    Add an @Override above your onEnable method :)

    EDIT: and your ondisable()
     
  18. Offline

    thomasb454

    xTrollxDudex

    I've done that, it just won't work.:(

    Edit, I've included my plugin.yml + the code.

    Code:
    Code:java
    1. package com.firenation.signs;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Sign;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Main extends JavaPlugin implements Listener {
    15.  
    16. @Override
    17. public void onEnable() {
    18. Bukkit.getPluginManager().registerEvents(this, this);
    19. }
    20. @Override
    21. public void onDisable() {
    22. }
    23.  
    24. @EventHandler
    25. public void PlayerInteractEvent(PlayerInteractEvent e) {
    26. Player player = e.getPlayer();
    27. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    28. if (e.getClickedBlock().getType() == Material.SIGN
    29. || e.getClickedBlock().getType() == Material.SIGN_POST
    30. || e.getClickedBlock().getType() == Material.WALL_SIGN) {
    31. Sign sign = (Sign) e.getClickedBlock().getState();
    32. if (sign.getLine(0).equalsIgnoreCase("[SINFO]")
    33. && (sign.getLine(1).equalsIgnoreCase("Factions"))) {
    34. player.sendMessage(ChatColor.GREEN + "The sign worked!");
    35. }
    36. }
    37. }
    38. }
    39. }


    plugin.yml
    Code:java
    1. main: com.firenation.signs.Main
    2. description: Sends user a message
    3. name: Signs
    4. version: 0.0.1
    5. author: Thomasb454
     
  19. Offline

    xTrollxDudex

    thomasb454
    Post the entire class picture I need to see where all the errors are
     
  20. Offline

    thomasb454

    xTrollxDudex
    There are no errors.

    It just won't load on my server.
     
  21. Offline

    xTrollxDudex

    thomasb454
    Oh my god screw myself. Your register line should not start with Bukkit. For future reference, this is the line
    Code:java
    1. getServer().getPluginManager().registerEvents(<class>, <plugin>);

    So replace your Bukkit. In the onEnable() with getServer().
     
  22. Offline

    thomasb454

  23. Offline

    xTrollxDudex

  24. Offline

    thomasb454

  25. Offline

    xTrollxDudex

    thomasb454
    This is super wierd. Even I don't know how to fix this. Close out of your server and click on your start.bat again I need to see the stacktrace
     
  26. Offline

    thomasb454

  27. Offline

    xTrollxDudex

    thomasb454
    No wonder! You have a spigot server. Spigot has a slightly different API, some of the guys from the spigot forums might help :). I can also tell that the out put isn't the entire thing. Looks like well need someone else to help....
     
  28. Offline

    thomasb454

    xTrollxDudex
    Ah, well I didn't think Spigot would make this not work, I'll try Bukkit. =D

    xTrollxDudex
    Still don't work. =(

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

    xTrollxDudex

    thomasb454
    Okay. I really can't follow you everywhere with this man. You just have to experiment. I really wish I can help but this issue really is way out of hand
     
  30. Offline

    thomasb454

    * bump *
    anyone please?
    Thanks btw^
     
Thread Status:
Not open for further replies.

Share This Page