2014-02-22 43 views
0

我試圖用一個Bukkit談話,已經工作,但是,當我使用TimeUnit.SECONDS.sleep(integer-value),它的工作原理一次,然後失敗,出現此錯誤控制檯:java.lang.InterruptedException: sleep interruptedBukkit對話失敗睡覺

當提示已顯示,下一個將顯示方法acceptInput被調用。在第一個提示符中,它可以正常工作,在其他提示符下,這些提示符會被調出(提示符會調用它自己的一個新實例)。除了睡眠部分,所有的工作都很好。任何想法來解決這個問題?

這裏是我的代碼:

package dbx12.Test1.Tutorial.Prompts; 


import java.util.concurrent.TimeUnit; 

import org.bukkit.conversations.ConversationContext; 
import org.bukkit.conversations.Prompt; 
import org.bukkit.entity.Player; 


public class Text implements Prompt { 

    @Override 
    public Prompt acceptInput(ConversationContext context, String input) { 
     int thisPrompt = (int) context.getSessionData("step"); 
     context.setSessionData("step", thisPrompt+1); 
     Player p = (Player) context.getForWhom(); 

     boolean type; 

     try { 
      TimeUnit.SECONDS.sleep(dbx12.Test1.Utils.Prompt_List.delay.get(thisPrompt)); 
     } catch (InterruptedException e) { 
      Thread.currentThread().interrupt(); 
      e.printStackTrace(); //DEBUG 
     }  

     try { 
      type = dbx12.Test1.Utils.Prompt_List.promptType.get(thisPrompt+1); 
     } catch (Exception e) { 
      return new Finish(); 
     } 

     if(dbx12.Test1.Utils.Prompt_List.hasLocation.get(thisPrompt+1) == true) 
      p.teleport(dbx12.Test1.Utils.Prompt_List.location.get(thisPrompt+1)); 

     if(type==true) 
     { 
      System.out.println("return a text"); 
      return new Text(); 
     } 
     else 
     { 
      System.out.println("return a interaction"); 
      return new Interaction(); 
     } 
    } 

    @Override 
    public boolean blocksForInput(ConversationContext context) { 
     return false; 
    } 

    @Override 
    public String getPromptText(ConversationContext context) { 
     return dbx12.Test1.Utils.Prompt_List.promptText.get(context.getSessionData("step")); 
    } 

} 
+0

你在這裏做什麼?設置延遲?這個插件將會做什麼?這可能有更好的方法。 – Jojodmo

+0

我嘗試通過提示顯示信息。經過一定的延遲後,顯示下一個提示。延遲第一次運行,但在接下來的x次提示中失敗。我把下一個提示稱爲這個新的實例。 – DBX12

回答

1

sleep會造成整個服務器停止做x秒什麼。取而代之的睡眠,使用SyncDelayedTask

this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){ 
    public void run(){ 
     //what you want to do here 
    } 
},delayInSeconds * 20); 

因此,可以說,例如,你想送test1到服務器,然後test2服務器5秒鐘後,你可以使用:

int repeats; 

public void sendMessages(){ 
    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){ 

    public void run(){ 
     if(repeats == 0){ //if it's the first time running 
      Bukkit.getServer.broadcastMessage("test1"); 
      repeats++; //add to repeats 
      sendMessages(); //call the method again 
     } 
     else if(repeats == 1){ //if it is the second time being called 
      Bukkit.getServer.broadcastMessage("test2"); 
     } 
    } 
},5 * 20);//wait 5 seconds 

於是有了上面的代碼,你可以做這樣的方法:

public void startSendingMessages(){ 
    repeats = 0; 
    sendMessages(); 
} 

在哪裏,當你叫startSendingMessages()時,test1將被髮送,然後,5秒後,test2將被髮送。

我們將時間乘以20秒的原因是因爲它必須在蜱蟲或我的世界時間,並且1 second = 20 ticks

還有很多其他調度程序類型,如SyncRepeatingTasks。要了解更多關於它們的信息,請查看bukkit JavaDocshttp://jd.bukkit.org/dev/apidocs/,這裏也有一個bukkit的好教程:http://wiki.bukkit.org/Scheduler_Programming

+0

這不會以我想要的方式工作。也許我不夠精確;) 我想延遲執行的時候,我使用了 'try {TimeUnit.SECONDS.sleep(dbx12.Test1.Utils.Prompt_List.delay.get(thisPrompt)); ()InterruptException e)Thread.currentThread()。interrupt(); e.printStackTrace(); // DEBUG }' 所以我嘗試等到指定的時間結束,然後我想返回一個新的「完成」或「文本」或「交互」。在我返回提示時顯示此提示。所以我想延遲這個提示的顯示。 – DBX12

+0

對不起,格式不正確,但stackoverflow取消我的編輯,因爲我超過5分鐘。題外:預覽或更長的時間進行編輯會很好。 – DBX12

+0

@ DBX12同樣的原則適用,你可以改變底部的時間,這裏:'},5 * 20);''到你想要的時間* 20' – Jojodmo