我的 Minecraft 插件出现错误,无法弄清楚如何解决它
Getting a error for my Minecraft plugin and cant figure out how to solve it
我目前正在创建一个插件,其中每 60 秒发生一次随机事件,当我准备构建第一个测试时出现错误,它在第 73 行,关于它说“这个”的部分我无法理解了解如何修复它,我们将不胜感激!
错误:java:类型不兼容:无法转换为 org.bukkit.plugin.Plugin
这是我的代码:
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
在该位置引用 BukkitRunnable()
,而不是您的 Main
class。您可以通过像这样
传递对 Plugin
的引用来解决此问题
public void runnable(Plugin plugin) {
...
}runTaskTimerAsynchronously(plugin, 600, 600);
}
然后这样称呼它
runnable(this);
但是,您的代码将不起作用;可能有几个原因,但我看到的一个原因是你的 for 循环有一个空体
for(Player p : getServer().getOnlinePlayers()) ;
我认为你认为这会给你一个玩家,然后你施放 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 的结果,这也有帮助。
我目前正在创建一个插件,其中每 60 秒发生一次随机事件,当我准备构建第一个测试时出现错误,它在第 73 行,关于它说“这个”的部分我无法理解了解如何修复它,我们将不胜感激!
错误:java:类型不兼容:无法转换为 org.bukkit.plugin.Plugin
这是我的代码:
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
在该位置引用 BukkitRunnable()
,而不是您的 Main
class。您可以通过像这样
Plugin
的引用来解决此问题
public void runnable(Plugin plugin) {
...
}runTaskTimerAsynchronously(plugin, 600, 600);
}
然后这样称呼它
runnable(this);
但是,您的代码将不起作用;可能有几个原因,但我看到的一个原因是你的 for 循环有一个空体
for(Player p : getServer().getOnlinePlayers()) ;
我认为你认为这会给你一个玩家,然后你施放 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 的结果,这也有帮助。