2016-06-27 5 views
-1

입니다. 현재 bukkit 용 플러그인을 만들고 있는데 출력을 변수로 갖는 타이머를 만드는 방법이 있는지 궁금합니다. 자바에서출력이 bukkit에있는 타이머를 만드는 방법 변수는

package com.agrocupcake.llamaenvoys.commands; 

import org.bukkit.ChatColor; 
import org.bukkit.command.Command; 
import org.bukkit.command.CommandExecutor; 
import org.bukkit.command.CommandSender; 
import org.bukkit.entity.Player; 
int time = getServer().getScheduler() 300L; 
var timer = time 
public class EnvoyMain 
implements CommandExecutor { 
    public boolean onCommand(CommandSender sender, Command command, String     label, String[] args) { 
    if (label.equalsIgnoreCase("envoy")) { 
     if (!(sender instanceof Player)) { 
      sender.sendMessage("You must be a Player to preform this command"); 
      return false; 
     } 
     Player player = (Player)sender; 
     sender.sendMessage((Object)ChatColor.GREEN + "Envoys Will drop in "+ timer + "seconds"); 
     } 
     return false; 
    } 
} 

답변

0

이것은 당신이해야 할 일이다 : 플레이어가 명령을 실행 할 때 타이머가 내 현재 코드가 다시 설정하기 전에, 내가 시간을 말한다 그래서 나는 그것을 5 분 타이머를 만들고 싶어하고 그것을 가지고 클래스 범위 앞에 변수를 입력 할 수 없습니다. 또한 변수 플레이어를 정의했지만 사용하지는 않았습니다. 그리고 자바에서 변수를 VAR로 초기화 할 수 없다. Java는 자바 스크립트와 많이 다르다. 메인 클래스의 onEnable()에서 타이머 메소드를 시작하거나 타이머의 카운트 다운을 시작할 때마다 시작해야합니다.

예를 들어 타이머를 리셋하면 시간이 1이되면 바로 다음 다운이 시작됩니다.하지만 취소하고 돌아 오면 다른 곳에서 타이머 메소드를 다시 실행해야합니다. 다시 타이머.

package com.agrocupcake.llamaenvoys.commands; 

import org.bukkit.ChatColor; 
import org.bukkit.command.Command; 
import org.bukkit.command.CommandExecutor; 
import org.bukkit.command.CommandSender; 
import org.bukkit.entity.Player; 

public class EnvoyMain 
implements CommandExecutor { 

private static int timer = 5; 

    public boolean onCommand(CommandSender sender, Command command, String     label, String[] args) { 
    if (label.equalsIgnoreCase("envoy")) { 
     if (!(sender instanceof Player)) { 
      sender.sendMessage("You must be a Player to preform this command"); 
      return false; 
     } 
     Player player = (Player)sender; 
     player.sendMessage((Object)ChatColor.GREEN + "Envoys Will drop in "+ timer + "seconds"); 
     } 
     return false; 
    } 
} 

//run this in your own enable or when you want your timer to start. 
public static void timer(){ 
    new BukkitRunnable(){ 
    public void run(){ 
     timer -= 1 
     if(timer == 0){ 
      //to reset the timer to 5 minutes 
      timer = 5; 
      //or to cancel the timer 
      cancel(); 
      return; 
     } 
    } 
    } 
}.runTaskLater(<MainClass>.getPlugin(<MainClass>.class), 20*60) 
0

는 내가 상황에서하는 것은 Timer class을 만드는 것입니다. I로

package yourPackage; 

import java.util.HashMap; 
import java.util.Map; 
import org.bukkit.command.Command; 
import org.bukkit.command.CommandExecutor; 
import org.bukkit.command.CommandSender; 
import org.bukkit.entity.Player; 

public class EnvoyCommand implements CommandExecutor { 

// This maps contains all players and their timers 
// This is to prevent players from running multiple timers at once. 
private final Map<Player, Timer> timers = new HashMap<>(); 
private final Main main; 

public EnvoyCommand(Main main) { 
    this.main = main; 
} 

@Override 
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { 
    if(!(sender instanceof Player)){ 
     sender.sendMessage("You must be a Player to preform this command"); 
     return true; 
     // Make sure to return true here, because return false is only used 
     // for an error in the usage of the command. 
    } 
    Player player = (Player) sender; 
    if(timers.containsKey(player)){ 
     player.sendMessage("The timer is already running!"); 
     return true; 
    } 

    Timer timer = new Timer(player, 5, 4, 3, 2, 1); 
    timers.put(player, timer); 

    timer.runTaskTimer(main, 0, 20 * 60); // 20*60 = 60 * 20 ticks or 60 seconds or 1 minute. 
    return true; 
    } 
} 

: 당신은 단순히 몇 가지 수정을 당신 같은 실행 프로그램 클래스를 만들 수 있습니다 명령이 후크하려면 지금

package yourPackage; 

import org.bukkit.ChatColor; 
import org.bukkit.entity.Player; 
import org.bukkit.scheduler.BukkitRunnable; 

public class Timer extends BukkitRunnable { 

// This is the player that executed the command 
private final Player player; 

// These are the numbers that will be shown to the player when the timer 
// reaches this number. 
private final int[] mentionNumbers; 

// The amount of minutes +1 because when the timer starts, it instantly 
// removes 1 minute (due to the 'count--;' at the begin of the 'run' method.) 
private int count = 6; 

public Timer(Player player, int... mentionNumbers) { 
    this.player = player; 
    this.mentionNumbers = mentionNumbers; 
} 

@Override 
public void run() { 
    count--; 
    if(count == 0){ 
     player.sendMessage("The timer has finished!"); 
     //Do whatever you want to do when the timer finishes. 
     } else { 
      for(int i : mentionNumbers){ 
       if(count == i){ 
        player.sendMessage(ChatColor.GREEN + "Envoys Will drop in " + i + "seconds"); 
        break; 
       } 
      } 
     } 
    } 
} 

: 이것은 당신의 타이머 클래스가 어떻게 보일지입니다 이미 수행 한 생각, 수 있도록 당신의 onEnable 방법에 집행 클래스를 등록하십시오 다음에서 명령을

package test; 

import org.bukkit.plugin.java.JavaPlugin; 

public class Main extends JavaPlugin { 

    @Override 
    public void onEnable() { 
     getCommand("envoy").setExecutor(new EnvoyCommand(this)); 
    } 
} 

그리고 마지막으로 등록:

name: yourPluginName 
version: 1.0 
main: yourPackage.yourMainClass 
author: AgroCupcake 

commands: 
    envoy: 
    description: Command description here 
    usage: /envoy 
관련 문제