大家好我正在尝试将Main类中的变量用于我的Event Listener类。这是我正在使用的Minecraft Java Bukkit代码。我正在尝试从我的MainClass接收“CanRestart”静态布尔变量,并尝试在Event Listeners类中使用它。程序中没有错误弹出,但在控制台中有错误,插件不起作用。
我知道问题是来自Event Listeners类的这行代码(我创建它来尝试从Main类中获取变量):
MainProgram MainCode = new MainProgram();
我对Java的OOP没有那么多的知识,但我真的很想知道我是否能得到帮助。
我正在尝试获得这样的代码:
MainProgram MainCode = new MainProgram();
if(MainCode.CanRestart == true){
//We received a variable from the main class!
}
这是我的主要课程:
package me.Shadowsych;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class MainProgram extends JavaPlugin{
public static boolean CanRestart = true;
public int CDNumber = 5;
public int DetermineCounter;
@Override
public void onEnable(){ //Essential, when your plugin is enabled.
getLogger().info("Shadowsych's Command Plugin is working!"); //CMD will print this out.
new EventListeners(this); //Inherits the EventListeners class
}
@Override
public void onDisable(){//Essential, when your plugin is disabled.
}
@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { //Creates command function.
Player player = (Player) sender;
if(cmd.getName().equalsIgnoreCase("timer")){
if(CanRestart == true){ //Checks to see if the Timer is still running
CanRestart = false; //The timer has ran.
CDNumber = 5;
DetermineCounter = Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
public void run(){
if(CDNumber > -1){ //This can be a 0 integer
if(!(CDNumber == 0)){ //If once it is 0.
player.sendMessage("" + CDNumber); //The "" is used to make number a String.
}
CDNumber --; //Makes the number -1 if it's already a 0.
}
if(CDNumber == -1){ //Now catches that the number is -1.
player.sendMessage("Count down finished");
Bukkit.getServer().getScheduler().cancelTask(DetermineCounter); //Disables counter.
CanRestart = true; //You can restart your timer now.
return;
}
}
}, 0L, 20L);
}
}
return false; //Is essential in a boolean function.
}
}
这是我的事件监听器类:
package me.Shadowsych;
import me.Shadowsych.MainProgram;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerToggleFlightEvent;
public class EventListeners implements Listener {
MainProgram MainCode = new MainProgram();
public EventListeners(MainProgram plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin); //Registers Event Main
}
@EventHandler
public void PlayerToggleFlight(PlayerToggleFlightEvent EventFloat){
Player player = EventFloat.getPlayer();
if(player.getGameMode() == GameMode.CREATIVE)
return; //If the player is creative then don't do this Event.
EventFloat.setCancelled(true);
player.setAllowFlight(false);
player.setVelocity(player.getLocation().getDirection().multiply(0).setY(1));
player.sendMessage(ChatColor.AQUA + "You have double jumped!");
}
@EventHandler
public void PlayerJump(PlayerMoveEvent EventJumped){
Player player = EventJumped.getPlayer();
if((player.getGameMode() != GameMode.CREATIVE) //Player is not creative
&& (player.getLocation().subtract(0, 1, 0).getBlock().getType() != Material.AIR) //Block beneath them is not air
&& (!player.isFlying())) //Player is not flying
{
player.setAllowFlight(true); //Allow the player to fly
}
}
}
以下是控制台所说的内容:
答案 0 :(得分:0)
您正在使用new MainProgram()
创建插件主类的另一个实例,这对于访问插件第一次创建的主类的原始实例中的变量不是必需的加载,特别是如果您尝试访问的变量是静态的,因此不属于特定对象。由于CanRestart
布尔值是公共和静态的,因此您只需使用MainProgram.CanRestart
来访问该布尔值。但是,如果变量不是静态的,那么"实例变量" (其中每个对象都有自己的变量副本),那么您需要在侦听器中添加对原始主类(不是新实例)的引用,以便您可以从主类中访问变量/方法。听众类。
答案 1 :(得分:0)
在你的监听器类中,你获取通过构造函数传入的MainProgram对象并为其创建一个引用变量。这样你就可以在你的监听器类中访问你的MainProgram类。
EventListeners类:
//Change
MainProgram MainCode = new MainProgram();
//to
MainProgram MainCode;
public EventListeners(MainProgram plugin) {
//Add this
this.MainCode = plugin;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
同时从CanRestart中删除static
修饰符。