I
I
ilyageekdays2020-06-14 18:01:32
Java
ilyageekdays, 2020-06-14 18:01:32

Why is the class method not being called?

Please do not beat with sticks) There are two Java classes. FrogSW is the main one, PlayerJoin is the event handler. Why can't I call the getGame method on the PlayerJoin class?

FrogSW.java code

package wastaz123.frogmine;

import org.bukkit.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import wastaz123.frogmine.objects.Game;
import wastaz123.frogmine.data.DataHandler;

import java.util.HashSet;
import java.util.Set;

public final class FrogSW extends JavaPlugin {

    private static FrogSW instance;
    private Set<Game> games = new HashSet<>();
    private int gamesLimit = 0;

    @Override
    public void onEnable() {
        instance = this;
        getConfig().options().copyDefaults(true);
        getConfig().options().copyHeader(true);
        Bukkit.getConsoleSender().sendMessage("[Frog SkyWars]" + ChatColor.GREEN +" is enabled!");
        Bukkit.getServer().getPluginManager().registerEvents(new GUI(), this);
        if (getConfig().getBoolean("single-server-mode")) {
            gamesLimit = 1;
        } else {
            gamesLimit = getConfig().getInt("max-games");
        }
        if (DataHandler.getInstance().getGameInfo().getConfigurationSection("games") != null) {
            for (String gameName : DataHandler.getInstance().getGameInfo().getConfigurationSection("games").getKeys(false)) {
                Game game = new Game(gameName);
                this.registerGame(game);
            }
        } else {
            getLogger().warning("No games has been created!");
        }
    }

    @Override
    public void onDisable() {
        Bukkit.getConsoleSender().sendMessage("[Frog SkyWars]" + ChatColor.RED +" is disabled!");
        instance = null;
    }

    public static Plugin getInstance() { return instance; }

    public boolean registerGame(Game game) {
        if (games.size() == gamesLimit && gamesLimit != -1) {
            return false;
        }
        games.add(game);
        return true;
    }

    public Game getGame(String gameName) {
        for (Game game : games) {
            if (game.getDisplayName().equalsIgnoreCase(gameName)) {
                return game;
            }
        }
        return null;
    }

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("sw")){
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "Only players can use this command");
                return true;
            }
            GUI.showMainMenu((Player) sender);
            return true;
        }
        return true;
    }
}


PlayerJoin.java code
package wastaz123.frogmine.listeners;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import wastaz123.frogmine.FrogSW;
import wastaz123.frogmine.objects.Game;
import wastaz123.frogmine.objects.GamePlayer;

public class PlayerJoin implements Listener {
    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        Game exampleGame = FrogSW.getInstance().getGame("example"); //<---- жалуется на getGame()
        exampleGame.joinGame(new GamePlayer(player));
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
edward_freedom, 2020-06-14
@ilyageekdays

You have a return another object
private static FrogSW instance
Plugin getInstance()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question