I know there have been plenty of other posts like this from people trying to find their problem in their static class and I have read them but to no avail. I am trying to make a minecraft bukkit plugin for bedwars and when trying to use world.--- I constantly get these errors(Cannot make a static reference to the non-static method getPlayers from the type World) and cannot find where the static class is originating from. Here is my code:
package me.fitch.bedwars.timers;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.World;
import org.bukkit.entity.Player;
import me.fitch.bedwars.main;
public class starttimer implements Listener {
private main plugin;
public starttimer(main plugin) {
this.plugin = plugin;
Bukkit.getPluginManager().registerEvents(this, plugin);
}
@EventHandler
public void startjoin(PlayerJoinEvent e)
{
Object [] players = World.getPlayers().toArray(); //error here
if(players.length == 8)
{
//start countdown
}
}
}
Hope you can help and that I'm not just blind Thanks :)
Edit: after instantiating World server = new World();
I am now getting "Cannot instantiate the type World" errors, thanks for the help so far guys, hope you can help with this :)
Edit 2: so e.getPlayer().getWorld()
works now so thanks but I'm now having issues in my main class where e is not a thing
package me.fitch.bedwars;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin;
import me.fitch.bedwars.listeners.beddestroy;
public class main extends JavaPlugin {
private main plugin;
public main(main plugin) {
this.plugin = plugin;
}
@Override
public void onEnable()
{
World.setSpawnLocation(Integer.parseInt( plugin.getConfig().getString("respawn_pointx")), Integer.parseInt( plugin.getConfig().getString("respawn_pointx")) + 1,Integer.parseInt( plugin.getConfig().getString("respawn_pointz")));
new beddestroy(this);
}
}
You will want your listener in its own class myListener.java
.
From within the listener events in that class you should be able to do everything you need to do.
I like to create a public static Plugin
object in case you ever need to reference the specific instance of the Plugin object from any other class in your plugin.
You will register the listener like this in Main.onEnable()
package myPlugin;
import myPlugin.myListenerClass;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
public static Plugin instance;
@Override
public void onEnable() {
instance = this;
PluginManager my_pm = getServer().getPluginManager();
my_pm.registerEvents(new myListenerClass(), this);
}
}
Your myListenerClass.java will be setup something like the following:
package myPlugin;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
public class myListenerClass implements Listener {
@EventHandler
public void onJoin(PlayerJoinEvent event) {
System.out.println("PlayerJoinEvent was triggered!");
int playerCount = Bukkit.getServer().getOnlinePlayers().size();
if (playerCount == 8) {
// start countdown
}
}
}