107 lines
3.5 KiB
Java
107 lines
3.5 KiB
Java
package pw.yumc.MiaoLobby;
|
|
|
|
import java.security.SecureRandom;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
|
|
import com.google.common.io.ByteArrayDataOutput;
|
|
import com.google.common.io.ByteStreams;
|
|
|
|
import pw.yumc.YumCore.bukkit.Log;
|
|
import pw.yumc.YumCore.bukkit.compatible.C;
|
|
import pw.yumc.YumCore.commands.CommandArgument;
|
|
import pw.yumc.YumCore.commands.CommandExecutor;
|
|
import pw.yumc.YumCore.commands.CommandManager;
|
|
import pw.yumc.YumCore.commands.annotation.Cmd;
|
|
import pw.yumc.YumCore.commands.annotation.Cmd.Executor;
|
|
import pw.yumc.YumCore.commands.annotation.Help;
|
|
import pw.yumc.YumCore.statistic.Statistics;
|
|
import pw.yumc.YumCore.update.SubscribeTask;
|
|
|
|
public class MiaoLobby extends JavaPlugin implements CommandExecutor {
|
|
public Config config;
|
|
private final SecureRandom random = new SecureRandom();
|
|
|
|
public void connect(final Player p, final String server) {
|
|
final ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
|
try {
|
|
out.writeUTF("Connect");
|
|
out.writeUTF(server);
|
|
} catch (final Exception localException) {
|
|
}
|
|
p.sendPluginMessage(this, "BungeeCord", out.toByteArray());
|
|
}
|
|
|
|
@Cmd(permission = "MiaoLobby.default", executor = Executor.PLAYER)
|
|
@Help("进行随机服务器传送")
|
|
public boolean def(final CommandArgument e) {
|
|
random((Player) e.getSender());
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public FileConfiguration getConfig() {
|
|
return config.getConfig();
|
|
}
|
|
|
|
@Override
|
|
public void onEnable() {
|
|
new CommandManager("MiaoLobby", this);
|
|
Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
|
|
if (config.LoginAutoTP) {
|
|
if (Bukkit.getPluginManager().isPluginEnabled("AuthMe")) {
|
|
new AuthMeHook();
|
|
}
|
|
if (Bukkit.getPluginManager().isPluginEnabled("VBossAuth")) {
|
|
new VBossAuthHook();
|
|
}
|
|
}
|
|
new Statistics();
|
|
new SubscribeTask();
|
|
}
|
|
|
|
@Override
|
|
public void onLoad() {
|
|
config = new Config();
|
|
}
|
|
|
|
public void random(final Player player) {
|
|
new BukkitRunnable() {
|
|
List<String> servers = new ArrayList<>(config.Servers);
|
|
int i = 0;
|
|
|
|
@Override
|
|
public void run() {
|
|
if (servers.isEmpty()) {
|
|
if (config.ReTry) {
|
|
servers.addAll(config.Servers);
|
|
return;
|
|
}
|
|
C.ActionBar.send(player, config.Unavailable);
|
|
player.sendMessage(config.Unavailable);
|
|
} else if (player.isOnline()) {
|
|
final String sname = servers.remove(random.nextInt(servers.size()));
|
|
C.ActionBar.send(player, String.format(i > 0 ? config.TimeOut : config.Message, sname));
|
|
connect(player, sname);
|
|
i++;
|
|
return;
|
|
}
|
|
cancel();
|
|
}
|
|
}.runTaskTimerAsynchronously(this, 0, config.WaitTime);
|
|
}
|
|
|
|
@Cmd(permission = "MiaoLobby.reload")
|
|
@Help("重载配置文件")
|
|
public void reload(final CommandArgument e) {
|
|
config.reload();
|
|
Log.toSender(e.getSender(), "§a配置文件已重载!");
|
|
}
|
|
}
|