I am currently creating a plugin where a random thing happens every 60 seconds and when I was about to build the first test an error came up it's in line 73 and about the part where it says "this" I cannot figure out how to fix it any and all help would be appreciated!
The Error : java: incompatible types: cannot be converted to org.bukkit.plugin.Plugin
Here is my code:
package com.zoren3105.Chaos;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.Random;
public class Main extends JavaPlugin {
@Override
public void onEnable() {
//Fired when the server enables the plugin
runnable();
}
@Override
public void onDisable() {
//Fired when the server stops and disables all plugins
}
Random rand = new Random();
public void runnable() {
new BukkitRunnable() {
@Override
public void run() {
for(Player p : getServer().getOnlinePlayers()) ;
Player player = (Player) Bukkit.getOnlinePlayers();
int chance = rand.nextInt(50);
if (chance == 0) {
player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 60, 2));
} else if (chance == 1) {
player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 60, 2));
} else if (chance == 2) {
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 60, 2));
} else if (chance == 3) {
player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 60, 60));
} else if (chance == 4) {
player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 60, 10));
} else if (chance == 5) {
player.getInventory().addItem(new ItemStack(Material.DIAMOND));
} else if (chance == 6) {
player.getInventory().addItem(new ItemStack(Material.STICK));
} else if (chance == 7) {
player.setHealth(1);
} else if (chance == 8) {
player.getWorld().spawnEntity((Location) player, EntityType.CREEPER);
} else if (chance == 9) {
for (int i = 0; i < 5; i++) {
player.getWorld().spawnEntity((Location) player, EntityType.PRIMED_TNT);
}
} else if (chance == 10) {
for (int i = 0; i < 50; i++) {
player.getWorld().spawnEntity((Location) player, EntityType.ARROW);
}
}runTaskTimerAsynchronously(this, 600, 600);
}
};
}
}
this
in that position references the BukkitRunnable()
, not your Main
class. You can fix this by passing a reference to your Plugin
like so
public void runnable(Plugin plugin) {
...
}runTaskTimerAsynchronously(plugin, 600, 600);
}
And then call it like so
runnable(this);
However, your code won't work; likely for a few reasons, but one that I see is that your for loop has an empty body
for(Player p : getServer().getOnlinePlayers()) ;
Which I think you assume gives you a player as you then cast the results of getOnlinePlayers()
to a Player
which will not work as the method returns Collection<? extends Player>
. You will benefit from taking a Java course; Spigot has a basic tutorial on their wiki which can also help.