-
-
Save zhuowei/3915241 to your computer and use it in GitHub Desktop.
Shows your Bukkit server to the LAN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.md_5; | |
import java.io.IOException; | |
import java.net.DatagramPacket; | |
import java.net.DatagramSocket; | |
import java.net.InetSocketAddress; | |
import java.util.logging.Level; | |
import org.bukkit.plugin.java.JavaPlugin; | |
public class LanBukkit extends JavaPlugin implements Runnable { | |
private DatagramSocket socket; | |
private InetSocketAddress broadcastAddr = new InetSocketAddress("224.0.2.60", 4445); | |
@Override | |
public void onEnable() { | |
try { | |
socket = new DatagramSocket(); | |
getServer().getScheduler().scheduleAsyncRepeatingTask(this, this, 0, 30); | |
} catch (IOException ex) { | |
getLogger().severe("Could not start LAN server broadcaster"); | |
} | |
} | |
@Override | |
public void onDisable() { | |
if (socket != null) { | |
socket.close(); | |
} | |
getServer().getScheduler().cancelTasks(this); | |
} | |
public void run() { | |
try { | |
byte[] broadcast = ("[MOTD]" + getServer().getMotd() + "[/MOTD][AD]" + getServer().getIp() + ":" + getServer().getPort() + "[/AD]").getBytes(); | |
socket.send(new DatagramPacket(broadcast, broadcast.length, broadcastAddr)); | |
} catch (IOException ex) { | |
getLogger().log(Level.SEVERE, "Caught exception, shutting down", ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment