error on enabling plugin - problem with listener

Discussion in 'Plugin Development' started by G4meM0ment, Apr 7, 2012.

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

    G4meM0ment

    Hi,
    I asked today a lot more questions but now I dont know any way to continue.

    I want to create a plugin which "chain" anyone, I want to do this with a simple rightClick Listener, but I need to give a lot of variables between the listeners and classes, and everything is very confusing...

    After I tried to start a server with the plugin then, this Exception was thrown:

    Code:
    [SEVERE] Could not load 'plugins\Chaintrain_v0.0.2.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ClassNotFoundException: me.G4meM0ment.Chaintrain
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:150)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:207)
        at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:183)
        at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:53)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:156)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:422)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.ClassNotFoundException: me.G4meM0ment.Chaintrain
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:41)
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:29)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:139)
        ... 8 more
    This is my sourcecode:

    Code:
    package me.G4meM0ment.Chaintrain;
     
    import me.G4meM0ment.Chaintrain.EventListener;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Chaintrain extends JavaPlugin{
       
        @Override
        public void onEnable() {
           
            PluginDescriptionFile pdf = getDescription();
           
            System.out.println("[" + pdf.getName() + "]" + " Successfully enabled!");
            System.out.println("[" + pdf.getName() + "] " + "Version: " + pdf.getVersion());
            System.out.println("[" + pdf.getName() + "] " + "By G4meM0ment (Originaly by T4sk)");
           
            @SuppressWarnings("unused")
            PluginManager pm = getServer().getPluginManager();
            new EventListener(this);
           
        }
       
        public static void chain(Player Chained, Player Chainer, PlayerMoveEvent moving) {
           
            //String Chainer;  // config...   
            //String Chained;  //config...       
           
            String chainer = Chainer.getName();
            String chained = Chained.getName();
           
            Chained.sendMessage("Du wurdest von " + chainer + " gefesselt!");
            Chainer.sendMessage("Du hast " + chained + " gefesselt!");
           
            boolean chaining = true;
     
            while(chaining == true) {
                EventListener.blockMoving(moving);
                Chained.sendMessage("Du bist gefesselt!");
            }
           
            EventListener.onPlayerInteractEntityEvent(null, null, chaining);
           
           
        }
       
        @Override
        public void onDisable() {
           
            PluginDescriptionFile pdf = getDescription();
            System.out.println("[" + pdf.getName() + "] "+ pdf.getName() + " disabled!");
        }
       
       
     
    }
    
    And the Listener:

    Code:
    package me.G4meM0ment.Chaintrain;
     
    import java.util.HashMap;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
     
    import me.G4meM0ment.Chaintrain.Chaintrain;
     
    public class EventListener implements org.bukkit.event.Listener {
       
        public static HashMap<Player, Player> data = new HashMap<Player, Player>();
       
        Chaintrain chaintrain;
       
        public EventListener(Chaintrain Chaintrain) {
           
        Chaintrain.getServer().getPluginManager().registerEvents(this, Chaintrain);
     
       
        }
     
    @EventHandler
    public static void blockMoving(PlayerMoveEvent move) {
          move.setCancelled(true);
         
          onPlayerInteractEntityEvent(null, move, false);
           
        }   
       
    @EventHandler
    public static void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event, PlayerMoveEvent move, boolean chaining) {
     
        PlayerMoveEvent moving = move;
       
        if(event.isCancelled())
        return;
        if(!(event.getRightClicked() instanceof Player))
        return;
        if(!(event.getPlayer() instanceof Player))
        return;
        if(event.getRightClicked() instanceof Player)
        {       
            Player Chained = (Player) event.getRightClicked();
            data.put((Player) event.getRightClicked(), Chained);
        }
        if(event.getPlayer() instanceof Player)
        {
            Player Chainer = (Player) event.getPlayer();
            data.put((Player) event.getPlayer(), Chainer);
        }
        else
            data.put((Player) event.getPlayer(), null);
       
        Player Chained = (Player) event.getRightClicked();
        Player Chainer = data.get(Chained);
       
        Chaintrain.chain(Chained, Chainer, moving);
       
        if(event.getRightClicked() instanceof Player && chaining == true)
        {
            chaining = false;
            return;
        }
       
       
       
       
        }
     
     
     
     
    }
    
    Thanks for helping me :D

    ~G4meM0ment
     
  2. Offline

    CorrieKay

    your exception says that your plugin.yml "main" is incorrect.

    it should read
    Code:
    main: me.G4meM0ment.Chaintrain.Chaintrain
     
  3. Offline

    G4meM0ment

    -.- Iam just an idiot, I forgot one .Chaintrain ...

    But is my sourcecode right?
     
  4. Offline

    CorrieKay

    nope, your listener wont do anything, its not registered :p
     
  5. Offline

    G4meM0ment

    How to register then?
     
  6. Bukkit.getServer().getPluginManager().registerEvents(plugin,listener);

    EDIT, ore just use the unused variable:
    pm.registrEvents(plugin,listener);

    EDIT 2: methode "onPlayerInteractEntityEvent" is not an valid methode to listen for an event

    EDIT 3: "event.getPlayer() instanceof Player" getPlayer() returns always an player, so replacethat line whit "true"

    EDIT 4:
    Code:java
    1.  
    2. if(!(event.getRightClicked() instanceof Player))
    3. return;
    4. if(!(event.getPlayer() instanceof Player))
    5. return;
    6. if(event.getRightClicked() instanceof Player)
    7. {
    8. Player Chained = (Player) event.getRightClicked();
    9. data.put((Player) event.getRightClicked(), Chained);
    10. }
    so, you stop the methode from running when event,getRightClicked is not an player "!(event.getRightClicked() instanceof Player)", and then you check again if its an player?

    EDIT 5: whats the point of cancelling all the player movements at "blockMoving()" ?




    EDIT 6: Using players as key and value is unsafe and cause memory leaks " public static HashMap<Player, Player> data = new HashMap<Player, Player>();", try using the name from the player (String) as key and value.

    EDIT 7: "System.out.println("[" + pdf.getName() + "]" + " Successfully enabled!");" use loggers instead of System.out.println(), like:
    plugin.getLogger().info("Enabled");
    plugin.getLogger().info("Disabled");
    plugin.getLogger().warning("Error");
    plugin.getLogger().servere("Critical Error");

    EDIT 8: "import me.G4meM0ment.Chaintrain.Chaintrain;", Importing from same package is not recommend, as this can leads to confusing, just listen to you IDE.

    EDIT 9: "Chaintrain.getServer()" Chaintrain dont have an static methode called "getServer()", try using an IDE to avoid errors like this.

    EDIT 10: "chain(Player Chained, Player Chainer, PlayerMoveEvent moving)", its recommend that you start ariable/functio arguments whit lower cased letters.
     
    Serilum likes this.
  7. Offline

    G4meM0ment


    2. What does that mean? Am I change the name? Or what to do?

    3. I should only write if(true) ???

    4. So Ill simply delete the if(!(...() instanceof Player)

    5. I deleted it and ... I dont know how to say (bad english :p) just take a look at in in the code below.

    6. Sry Iam a beginner how do you mean that? Set not Player into the hashmap, something like data.put(Player.getName)... ?

    7. Dont works it says I cant use that in context with a non-static methode...

    8. As I sad Iam not so good in english, what do you mean with
    9. I changed this to Bukkit.getServer()....

    10. I changed the variables and arguments

    My newer sourcecode:

    Code:java
    1. package me.G4meM0ment.Chaintrain;
    2.  
    3. import me.G4meM0ment.Chaintrain.EventListener;
    4.  
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.player.PlayerMoveEvent;
    7. import org.bukkit.plugin.PluginDescriptionFile;
    8. import org.bukkit.plugin.PluginManager;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Chaintrain extends JavaPlugin{
    12.  
    13. @Override
    14. public void onEnable() {
    15.  
    16. PluginDescriptionFile pdf = getDescription();
    17.  
    18. System.out.println("[" + pdf.getName() + "]" + "Successfully enabled!");
    19. System.out.println("[" + pdf.getName() + "] " + "Version: " + pdf.getVersion());
    20. System.out.println("[" + pdf.getName() + "] " + "By G4meM0ment (Originaly by T4sk)");
    21.  
    22. @SuppressWarnings("unused")
    23. PluginManager pm = getServer().getPluginManager();
    24. new EventListener(this);
    25.  
    26. }
    27.  
    28. public static void chain(Player chained, Player chainer, PlayerMoveEvent move) {
    29.  
    30. //String Chainer; config...
    31. //String Chained; config...
    32.  
    33. String chainername = chainer.getName();
    34. String chainedname = chained.getName();
    35.  
    36. chained.sendMessage("Du wurdest von " + chainername + " gefesselt!");
    37. chainer.sendMessage("Du hast " + chainedname + " gefesselt!");
    38.  
    39. boolean chaining = true;
    40.  
    41. while(chaining == true) {
    42. move.setCancelled(true);
    43. chained.sendMessage("Du bist gefesselt!");
    44. }
    45.  
    46.  
    47. }
    48.  
    49. @Override
    50. public void onDisable() {
    51.  
    52. PluginDescriptionFile pdf = getDescription();
    53. System.out.println("[" + pdf.getName() + "] "+ pdf.getName() + " disabled!");
    54. }
    55.  
    56.  
    57.  
    58. }
    59.  


    Code:java
    1. package me.G4meM0ment.Chaintrain;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerInteractEntityEvent;
    10. import org.bukkit.event.player.PlayerMoveEvent;
    11.  
    12. import me.G4meM0ment.Chaintrain.Chaintrain;
    13.  
    14. public class EventListener implements Listener {
    15.  
    16. public static HashMap<Player, Player> data = new HashMap<Player, Player>();
    17.  
    18. Chaintrain chaintrain;
    19.  
    20. public EventListener(Chaintrain Chaintrain) {
    21.  
    22. Bukkit.getServer().getPluginManager().registerEvents(this, Chaintrain);
    23.  
    24.  
    25. }
    26.  
    27.  
    28. @EventHandler
    29. public void listener(PlayerInteractEntityEvent event, PlayerMoveEvent move) {
    30.  
    31. if(event.isCancelled())
    32. return;
    33. if(!(event.getRightClicked() instanceof Player))
    34. return;
    35. if(!(event.getPlayer() instanceof Player))
    36. return;
    37. if(event.getRightClicked() instanceof Player)
    38. {
    39. Player chained = (Player) event.getRightClicked();
    40. data.put((Player) event.getRightClicked(), chained);
    41. }
    42. if(event.getPlayer() instanceof Player)
    43. {
    44. Player chainer = (Player) event.getPlayer();
    45. data.put((Player) event.getPlayer(), chainer);
    46. }
    47. else
    48. data.put((Player) event.getPlayer(), null);
    49.  
    50. Player chained = (Player) event.getRightClicked();
    51. Player chainer = data.get(chained);
    52.  
    53. Chaintrain.chain(chained, chainer, move);
    54.  
    55.  
    56.  
    57. }
    58.  
    59.  
    60.  
    61.  
    62. }
    63.  
    64.  


    Thanks for your fast and helpful help :p

    ~G4meM0ment
     
  8. Code:java
    1. if(!(event.getPlayer() instanceof Player))
    2. return;
    Code:java
    1. if(event.getPlayer() instanceof Player)
    2. {
    3. Player chainer = (Player) event.getPlayer();
    4. data.put((Player) event.getPlayer(), chainer);
    5. }
    6. else
    7. data.put((Player) event.getPlayer(), null);
    Still checking if event.getPlayer() instanceof Player, it wil be always true

    EDIT 1: event listeners need exact 1 methode, so
    Code:java
    1. @EventHandler
    2. public void listener(PlayerInteractEntityEvent event, PlayerMoveEvent move)
    3. {
    4. /*...*/
    5. }
    wont work
     
  9. Offline

    G4meM0ment


    But how can I replace event.getPlayer()? I dont know what to fill in there.

    To edit 1:
    So I need to create two listener, right?

    But how can I overgive the variable chained & chainer to the onPlayerMoveEvent methode, and then to the chain methode.

    Code:java
    1.  
    2. package me.G4meM0ment.Chaintrain;
    3.  
    4. import me.G4meM0ment.Chaintrain.EventListener;
    5.  
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.player.PlayerMoveEvent;
    8. import org.bukkit.plugin.PluginDescriptionFile;
    9. import org.bukkit.plugin.PluginManager;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Chaintrain extends JavaPlugin{
    13.  
    14. @Override
    15. public void onEnable() {
    16.  
    17. PluginDescriptionFile pdf = getDescription();
    18.  
    19. System.out.println("[" + pdf.getName() + "]" + "Successfully enabled!");
    20. System.out.println("[" + pdf.getName() + "] " + "Version: " + pdf.getVersion());
    21. System.out.println("[" + pdf.getName() + "] " + "By G4meM0ment (Originaly by T4sk)");
    22.  
    23. @SuppressWarnings("unused")
    24. PluginManager pm = getServer().getPluginManager();
    25. new EventListener(this);
    26.  
    27. }
    28.  
    29. public static void chain(PlayerMoveEvent moveevent, Player chained, Player chainer) {
    30.  
    31. //String Chainer; config...
    32. //String Chained; config...
    33.  
    34. String chainername = chainer.getName();
    35. String chainedname = chained.getName();
    36.  
    37. chained.sendMessage("Du wurdest von " + chainername + " gefesselt!");
    38. chainer.sendMessage("Du hast " + chainedname + " gefesselt!");
    39.  
    40. boolean chaining = true;
    41.  
    42. while(chaining == true) {
    43. moveevent.setCancelled(true);
    44. chained.sendMessage("Du bist gefesselt!");
    45. }
    46.  
    47.  
    48. }
    49.  
    50. @Override
    51. public void onDisable() {
    52.  
    53. PluginDescriptionFile pdf = getDescription();
    54. System.out.println("[" + pdf.getName() + "] "+ pdf.getName() + " disabled!");
    55. }
    56.  
    57.  
    58.  
    59. }
    60.  
    61.  


    And the listeners:

    Code:java
    1.  
    2. package me.G4meM0ment.Chaintrain;
    3.  
    4. import java.util.HashMap;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerInteractEntityEvent;
    11. import org.bukkit.event.player.PlayerMoveEvent;
    12.  
    13. import me.G4meM0ment.Chaintrain.Chaintrain;
    14.  
    15. public class EventListener implements Listener {
    16.  
    17. public static HashMap<Player, Player> data = new HashMap<Player, Player>();
    18.  
    19. Chaintrain chaintrain;
    20.  
    21. public EventListener(Chaintrain Chaintrain) {
    22.  
    23. Bukkit.getServer().getPluginManager().registerEvents(this, Chaintrain);
    24.  
    25.  
    26. }
    27.  
    28.  
    29. @EventHandler
    30. public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {
    31.  
    32. if(event.isCancelled())
    33. return;
    34. if(!(event.getRightClicked() instanceof Player))
    35. return;
    36. if(!(event.getPlayer() instanceof Player))
    37. return;
    38. if(event.getRightClicked() instanceof Player)
    39. {
    40. Player chained = (Player) event.getRightClicked();
    41. data.put((Player) event.getRightClicked(), chained);
    42. }
    43. if(event.getPlayer() instanceof Player)
    44. {
    45. Player chainer = (Player) event.getPlayer();
    46. data.put((Player) event.getPlayer(), chainer);
    47. }
    48. else
    49. data.put((Player) event.getPlayer(), null);
    50.  
    51. Player chained = (Player) event.getRightClicked();
    52. Player chainer = data.get(chained);
    53.  
    54. onPlayerMoveEvent(null, chained, chainer);
    55. }
    56.  
    57. @EventHandler
    58. public void onPlayerMoveEvent(PlayerMoveEvent moveevent, Player chained, Player chainer) {
    59.  
    60. Chaintrain.chain(moveevent, chained, chainer);
    61.  
    62. }
    63.  
    64.  
    65.  
    66. }
    67.  
    68.  


    ~G4meM0ment
     
  10. Use maps for this target, I thinking your not knowing how to implement listeners right
     
  11. Offline

    G4meM0ment


    Then tell me how to implement... only to say this: I already made a plugin and my listeners worked there...
     
  12. listener methodes need exact 1 argument, if you need more, then you will nee to pass them whit maps/varibles inside the listener/plugin class, the argument of an listener methode need to be an event, and the listener methode may not shrow anny checcked exceptions
     
  13. Offline

    G4meM0ment

    So I need here to create a hashmap, and put in there the playermoveevent, to get it then in another method, am I right?

    OK may you noticed Iam just learning java at the moment ^^

    I tried now something out, could you take a look a it? - What happens if i start a server with the plugin installed:

    1. No errors
    2. Server dont awnsers, I only can close the execution window.

    My main class:

    Code:java
    1.  
    2. package me.G4meM0ment.Chaintrain;
    3.  
    4. import java.util.HashMap;
    5.  
    6. import me.G4meM0ment.Chaintrain.EventListener;
    7.  
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Chaintrain extends JavaPlugin{
    14.  
    15. @Override
    16. public void onEnable() {
    17.  
    18. PluginDescriptionFile pdf = getDescription();
    19.  
    20. System.out.println("[" + pdf.getName() + "]" + "Successfully enabled!");
    21. System.out.println("[" + pdf.getName() + "] " + "Version: " + pdf.getVersion());
    22. System.out.println("[" + pdf.getName() + "] " + "By G4meM0ment (Originaly by T4sk)");
    23.  
    24. @SuppressWarnings("unused")
    25. PluginManager pm = getServer().getPluginManager();
    26. new EventListener(this);
    27.  
    28. }
    29.  
    30. public static HashMap<String, String> moveboolean = new HashMap<String, String>();
    31.  
    32. public static void chain(boolean moving, Player chained, Player chainer) {
    33.  
    34.  
    35. //String Chainer; config...
    36. //String Chained; config...
    37.  
    38. //String chainername = chainer.getName();
    39. //String chainedname = chained.getName();
    40.  
    41. //chained.sendMessage("Du wurdest von " + chainername + " gefesselt!");
    42. //chainer.sendMessage("Du hast " + chainedname + " gefesselt!");
    43.  
    44. boolean chainedIsChained = Boolean.parseBoolean(EventListener.cIsC.get("cIsC"));
    45.  
    46. if(moving == true && chainedIsChained == false)
    47. {
    48. boolean chaining = true;
    49. while(chaining == true)
    50. {
    51. // chained.sendMessage("Du bist gefesselt!");
    52. moveboolean.put("moveboolean", "true");
    53.  
    54. }
    55. }
    56.  
    57.  
    58. }
    59.  
    60. @Override
    61. public void onDisable() {
    62.  
    63. PluginDescriptionFile pdf = getDescription();
    64. System.out.println("[" + pdf.getName() + "] "+ pdf.getName() + " disabled!");
    65. }
    66.  
    67.  
    68.  
    69. }
    70.  
    71.  


    And the listeners class:

    Code:java
    1.  
    2. package me.G4meM0ment.Chaintrain;
    3.  
    4. import java.util.HashMap;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerInteractEntityEvent;
    11. import org.bukkit.event.player.PlayerMoveEvent;
    12. import org.bukkit.inventory.ItemStack;
    13.  
    14. import me.G4meM0ment.Chaintrain.Chaintrain;
    15.  
    16. public class EventListener implements Listener {
    17.  
    18. public static HashMap<Player, Player> data = new HashMap<Player, Player>();
    19. public static HashMap<ItemStack, ItemStack> itemInHand = new HashMap<ItemStack, ItemStack>();
    20. public static HashMap<String, String> cIsC = new HashMap<String, String>();
    21.  
    22. Chaintrain chaintrain;
    23.  
    24. public EventListener(Chaintrain Chaintrain) {
    25.  
    26. Bukkit.getServer().getPluginManager().registerEvents(this, Chaintrain);
    27.  
    28.  
    29. }
    30.  
    31.  
    32. @EventHandler
    33. public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {
    34.  
    35. if(event.isCancelled())
    36. return;
    37. if(!(event.getRightClicked() instanceof Player))
    38. return;
    39. if(!(event.getPlayer() instanceof Player))
    40. return;
    41.  
    42. ItemStack item;
    43. if(event.getPlayer() instanceof Player)
    44. {
    45. Player chainer = (Player) event.getPlayer();
    46. data.put((Player) event.getPlayer(), chainer);
    47. item = chainer.getItemInHand();
    48. itemInHand.put((ItemStack)chainer.getItemInHand(), item);
    49. }
    50. if(event.getRightClicked() instanceof Player)
    51. {
    52. // if(item.getTypeId() == ItemID.STRING)
    53. // {
    54. Player chained = (Player) event.getRightClicked();
    55. data.put((Player) event.getRightClicked(), chained);
    56.  
    57. boolean chainedIsChained = true;
    58. if(chainedIsChained == true && event.getRightClicked() instanceof Player)
    59. {
    60. data.put((Player) event.getRightClicked(), null);
    61. chainedIsChained = false;
    62. cIsC.put("cIsC", "true");
    63. }
    64. // }
    65. }
    66.  
    67. else
    68. data.put((Player) event.getPlayer(), null);
    69.  
    70. }
    71.  
    72. @EventHandler
    73. public void onPlayerMoveEvent(PlayerMoveEvent moveevent) {
    74. if(moveevent.isCancelled())
    75. return;
    76. if(!(moveevent.getPlayer() instanceof Player))
    77. return;
    78.  
    79. if(moveevent.getPlayer() instanceof Player)
    80. {
    81. Player chained = moveevent.getPlayer();
    82. Player chainer = data.get(chained);
    83.  
    84. boolean moving = true; //boolean to tell chain method to start
    85. Chaintrain.chain(moving, chained, chainer);
    86. }
    87.  
    88. boolean move = Boolean.parseBoolean(Chaintrain.moveboolean.get("moveboolean"));
    89.  
    90. if(move == true)
    91. {
    92. moveevent.setCancelled(true);
    93. }
    94.  
    95.  
    96. }
    97.  
    98.  
    99.  
    100. }
    101.  


    Thanks
    ~G4meM0ment

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  14. Error here:
    Code:java
    1.  
    2. boolean chaining = true;
    3. while(chaining == true)
    4. {
    5. // chained.sendMessage("Du bist gefesselt!");
    6. moveboolean.put("moveboolean", "true");
    7.  
    8. }
    I will do "moveboolean.put("moveboolean", "true");" FOREVER, you know that
     
  15. Offline

    G4meM0ment

    Oh sry I forgot to update my post I edited this already:

    Code:java
    1.  
    2. public static void chain(boolean moving, Player chained, Player chainer) {
    3.  
    4.  
    5. //String Chainer; config...
    6. //String Chained; config...
    7.  
    8. //String chainername = chainer.getName();
    9. //String chainedname = chained.getName();
    10.  
    11. //chained.sendMessage("Du wurdest von " + chainername + " gefesselt!");
    12. //chainer.sendMessage("Du hast " + chainedname + " gefesselt!");
    13.  
    14. boolean chainedIsChained = Boolean.parseBoolean(EventListener.cIsC.get("cIsC"));
    15.  
    16. if(moving == true && chainedIsChained == false)
    17. {
    18. boolean chaining = true;
    19. while(chaining == true)
    20. {
    21. // chained.sendMessage("Du bist gefesselt!");
    22. moveboolean.put("moveboolean", "true");
    23.  
    24. }
    25. }
    26.  
    27.  
    28. }
    29.  


    Code:java
    1.  
    2. public static HashMap<String, String> cIsC = new HashMap<String, String>();
    3.  


    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {
    4.  
    5. if(event.isCancelled())
    6. return;
    7. if(!(event.getRightClicked() instanceof Player))
    8. return;
    9. if(!(event.getPlayer() instanceof Player))
    10. return;
    11.  
    12. ItemStack item;
    13. if(event.getPlayer() instanceof Player)
    14. {
    15. Player chainer = (Player) event.getPlayer();
    16. data.put((Player) event.getPlayer(), chainer);
    17. item = chainer.getItemInHand();
    18. itemInHand.put((ItemStack)chainer.getItemInHand(), item);
    19. }
    20. if(event.getRightClicked() instanceof Player)
    21. {
    22. // if(item.getTypeId() == ItemID.STRING)
    23. // {
    24. Player chained = (Player) event.getRightClicked();
    25. data.put((Player) event.getRightClicked(), chained);
    26.  
    27. boolean chainedIsChained = true;
    28. if(chainedIsChained == true && event.getRightClicked() instanceof Player)
    29. {
    30. data.put((Player) event.getRightClicked(), null);
    31. chainedIsChained = false;
    32. cIsC.put("cIsC", "true");
    33. }
    34. // }
    35. }
    36.  
    37. else
    38. data.put((Player) event.getPlayer(), null);
    39.  
    40. }
    41.  
     
  16. still loop whitout end at
    Code:java
    1. while(chaining == true)
    2. {
    3. // chained.sendMessage("Du bist gefesselt!");
    4. moveboolean.put("moveboolean", "true");
    5.  
    6. }
     
  17. Offline

    G4meM0ment

    OK, silly mistake -.-

    I fixed it:

    Code:java
    1.  
    2. public static void chain(boolean moving, Player chained, Player chainer) {
    3.  
    4.  
    5. //String Chainer; config...
    6. //String Chained; config...
    7.  
    8. //String chainername = chainer.getName();
    9. //String chainedname = chained.getName();
    10.  
    11. //chained.sendMessage("Du wurdest von " + chainername + " gefesselt!");
    12. //chainer.sendMessage("Du hast " + chainedname + " gefesselt!");
    13.  
    14. boolean chainedIsChained = Boolean.parseBoolean(EventListener.cIsC.get("cIsC"));
    15.  
    16. while(moving == true && chainedIsChained == true)
    17. {
    18. // chained.sendMessage("Du bist gefesselt!");
    19. moveboolean.put("moveboolean", "true");
    20.  
    21. }
    22.  
    23.  
    24. }
    25.  


    But now if someone richtclicks another player, to chain him/her, the server wont do anything... I dont know why?
     
  18. Code:java
    1.  
    2. while(moving == true && chainedIsChained == true)
    3. {
    4. // chained.sendMessage("Du bist gefesselt!");
    5. moveboolean.put("moveboolean", "true");
    6.  
    7. }
    So it waits until (moving == true && chainedIsChained == true), but you never alter the varibles inside the loop, so the server would never reatch that spot, or it would be stuck there forever. Remember, when the server is buzzy doing 1 oof your code, it cant do another place at the same time, unless theres an faulthy plugin
     
  19. Offline

    G4meM0ment

    This should fix it or not?

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerMoveEvent(PlayerMoveEvent moveevent) {
    4. if(moveevent.isCancelled())
    5. {
    6. Chaintrain.moveboolean.put("moveboolean", "false");
    7. return;
    8. }
    9. if(!(moveevent.getPlayer() instanceof Player))
    10. return;
    11.  
    12. if(moveevent.getPlayer() instanceof Player)
    13. {
    14. Player chained = moveevent.getPlayer();
    15. Player chainer = data.get(chained);
    16.  
    17. boolean moving = true; //boolean to tell chain method to start
    18. Chaintrain.chain(moving, chained, chainer);
    19. }
    20.  
    21. boolean move = Boolean.parseBoolean(Chaintrain.moveboolean.get("moveboolean"));
    22.  
    23. if(move == true)
    24. {
    25. moveevent.setCancelled(true);
    26. }
    27.  
    28.  
    29. }
    30.  



    Code:java
    1.  
    2. boolean chainedIsChained = true;
    3. if(chainedIsChained == true && event.getRightClicked() instanceof Player)
    4. {
    5. data.put((Player) event.getRightClicked(), null);
    6. chainedIsChained = false;
    7. cIsC.put("cIsC", "false");
    8. }
    9.  
     
  20. yes, it shuld fix it, but why:
    Code:java
    1. if(!(moveevent.getPlayer() instanceof Player))
    2. return;
    3.  
    4. if(moveevent.getPlayer() instanceof Player)
    its just makes it harder to read
     
  21. Offline

    G4meM0ment

    Ok, I changed that now to unchain the player if he gets rightclicked again, but I dont understand why the server already crashes...

    Code:java
    1.  
    2. package me.G4meM0ment.Chaintrain;
    3.  
    4. import java.util.HashMap;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerInteractEntityEvent;
    11. import org.bukkit.event.player.PlayerMoveEvent;
    12. import org.bukkit.inventory.ItemStack;
    13.  
    14. import me.G4meM0ment.Chaintrain.Chaintrain;
    15.  
    16. public class EventListener implements Listener {
    17.  
    18. public static HashMap<Player, Player> data = new HashMap<Player, Player>();
    19. public static HashMap<ItemStack, ItemStack> itemInHand = new HashMap<ItemStack, ItemStack>();
    20. public static HashMap<String, String> cIsC = new HashMap<String, String>();
    21.  
    22. Chaintrain chaintrain;
    23.  
    24. public EventListener(Chaintrain Chaintrain) {
    25.  
    26. Bukkit.getServer().getPluginManager().registerEvents(this, Chaintrain);
    27.  
    28.  
    29. }
    30.  
    31.  
    32. @EventHandler
    33. public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {
    34.  
    35. ItemStack item;
    36. boolean chainedIsChained = Boolean.parseBoolean(cIsC.get("cIsC"));
    37.  
    38. if(event.isCancelled())
    39. return;
    40. if(!(event.getRightClicked() instanceof Player))
    41. return;
    42. else if(chainedIsChained == true && event.getRightClicked() instanceof Player)
    43. {
    44. cIsC.put("cIsC", "false");
    45. data.put((Player) event.getRightClicked(), null);
    46. }
    47. else if(event.getPlayer() instanceof Player && event.getRightClicked() instanceof Player)
    48. {
    49. Player chainer = (Player) event.getPlayer();
    50. data.put((Player) event.getPlayer(), chainer);
    51. item = chainer.getItemInHand();
    52. itemInHand.put((ItemStack)chainer.getItemInHand(), item);
    53.  
    54. Player chained = (Player) event.getRightClicked();
    55. data.put((Player) event.getRightClicked(), chained);
    56. chained.sendMessage("Du wurdest gerechtsklickt!");
    57. cIsC.put("cIsC", "true");
    58. }
    59. else
    60. {
    61. cIsC.put("cIsC", "false");
    62. data.put((Player) event.getPlayer(), null);
    63. }
    64. }
    65.  
    66. @EventHandler
    67. public void onPlayerMoveEvent(PlayerMoveEvent moveevent) {
    68. if(moveevent.isCancelled())
    69. {
    70. Chaintrain.moveboolean.put("moveboolean", "false");
    71. return;
    72. }
    73. if(!(moveevent.getPlayer() instanceof Player))
    74. return;
    75.  
    76. if(moveevent.getPlayer() instanceof Player)
    77. {
    78. Player chained = moveevent.getPlayer();
    79. Player chainer = data.get(chained);
    80.  
    81. boolean moving = true; //boolean to tell chain method to start
    82. Chaintrain.chain(moving, chained, chainer);
    83. }
    84.  
    85. boolean move = Boolean.parseBoolean(Chaintrain.moveboolean.get("moveboolean"));
    86.  
    87. while(move == true)
    88. {
    89. moveevent.setCancelled(true);
    90. }
    91.  
    92.  
    93. }
    94.  
    95. }
    96.  
     
  22. cause:
    Code:java
    1. while(move == true)
    2. {
    3. moveevent.setCancelled(true);
    4. }
     
  23. Offline

    G4meM0ment

    Wont this loop stop when moveboolean becomes false? Because moveboolean only will be true if chainedIsChained == true and moving == true...
    Why it will loop then forever? Do I need to "update" the hashmap moveboolean?
     
  24. you never set the move to "false"
     
  25. Offline

    G4meM0ment

    Finally it works *puh* thanks for your help, but there is one problem left, I want to set the moveevent.setCancelled(true) only if the player which get stopped is the chained ones... but if(move == true && moveevent.getPlayer() instanceof chained)... wont work, how would you handle that problem?
     
  26. instanceof syntax = <variable> instanceof <class>
    You have: moveevent.getPlayer() instanceof chained - <variable> instanceof <variable>
     
  27. Offline

    G4meM0ment

    Would chained instanceof Player work? because all everytime set null if it isnt chained?
     
  28. null instanceof Player always return true, and as far I know "chained" is an player, so the statement would return true always, is this what you want?
     
  29. Offline

    G4meM0ment

    Now I know it wont :( How would you fix that problem?

    I only want to stop the movement if the player which has been rightclicked start to move, he starts to move when move == true, but how to say that the player were chained or is teh player defined as chained.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  30. if(chained.getName().equals(evt.getPlayer().getName())){}
     
Thread Status:
Not open for further replies.

Share This Page