PluginMessage on disable

Discussion in 'BungeeCord Plugin Development' started by Bentipa, May 31, 2015.

  1. Hey guys,
    I have to send a plugin message in my ondisable method to tell the others server the server is offline now. But I can't because it says you plugin must be enabled to send plugin messages :/ Ideas?

    Thanks in advance,
    Benny
     
  2. Umm, I'm not sure what your problem is, as I can't see your code, or your error. But I'm pretty sure this is basically all you need:
    Code (Text):
    package me.test;

    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin {

        public void onEnable(){
            System.out.println("Server is now ONLINE.");
        }
       
        public void onDisable(){
            System.out.println("Server is now OFFLINE.");
        }
    }
    And that should produce the following two results:
    [​IMG]
    [​IMG]

    But, if you are talking about Bungee, then you would as usual send a message to the proxy through an Output Stream.

    And I'm still not sure where you want to send the message to. So you have to explain yourself a bit more.
     
  3. Code (Text):
     public void onDisable(){
             sendBCM("stopped");
           
                 if(!useBCM)
                bsr.stop();
               
                this.getLogger().info("[Info] Refresh-Task stoped!");
                this.getLogger().info("[Info] Plugin disabled!");
         }
    Code (Text):
     public void sendBCM(String channel){
             final ByteArrayOutputStream b = new ByteArrayOutputStream();
             final DataOutputStream out = new DataOutputStream(b);
           
             try {
                    out.writeUTF("Forward");
                    out.writeUTF("ALL");
                    out.writeUTF(channel);              
                    ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
                    DataOutputStream msgout = new DataOutputStream(msgbytes);
                    out.writeShort(msgbytes.toByteArray().length);
                    out.write(msgbytes.toByteArray());
                    getServer().sendPluginMessage(this, "BungeeCord", b.toByteArray());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
         }
    Causes error:
    [​IMG]
     
  4. Could you point me to line 377?
    I'm guessing line no. 97 is:
    Code (Text):
    sendBCM("stopped");
    If not, please point me to line 97 too.
     
    #4 GODofCRAFTERS, May 31, 2015
    Last edited: May 31, 2015
  5. The problem is, that your plugin is already disabled, so the function you want to use isn't allowed to use.
     
  6. Yes i know :D Thats because i created this post. But how can i make it work?
     
    • Funny Funny x 1
  7. And how can i solve it ?

    Yes line 97 is sendBCM("stopped");
    Thats because it is BungeecordForwarding.
     
  8.  
  9. In line 377 it is
    getServer().sendPluginMessage(this, "BungeeCord", b.toByteArray());
     
  10. Hmmm, well, it's occurring because the validation is taking place after the entire onDisable() method gets executed.

    I guess one thing you can try is (not entirely sure if possible):
    Instead of using "this" as a parameter in sendPluginMessage(), use a plugin instance that doesn't disable yet. Best to use the plugin that disables last.
     
  11. Override the setEnabled method in your Plugin class:
    Code (Java):
    @Override
    public void setEnabled(boolean enabled) {
        if (!enabled)
            sendBCM("stopped");

        super.setEnabled(enabled);
    }
    Then remove it from your onDisable() method
     
  12. Will try that :)
    Edit: can not be overwritten
     
  13. I just checked the source and the only way to do it is with reflection. Since this is an easy kind of reflection for bukkit, I wrote it for you:
    Code (Java):
    @Override
    public void onDisable() {
        try {
            Field field = this.getClass().getField("isEnabled");
            field.setAccessible(true);
            field.set(this, true);

            // Call original onDisable code
            sendBCM("stopped");

            field.set(this, false);
        } catch (Exception ex) { }
    }
     
    • Like Like x 2
    • Useful Useful x 2
  14. Will try soon,
    thanks a lot :)

    Edit: Worked!
     
    #14 Bentipa, Jun 1, 2015
    Last edited: Jun 1, 2015