Minecraft bukkit错误处理玩家

时间:2017-01-19 19:06:30

标签: java minecraft bukkit

我试图将所有在线玩家设置为游戏模式0(生存)(我做一个varo插件)

以下是my code

package com.abgelutscht.varo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin{
    private int gamemode = 2;
    private int countdown = 30;
    private boolean countdownStarted = false;
    private boolean started = false;
    private Timer count = new Timer();
    private TimerTask count2 = new TimerTask(){
        public void run(){
            if(countdown>0){
                Bukkit.broadcastMessage(blue+"Noch "+aqua+countdown+blue+" Sekunden bis zum Start!");
                countdown--;
            }
            else{
                Bukkit.broadcastMessage(blue+"Los gehts!");
                startVaro();
                count.cancel();
            }
        }
    };
    private List<String[]> teams = new ArrayList<String[]>();
    public static final ChatColor dRed = ChatColor.DARK_RED;
    public static final ChatColor red = ChatColor.RED;
    public static final ChatColor aqua = ChatColor.AQUA;
    public static final ChatColor dAqua = ChatColor.DARK_AQUA;
    public static final ChatColor blue = ChatColor.BLUE;
    public static final ChatColor yellow = ChatColor.YELLOW;
    public static final ChatColor white = ChatColor.WHITE;
    private final String teamCommands = red+"Team-Befehle:\n"+blue
            + "/team " + dRed + "create " + red + "<" + aqua + "Teamname" + red + ">" + blue + "\n"
            + "/team " + dRed + "create " + red + "<" + aqua + "Teamname" + red + "> " + blue + red+ "<"+ dAqua+ "Spieler1" + red + "> " + blue + red+ "<"+ dAqua+ "Spieler2" + red + ">" + blue+ "\n"
            + "/team " + dRed + "add "    + red + "<" + aqua + "Teamname" + red + ">" + blue + " "+ red+ "<"+ dAqua+ "Spieler" + red + ">" + blue + "\n"
            + "/team " + dRed + "remove " + red + "<" + aqua + "Teamname" + red + ">" + blue + " "+ red+ "<"+ dAqua+ "Spieler" + red + ">" + blue + "\n"
            + "/team " + dRed + "delete " + red + "<" + aqua + "Teamname" + red + ">" + blue;
    @Override
    public void onEnable(){
        new PlayerListener(this);
        loadConfig();
        System.out.println("varo-plugin by abgelutscht enabled");
    }
    @Override
    public void onDisable(){
        writeConfig();
        System.out.println("varo-plugin by abgelutscht disabled");
    }
    private void loadConfig(){
        this.getConfig().options().copyDefaults(true);
        this.saveConfig();
        for(int i = 0; i < this.getConfig().getInt("length"); i++){
            List<String> list = new ArrayList<String>();
            list = getConfig().getStringList(String.format("%s", i));
            String[] team = new String[3];
            if(list.size()==1){
                team[0] = list.get(0);
            }
            else if(list.size()==2){
                team[0] = list.get(0);
                team[1] = list.get(1);
            }
            else if(list.size()==3){
                team[0] = list.get(0);
                team[1] = list.get(1);
                team[2] = list.get(2);
            }
            teams.add(team);
        }
        if(this.getConfig().getInt("countdown")!=0){
            countdown = this.getConfig().getInt("countdown");
        }
        started = this.getConfig().getBoolean("started");
        if(started){
            gamemode = 0;
        }
        if(!started){
            gamemode = 2;
        }
    }
    private void writeConfig(){
        this.getConfig().set("countdown", countdown);
        this.getConfig().set("started", started);
        this.getConfig().set("length", teams.size());
        for(int i = 0; i<teams.size(); i++){
            this.getConfig().set(""+i, teams.get(i));
        }
        this.getConfig().options().copyDefaults(true);
        this.saveConfig();
    }
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
        Player player = null;
        if(sender instanceof Player){
            player = (Player) sender;
        }
        if(command.getName().equalsIgnoreCase("team")){
            if(player.isOp()){
                if(!(args.length==0 || args.length>4)){
                    if(args.length==1){
                        if(args[0].equals("list")){
                            if(teams.size()!=0){
                                player.sendMessage(red+"Die Teams:\n");
                                for(int i = 0; i<teams.size(); i++){
                                    player.sendMessage(aqua+teams.get(i)[0]+blue+" "+teams.get(i)[1]+" "+teams.get(i)[2]+"\n");
                                }
                            }
                            else{
                                player.sendMessage(dRed+"[Fehler] Es existieren keine Teams!");
                            }
                        }
                    }
                    else if(args.length==2){
                        if(args[0].equals("create")){
                            if(!teamExisting(args[1])){
                                String[] team = new String[3];
                                team[0] = args[1];
                                teams.add(team);
                                player.sendMessage(blue+"Das Team "+aqua+args[1]+blue+" wurde erstellt");
                            }
                            else{
                                player.sendMessage(dRed+"[Fehler] Das Team "+aqua+args[1]+dRed+" existiert bereits!");
                            }
                        }
                        else if(args[0].equals("remove")){
                            for(int i = 0; i<teams.size(); i++){
                                if(teams.get(i)[0].equals(args[1])){
                                    teams.remove(i);
                                    player.sendMessage(blue+"Das Team "+aqua+args[1]+blue+" wurde entfernt!");
                                }
                            }
                            if(teamExisting(args[1])){
                                player.sendMessage(dRed+"[Fehler] Das Team "+aqua+args[1]+dRed+" existiert nicht!");
                            }
                        }
                    }
                    else if(args.length==3){
                        if(args[0].equals("add")){
                            for(int i = 0; i<teams.size(); i++){
                                if(teams.get(i)[0].equals(args[1])){
                                    if(teams.get(i)[1]!=null && teams.get(i)[2]!=null){
                                        player.sendMessage(dRed+"[Fehler] Das Team "+aqua+args[1]+dRed+" ist voll!");
                                    }
                                    else if(teams.get(i)[1]==null && teams.get(i)[2]==null){
                                        if(!playerExisting(args[2])){
                                            teams.get(i)[1] = args[2];
                                            player.sendMessage(blue+"Der Spieler "+aqua+args[2]+blue+" wurde zum Team "+aqua+args[1]+blue+" hinzugefügt!");
                                        }
                                        else{
                                            player.sendMessage(dRed+"[Fehler] Der Spieler "+aqua+args[2]+dRed+" ist bereits in einem anderen Team!");
                                        }
                                    }
                                    else if((teams.get(i)[1]!=null && teams.get(i)[2]==null) && !teams.get(i)[1].equals(args[2])){
                                        if(!playerExisting(args[2])){
                                            teams.get(i)[2] = args[2];
                                            player.sendMessage(blue+"Der Spieler "+aqua+args[2]+blue+" wurde zum Team "+aqua+args[1]+blue+" hinzugefügt!");
                                        }
                                        else{
                                            player.sendMessage(dRed+"[Fehler] Der Spieler "+aqua+args[2]+dRed+" ist bereits in einem anderen Team!");
                                        }
                                    }
                                    else if((teams.get(i)[1]!=null && teams.get(i)[2]==null) && teams.get(i)[1].equals(args[2])){
                                        player.sendMessage(dRed+"[Fehler] Der Spieler "+aqua+args[2]+dRed+" ist bereits in dem Team");
                                    }
                                }
                            }
                            if(!teamExisting(args[1])){
                                player.sendMessage(dRed+"[Fehler] Das Team "+aqua+args[1]+dRed+" existiert nicht!");
                            }
                        }
                        else if(args[0].equals("remove")){
                            for(int i = 0; i<teams.size(); i++){
                                if(teams.get(i)[0].equals(args[1])){
                                    if(teams.get(i)[1]==null && teams.get(i)[2]==null){
                                        player.sendMessage(dRed+"[Fehler] Das Team "+aqua+args[1]+blue+" ist leer!");
                                    }
                                    else if(teams.get(i)[1]!=null && teams.get(i)[2]!=null){
                                        if(args[2].equals(teams.get(i)[1])){
                                            teams.get(i)[1] = teams.get(i)[2];
                                            teams.get(i)[2] = null;
                                            player.sendMessage(blue+"Der Spieler "+aqua+args[2]+blue+" wurde aus dem Team "+aqua+args[1]+blue+" entfernt!");
                                        }
                                        else if(args[2].equals(teams.get(i)[2])){
                                            teams.get(i)[2] = null;
                                            player.sendMessage(blue+"Der Spieler "+aqua+args[2]+blue+" wurde aus dem Team "+aqua+args[1]+blue+" entfernt!");
                                        }
                                    }
                                    else if((teams.get(i)[1]!=null && teams.get(i)[2]==null) && teams.get(i)[1].equals(args[2])){
                                        teams.get(i)[1] = null;
                                        player.sendMessage(blue+"Der Spieler "+aqua+args[2]+blue+" wurde aus dem Team "+aqua+args[1]+blue+" entfernt!");
                                    }
                                    else if(!args[2].equals(teams.get(i)[1]) && !args[2].equals(teams.get(i)[2])){
                                        player.sendMessage(dRed+"[Fehler] Der Spieler "+aqua+args[2]+dRed+" ist nicht in dem Team!");
                                    }
                                }
                            }
                            if(!teamExisting(args[1])){
                                player.sendMessage(dRed+"[Fehler] Das Team "+aqua+args[1]+dRed+" existiert nicht!");
                            }
                        }
                    }
                    else if(args.length==4){
                        if(args[0].equalsIgnoreCase("create")){
                            if(args[2]!=args[3] && (!playerExisting(args[2]) && !playerExisting(args[3])) && !args[2].equals(args[3])){
                                if(!teamExisting(args[1])){
                                    String[] team = new String[3];
                                    team[0] = args[1];
                                    team[1] = args[2];
                                    team[2] = args[3];
                                    teams.add(team);
                                    player.sendMessage(blue+"Das Team "+aqua+args[1]+blue+" wurde erstellt");
                                }
                                else{
                                    player.sendMessage(dRed+"[Fehler] Das Team "+aqua+args[1]+dRed+" existiert bereits!");
                                }
                            }
                            else if(args[2].equals(args[3])){
                                player.sendMessage(dRed+"[Fehler] Die Spieler "+aqua+args[2]+dRed+" und "+aqua+args[3]+dRed+" sind identisch!");
                            }
                        }
                    }
                }
                else{
                    player.sendMessage(teamCommands);
                }
            }
            else{
                player.sendMessage(dRed+"[Fehler] Du hast nicht die nötigen Rechte, diesen Befehl auszuführen!");
            }
        }
        if(command.getName().equalsIgnoreCase("countdown")){
            if(args.length==1){
                if(args[0].equals("start")){
                    if(!countdownStarted && !started){
                        countdownStarted = true;
                        count.schedule(count2, 0, 1000);
                    }
                    else{
                        player.sendMessage(dRed+"[Fehler] Der Countdown ist bereits gestartet worden!");
                    }
                }
            }
            else if(args.length==2){
                if(args[0].equals("set")){
                    if(!countdownStarted && !started){
                        try{
                            if(Integer.parseInt(args[1])<=0){
                                player.sendMessage(dRed+"[Fehler] Die Eingabe "+aqua+args[1]+dRed+" ist ungültig!");
                            }
                            else{
                                countdown = Integer.parseInt(args[1]);
                                player.sendMessage(blue+"Der Countdown wurde auf "+aqua+args[1]+blue+" gesetzt!");
                            }
                        }
                        catch(Exception e){
                            player.sendMessage(dRed+"[Fehler] Die Eingabe "+aqua+args[1]+dRed+" ist ungültig!");
                        }
                    }
                    else{
                        player.sendMessage(dRed+"[Fehler] Der Countdown ist bereits gestartet worden!");
                    }
                }
            }
        }
        return true;
    }
    private boolean playerExisting(String player){
        for(int i = 0; i<teams.size(); i++){
            try{
                for(int i2 = 0; i2<teams.get(i).length; i++){
                    if(player.equals(teams.get(i)[i2])){
                        return true;
                    }
                }
            }
            catch(Exception e){}
        }
        return false;
    }
    private boolean teamExisting(String team){
        for(int i = 0; i<teams.size(); i++){
            if(team.equals(teams.get(i)[0])){
                return true;
            }
        }
        return false;
    }
    private void startVaro(){
        started = true;
        for(Player player: this.getServer().getOnlinePlayers()){
            player.setGameMode(GameMode.SURVIVAL);
        }
    }
    public List<String[]> getTeams(){
        return teams;
    }
    public int getGamemode(){
        return gamemode;
    }
}

如果我开始倒计时并且方法startVaro()被调用,我得到an error

Exception in thread "Timer-0"
[20:04:39 WARN]: java.lang.IllegalStateException: Asynchronous player tracker update!
[20:04:39 WARN]:        at org.spigotmc.AsyncCatcher.catchOp(AsyncCatcher.java:14)
[20:04:39 WARN]:        at net.minecraft.server.v1_8_R3.EntityTrackerEntry.updatePlayer(EntityTrackerEntry.java:313)
[20:04:39 WARN]:        at net.minecraft.server.v1_8_R3.EntityTracker.a(EntityTracker.java:207)
[20:04:39 WARN]:        at net.minecraft.server.v1_8_R3.EntityPlayer.B(EntityPlayer.java:1041)
[20:04:39 WARN]:        at net.minecraft.server.v1_8_R3.EntityPlayer.updateAbilities(EntityPlayer.java:934)
[20:04:39 WARN]:        at net.minecraft.server.v1_8_R3.PlayerInteractManager.setGameMode(PlayerInteractManager.java:36)
[20:04:39 WARN]:        at org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer.setGameMode(CraftPlayer.java:759)
[20:04:39 WARN]:        at com.x.varo.Main.startVaro(Main.java:304)
[20:04:39 WARN]:        at com.x.varo.Main.access$2(Main.java:300)
[20:04:39 WARN]:        at com.x.varo.Main$1.run(Main.java:31)
[20:04:39 WARN]:        at java.util.TimerThread.mainLoop(Unknown Source)
[20:04:39 WARN]:        at java.util.TimerThread.run(Unknown Source)

为什么我有这个错误?我该如何解决?

1 个答案:

答案 0 :(得分:0)

问题的原因是你试图异步更改播放器状态(相对于Bukkit的主线程)。有关如何安排单个或重复Bukkit任务的详细信息,请查看Scheduler Programming。我相信您最感兴趣的方法是scheduleSyncDelayedTask()