Search code examples
javaooppluginsminecraftbukkit

Minecraft Bukkit: How can I access properties/methods in another class from the main class?


Hi everyone I'm trying to use variables from the Main class into my Event Listener class. This is a Minecraft Java Bukkit code that I'm working with. I'm trying to receive the "CanRestart" static boolean variable from my MainClass and trying to use it in the Event Listeners class. No errors popup in the program, but in the console there are errors and the plugin does not work.

I know that the problem is this line of code from the Event Listeners class (which I created to try and get the variables from the Main class):

MainProgram MainCode = new MainProgram();

I do not have that much knowledge with Java's OOP, but I was really wondering if I could get help.

I'm trying to get a code like this work:

MainProgram MainCode = new MainProgram();
if(MainCode.CanRestart == true){
//We received a variable from the main class!
}

Here is my 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.

    }
}

Here is my Event Listener Class:

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
}


}
}

Here is what the console says:

http://prntscr.com/9ki7a5


Solution

  • In your listener class, you take the MainProgram object passed in through the constructer and create an reference variable for it in. This way you can access your MainProgram class in your listeners class.

    EventListeners class:

    //Change
    MainProgram MainCode = new MainProgram();
    //to
    MainProgram MainCode;
    
    public EventListeners(MainProgram plugin) {
        //Add this
        this.MainCode = plugin;
        plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }
    

    Also remove the static modifier from CanRestart.