在Bukkit中围绕一匹骨骼马产卵Mob Spawner颗粒

时间:2014-08-02 20:50:02

标签: java minecraft bukkit

嘿,所以我只是想在Minecraft中制作一匹骨骼马,不断地在它周围生成怪物产卵粒子。我无法弄清楚如何循环它,以便它不断产生火焰粒子。 到目前为止我所做的就是下面的内容。

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.Effect;

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!");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        Player player = (Player) sender;
        if(commandLabel.equalsIgnoreCase("firehorse") || commandLabel.equalsIgnoreCase("fh")){
            Horse horse = (Horse) player.getWorld().spawnEntity(player.getLocation(), EntityType.HORSE);
            horse.setBreed(true);
            horse.setAdult();
            horse.setVariant(Variant.SKELETON_HORSE);
            horse.setCustomName(player.getDisplayName() + "'s Fire Horse");
            horse.getWorld().playEffect(horse.getLocation(), Effect.MOBSPAWNER_FLAMES, 0);
        }
        return false;
    }   
}

2 个答案:

答案 0 :(得分:3)

使用Scheduler Programming。您可以创建一个重复任务,每隔几个刻度生成粒子:

getServer().getScheduler().runTaskTimer(this, new Runnable() {
    public void run() {
        horse.getWorld().playEffect(horse.getLocation(), Effect.MOBSPAWNER_FLAMES, 0);
    }
}, 0L, 10L);

只需将当前playEffect行替换为该行,并使horse成为最终版。

答案 1 :(得分:1)

如果你想创建一个循环,你应该使用Bukkit的内置SchedulerAPI - explained here

以下是使用您的代码的示例:

创建一个名为HorseTimer的新类(或任何你想要的)。让它实现Runnable类。

public class HorseTimer implements Runnable {
    @Override
    public void run() {

    }
}

然后添加一个构造函数,以便类知道产生粒子的实体。

public class HorseTimer implements Runnable {
    private Horse horse;

    public HorseTimer(Horse horse) { 
        this.horse = horse; 
    }

    @Override
    public void run() {

    }
}

现在你可以在马周围产生粒子(就像你只做一次一样)。

public class HorseTimer implements Runnable {
    private Horse horse;

    public HorseTimer(Horse horse) { 
        this.horse = horse; 
    }

    @Override
    public void run() {
        // Do stuff with the horse, will be in the loop.
    }
}

最后,我们需要在Main类中启动循环。将此代码放在onCommand(...)方法的if语句的末尾。

Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new HorseTimer(horse), 0l, <DELAY BETWEEN REPEATS IN TICKS HERE>l);