今天我试图制作一个插件,它会产生一匹骷髅马,并且会在马周围形成火焰粒子。我把那部分放下了,但是每当有人杀死骨马时,火颗粒就会停留在那里。有人请帮忙吗?
我的代码(2个类):
package survivalcraft.FireHorse;
import java.util.logging.Logger;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.command.CommandSender;
import org.bukkit.command.Command;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse.Variant;
import org.bukkit.entity.Player;
import org.bukkit.entity.Horse;
import org.bukkit.inventory.HorseInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.Material;
public class Main extends JavaPlugin{
public static Main plugin;
public final Logger logger = Logger.getLogger("Minecraft");
@Override
public void onDisable(){
PluginDescriptionFile pdf = this.getDescription();
this.logger.info(pdf.getName() + " version " + pdf.getVersion() + " has been disabled! Neigh! ;D");
}
@Override
public void onEnable(){
PluginDescriptionFile pdf = this.getDescription();
this.logger.info(pdf.getName() + " version " + pdf.getVersion() + " has been enabled! Neigh! ;D");
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
Player player = (Player) sender;
if(sender instanceof Player){
if(player.hasPermission("firehorse.spawn")){
if(commandLabel.equalsIgnoreCase("firehorse") || commandLabel.equalsIgnoreCase("fh")){
if(args.length == 0){
Horse horse = (Horse) player.getWorld().spawnEntity(player.getLocation(), EntityType.HORSE);
HorseInventory horseinv = null;
ItemStack saddle = new ItemStack(Material.SADDLE);
horse.setVariant(Variant.SKELETON_HORSE);
horse.setCustomName(player.getDisplayName() + "'s Fire Horse");
horse.setTamed(true);
horseinv.setSaddle(saddle);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new HorseTimer(horse), 0l, 5l);
}else{
player.sendMessage(ChatColor.RED + "Usage: /firehorse");
}
}
}else{
player.sendMessage(ChatColor.RED + "You can't spawn a fire horse!");
}
}else{
player.sendMessage(ChatColor.RED + "You must be a player in order to spawn a fire horse!");
}
return false;
}
}
第二课:
package survivalcraft.FireHorse;
import org.bukkit.Effect;
import org.bukkit.entity.Horse;
public class HorseTimer implements Runnable{
private Horse horse;
public HorseTimer(Horse horse){
this.horse = horse;
}
@Override
public void run(){
horse.getWorld().playEffect(horse.getLocation(), Effect.MOBSPAWNER_FLAMES, 0);
}
}
答案 0 :(得分:2)
第一: 检查一匹马是否像@Momo一样活着,经过@CrypticStorm的小修正
第二: HorseTimer类应该扩展 BukkitRunnable而不是实现Rumnable,因此它可以取消自己 见http://wiki.bukkit.org/Scheduler_Programming#Self-Canceling_Example_2
答案 1 :(得分:1)
您应该将scheduleSyncRepeatingTask
更改为runTaskTimer
,因为这是正确的方法。返回一个表示其id的int,您可以存储该句号,然后在收听Bukkit.getScheduler().cancelTask(int id)
时调用EntityDeathEvent
。
答案 2 :(得分:1)
对不起所有的代码,但这应该完成工作,它存储任务ID和马id在后期使用取消基于马死亡的任务。
public class Main extends JavaPlugin{
public static Main plugin;
public final Logger logger = Logger.getLogger("Minecraft");
@Override
public void onDisable(){
PluginDescriptionFile pdf = this.getDescription();
this.logger.info(pdf.getName() + " version " + pdf.getVersion() + " has been disabled! Neigh! ;D");
Bukkit.getServer().getPluginManager().registerEvents(this, this);
}
@Override
public void onEnable(){
PluginDescriptionFile pdf = this.getDescription();
this.logger.info(pdf.getName() + " version " + pdf.getVersion() + " has been enabled! Neigh! ;D");
}
Map<UUID, Integer> tasks = new HashMap<UUID, Integer>();
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
Player player = (Player) sender;
if(sender instanceof Player){
if(player.hasPermission("firehorse.spawn")){
if(commandLabel.equalsIgnoreCase("firehorse") || commandLabel.equalsIgnoreCase("fh")){
if(args.length == 0){
Horse horse = (Horse) player.getWorld().spawnEntity(player.getLocation(), EntityType.HORSE);
HorseInventory horseinv = null;
ItemStack saddle = new ItemStack(Material.SADDLE);
horse.setVariant(Variant.SKELETON_HORSE);
horse.setCustomName(player.getDisplayName() + "'s Fire Horse");
horse.setTamed(true);
horseinv.setSaddle(saddle);
int id = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new HorseTimer(horse), 0l, 5l);
tasks.put(horse.getUniqueId(), id);
}else{
player.sendMessage(ChatColor.RED + "Usage: /firehorse");
}
}
}else{
player.sendMessage(ChatColor.RED + "You can't spawn a fire horse!");
}
}else{
player.sendMessage(ChatColor.RED + "You must be a player in order to spawn a fire horse!");
}
return false;
}
Map<UUID, Integer> map = null;
@EventHandler
public void handleDeaths(EntityDeathEvent event){
for(UUID id : map.keySet())
if(event.getEntity().getUniqueId().equals(id))
Bukkit.getServer().getScheduler().cancelTask(map.get(id));
}
}
答案 3 :(得分:0)
在run()方法中添加此检查:
if (!horse.isDead()) {
// Spawn particles
}
这样可以防止颗粒产生死马。