如何使我的(结果整数)成为一个全局静态变量

时间:2017-11-28 22:08:37

标签: java bukkit

我想听聊天,如果玩家说出随机数,它会播放说玩家赢了。我无法识别int结果(随机数)。

我想我必须把它变成一个全局静态变量,但不知道如何,因为如果我尝试说它是一个非法的修饰符。

代码:

package me.harry.learning;

import java.util.Random;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class pear extends JavaPlugin implements Listener
{
Logger myLogger = Bukkit.getLogger();


@Override
public void onEnable()
{
    myLogger.info("Giveaway start-up has been successful!");
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
}

@Override
public void onDisable()
{
    myLogger.info("Giveaway shut-down has been successful");
}


public boolean onCommand(CommandSender theSender, Command cmd, String commandLabel, String[] args)
{
    if(theSender instanceof Player)
    {
        Player player = (Player) theSender;


        //giveaway {start} {mxnumber}
        if(player.hasPermission("giveaway.admin"))
        {
            if(commandLabel.equalsIgnoreCase("giveaway"))
            {
                if(args.length == 2)
                {
                    if(args[0].equalsIgnoreCase("stop"))
                    {
                        theSender.sendMessage(ChatColor.GREEN + "[Giveaway] " + ChatColor.AQUA + "Giveaway has stopped!");
                        return false;
                    }
                    else if(args[0].equalsIgnoreCase("start"))
                    {
                        try
                        {
                            int numberStart = 0;
                            int numberEnd = (Integer.parseInt(args[1]));
                            Random random = new Random();
                            int result = numberStart + random.nextInt(numberEnd);
                            theSender.sendMessage(ChatColor.GREEN + "[Giveaway] " + ChatColor.AQUA + "Giveaway commencing...");
                            theSender.sendMessage(ChatColor.GREEN + "[Giveaway] " + ChatColor.AQUA + "The winning number is " + result);
                            Bukkit.broadcastMessage(ChatColor.GREEN + (ChatColor.BOLD + "A giveaway has begun ranging from 0 - " + numberEnd));
                        }
                        catch(NumberFormatException exception)
                        {
                            player.sendMessage(ChatColor.GREEN + "[Giveaway] " + ChatColor.RED + args[1] + ChatColor.AQUA + " is not a valid number!");
                        }
                    }
                    else player.sendMessage(ChatColor.GREEN + "[Giveaway] " + ChatColor.AQUA + "Unknown Arguments, try /giveaway help");
                }
                else player.sendMessage(ChatColor.GREEN + "[Giveaway] " + ChatColor.AQUA + "Unknown Arguments, try /giveaway help");
            }
            else player.sendMessage(ChatColor.GREEN + "[Giveaway] " + ChatColor.AQUA + "Giveaway 1.0 made by Herry");
        }
        else player.sendMessage(ChatColor.RED + "No Permission");
    }
    return true;
}
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent winningPlayer)
{
    if(winningPlayer.getMessage().contains(result))
    {
        Bukkit.broadcastMessage(ChatColor.BOLD + (ChatColor.AQUA + "" + winningPlayer + ChatColor.GREEN + " has won the giveaway!"));
    }
    }
}

有谁知道我怎么可能这样做?显然,我需要一个停止命令,所以可能是一个循环,我可以用命令取消?为子命令使用单独的类更好吗?我是否需要BukkitRunnable用于赠品循环?

2 个答案:

答案 0 :(得分:0)

你不能像在C中那样在Java中的函数内部建立静态变量。如果你想在Java中创建一个静态变量,那么它必须在函数之外声明。

答案 1 :(得分:0)

如果你想访问函数之外的变量,你应该这样做:

public class MyClass {

    public String myString;

    public void a() {
        myString = "aaa";
    }

    public void b() {
        System.out.println(myString);
    }

}