API PingAPI: Listen for and modify outgoing ping responses

Discussion in 'Resources' started by Skionz, Feb 7, 2015.

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

    Plukkies

    This is my code: (Remember I'm noob in this.)
    Main.class
    Code:
    package me.Plukkies.animatedping;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.skionz.pingapi.PingAPI;
    public class Main extends JavaPlugin{
    public void onEnable() {
    PingAPI.registerListener(new Listener());
    }
    }
    
    Listener.class
    Code:
    package me.Plukkies.animatedping;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.plugin.Plugin;
    import com.skionz.pingapi.PingEvent;
    import com.skionz.pingapi.PingListener;
    public class Listener implements PingListener{
      @EventHandler
      public void onPing(PingEvent event) {
      event.getReply().setProtocolVersion(-1);
      event.getReply().setProtocolName(ChatColor.GREEN + "Spelers: " + Bukkit.getServer().getOnlinePlayers().length + "/100");
      Bukkit.getScheduler().scheduleSyncDelayedTask((Plugin) this, new Runnable(){
    @Override
    public void run() {
    PingEvent.createNewPacket(???? What here?);
    
    }
       
      });
      }
    }
    
    Can i fix this?[/code]
     
  2. Offline

    Skionz

    @Plukkies PingEvent#createNewPacket isn't static and it takes a PingReply instance inside the parenthesis. You can get one by invoking PingEvent#getReply().
     
  3. Offline

    Plukkies

    I can't make it no-static.
     
  4. Offline

    Skionz

    @Plukkies It isn't static in the first place.
     
  5. Offline

    mrCookieSlime

    @Plukkies
    Make your event Instance final, then you can use it in the abstract method as well.
     
  6. Offline

    Plukkies

    I'm almost done (after so much work.) Now he shows "Cannot instantiate the type ServerInfoPacket"

    I don't have a abstract class:
    Code:
    package me.Plukkies.animatedping;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.skionz.pingapi.PingAPI;
    import com.skionz.pingapi.PingEvent;
    import com.skionz.pingapi.PingListener;
    import com.skionz.pingapi.PingReply;
    import com.skionz.pingapi.ServerInfoPacket;
    public class Main extends JavaPlugin implements PingListener{
    public void onEnable() {
    PingAPI.registerListener(this);}
    @EventHandler
    public void onPing(PingEvent event) {
    event.getReply().setProtocolVersion(-1);
    event.getReply().setProtocolName(ChatColor.GREEN + "Spelers: " + Bukkit.getServer().getOnlinePlayers().length + "/100");
    Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
     public void run(){
     
     PingReply hoi;
     event.createNewPacket(hoi);
     
     hoi.setMOTD("HOI");
     hoi.setMOTD("HOI2");
     event.cancelPong(false);
      ServerInfoPacket packet = new ServerInfoPacket(hoi);
      packet.send();
     
     }
    },40L);
    }
    }
    
     
    Last edited: Feb 11, 2015
  7. Offline

    Skionz

    Use PingEvent#createNewPacket() to make the packet. ServerInfoPacket is an interface so it cannot be instantiated. Instead, the 'createNewPacket' method gets the 'ServerInfoPacketHandler' corresponding to your server's version. 'ServerInfoPacketHandler' implements 'ServerInfoPacket.'
     
  8. Offline

    Plukkies

    It continues to not work, I'll try something else if not successful. Thanks for the help!
     
  9. Offline

    Skionz

    Post your updated class.
     
  10. Offline

    Plukkies

    Code:
    package me.Plukkies.animatedping;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.skionz.pingapi.PingAPI;
    import com.skionz.pingapi.PingEvent;
    import com.skionz.pingapi.PingListener;
    import com.skionz.pingapi.PingReply;
    import com.skionz.pingapi.ServerInfoPacket;
    import com.skionz.pingapi.v1_7_R3.ServerInfoPacketHandler;
    public class Main extends JavaPlugin implements PingListener{
    public void onEnable() {
    PingAPI.registerListener(this);
    }
    @EventHandler
    public void onPing(PingEvent event) {
    event.getReply().setProtocolVersion(-1);
    event.getReply().setProtocolName(ChatColor.GREEN + "Spelers: " + Bukkit.getServer().getOnlinePlayers().length + "/100");
    Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
     public void run(PingEvent event){
     PingEvent.createNewPacket(packet);
     packet.setMOTD("TEST 1");
     packet.setMOTD("TEST 2");
     event.cancelPong(false);
     
     }
    },40L);
    }
    }
    
     
  11. Offline

    Skionz

    @Plukkies For like the 5th time, the 'createNewPacket()' method is not static.
     
  12. Offline

    Plukkies

    Okay, I understand but how fix this?
     
  13. Offline

    Skionz

    @Plukkies Invoke 'createNewPacket' on your PingEvent instance, AKA 'event.'
     
  14. @Plukkies Well stop trying to use a none static method. Invoke createNewPacket
     
    Last edited: Feb 11, 2015
  15. Offline

    Plukkies

    Can you send me an example? in [ code ] tags?
     
  16. Offline

    Skionz

    @Plukkies 'event' is a PingEvent instance *facepalm*
     
  17. Offline

    teej107

    There are code examples in the OP.
     
  18. Offline

    Plukkies

    Code:
    package me.Plukkies.animatedping;
    import java.lang.reflect.Method;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.skionz.pingapi.PingAPI;
    import com.skionz.pingapi.PingEvent;
    import com.skionz.pingapi.PingListener;
    import com.skionz.pingapi.PingReply;
    import com.skionz.pingapi.ServerInfoPacket;
    import com.skionz.pingapi.v1_7_R3.ServerInfoPacketHandler;
    public class Main extends JavaPlugin implements PingListener{
    public void onEnable() {
    PingAPI.registerListener(this);
    }
    
    @EventHandler
    public void onPing(PingEvent event) {
    event.getReply().setProtocolVersion(-1);
    event.getReply().setProtocolName(ChatColor.GREEN + "Spelers: " + Bukkit.getServer().getOnlinePlayers().length + "/100");
    Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    public void run(){
    ServerInfoPacket packet = Method.invoke(event, PingEvent.createNewPacket());
    PingReply ding = (PingReply) packet;
    ding.setMOTD("HOI");
    event.cancelPong(false);
    packet.send();
    
    }
    },40L);
    }
    }
    
    So, now PingEvent.createNewPacket(event.getReply)
    I found it ^
     
  19. Offline

    Skionz

    @Plukkies ServerInfoPacket is not an instance of PingReply.
     
  20. Offline

    Plukkies

    No errors anymore, but now the animation.....
    Code:
    package me.Plukkies.animatedping;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.skionz.pingapi.PingAPI;
    import com.skionz.pingapi.PingEvent;
    import com.skionz.pingapi.PingListener;
    import com.skionz.pingapi.ServerInfoPacket;
    public class Main extends JavaPlugin implements PingListener{
     private final JavaPlugin plugin;
     public void onEnable() {
    PingAPI.registerListener(this);
    }
    public Main(JavaPlugin plugin){
     this.plugin = plugin;
    }
    @EventHandler
    public void onPing(PingEvent event) {
     event.cancelPong(true);
     Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
      public void run(){
      PingEvent event1 = null;
     ServerInfoPacket packet = event1.createNewPacket(event1.getReply());
     event1.getReply().setMOTD("HOI");
     event1.getReply().setMOTD("HOI2");
     event1.getReply().setMOTD("HOI3");
     event1.getReply().setProtocolVersion(-1);
     event1.getReply().setProtocolName(ChatColor.GREEN + "Spelers: " + Bukkit.getServer().getOnlinePlayers().length + "/100");
     packet.send();
     event1.cancelPong(false);
     Bukkit.broadcastMessage(ChatColor.RED + "HEY");
     }
     }, 40l);
    }
    }
    
    Perhaps you have noticed I've started coding, first java and then bukkit.
     
  21. Offline

    Skionz

    @Plukkies You have to send a ServerInfoPacket for each MOTD.
     
  22. Offline

    Plukkies

    Code:
    package me.Plukkies.animatedping;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.skionz.pingapi.PingAPI;
    import com.skionz.pingapi.PingEvent;
    import com.skionz.pingapi.PingListener;
    import com.skionz.pingapi.ServerInfoPacket;
    public class Main extends JavaPlugin implements PingListener{
    public void onEnable() {
    PingAPI.registerListener(this);
    }
    @EventHandler
    public void onPing(final PingEvent event) {
    event.cancelPong(true);
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    public void run(){
    ServerInfoPacket packet = event.createNewPacket(event.getReply());
    event.getReply().setMOTD("HOI");
    packet.send();
    event.getReply().setMOTD("HOI2");
    packet.send();
    event.getReply().setMOTD("HOI3");
    packet.send();
    event.getReply().setProtocolVersion(-1);
    packet.send();
    event.getReply().setProtocolName(ChatColor.GREEN + "Spelers: " + Bukkit.getServer().getOnlinePlayers().length + "/100");
    packet.send();
    event.cancelPong(false);
    }
    }, 40L);
    }
    }
    
    
    It still don't working ;(
     
  23. Offline

    Skionz

    @Plukkies There is no delay so it all happens to fast for the eye to see. Is the outputted MOTD "HOI3?"
     
  24. Offline

    Plukkies

    No the motd is my motd in server.properties, when I add a motd above the schedule will it works well.
     
    Last edited: Feb 11, 2015
  25. Offline

    CoderRyan

    fixed my prob
     
  26. Offline

    Plukkies

    @Skionz Can you send me your Animated code? so that I can learn from.
     
  27. Offline

    567legodude

    @Skionz Just a question, to make an animated MOTD, all I have to do is make a repeating task and send the ServerInfoPacket for each frame?
     
  28. Offline

    Skionz

  29. Offline

    jb42300

    How do u animate it like you do on the top of the description. i tried useing a bukkit runable but didnt work

    Edit: Btw NICE api

    Edit 2: nvm i frogot all about ServerInfoPacket
     
    Last edited: Feb 15, 2015
    Skionz likes this.
  30. Offline

    567legodude

    @Skionz Whenever someone refreshes their server list and pings it, everyone on the server gets disconnected. The console says the disconnected reason is: Internal Exception: java.lang.NoClassDefFoundError net/minecraft/server/v1_8_R1/PacketStatusOutPong
     
Thread Status:
Not open for further replies.

Share This Page