更改马属性

时间:2018-11-10 15:53:33

标签: java plugins minecraft bukkit

最近我一直在为“便携式马匹”制作插件,而且我已经接近完成了,我的马匹可以随心所欲地产卵和产卵。但是,我唯一遇到的麻烦就是更改Horse属性,例如速度,颜色,跳跃高度和Variant。 这是我的代码:

package io.github.bxnie.events;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Donkey;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.vehicle.VehicleExitEvent;
import org.bukkit.inventory.ItemStack;

public class HorseSpawn implements Listener {

    @SuppressWarnings({ "deprecation" })
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
            Player p = e.getPlayer();
            ItemStack item = e.getItem();

            if(item.getItemMeta().getDisplayName().equals(ChatColor.GRAY + "Donkey")) {
                Donkey donkey = (Donkey) p.getWorld().spawn(p.getLocation(), Donkey.class);
                donkey.setAdult();
                donkey.setTamed(true);
                donkey.setOwner(p);
                donkey.getInventory().setSaddle(new ItemStack(Material.SADDLE));
                donkey.setCustomName(ChatColor.GRAY + "Donkey");
                donkey.setPassenger(p);
            }

            if(item.getItemMeta().getDisplayName().equals(ChatColor.RED + "Brown Horse")) {
                Horse horsebrown = (Horse) p.getWorld().spawn(p.getLocation(), Horse.class);
                horsebrown.setAdult();
                horsebrown.setTamed(true);
                horsebrown.setOwner(p);
                horsebrown.getInventory().setSaddle(new ItemStack(Material.SADDLE));
                horsebrown.setCustomName(ChatColor.RED + "Horse");
                horsebrown.setPassenger(p);
            }

            if(item.getItemMeta().getDisplayName().equals(ChatColor.DARK_GRAY + "Black Horse")) {
                Horse horseblack = (Horse) p.getWorld().spawn(p.getLocation(), Horse.class);
                horseblack.setAdult();
                horseblack.setTamed(true);
                horseblack.setOwner(p);
                horseblack.getInventory().setSaddle(new ItemStack(Material.SADDLE));
                horseblack.setCustomName(ChatColor.DARK_GRAY + "Horse");
                horseblack.setPassenger(p);
            }

            if(item.getItemMeta().getDisplayName().equals(ChatColor.WHITE + "White Horse")) {
                Horse horsewhite = (Horse) p.getWorld().spawn(p.getLocation(), Horse.class);
                horsewhite.setAdult();
                horsewhite.setTamed(true);
                horsewhite.setOwner(p);
                horsewhite.getInventory().setSaddle(new ItemStack(Material.SADDLE));
                horsewhite.setCustomName(ChatColor.WHITE + "Horse");
                horsewhite.setPassenger(p);
            }
        }
    }
    @EventHandler
    public void onPLayerDismount(VehicleExitEvent e) {
        if(e.getExited() instanceof Player) {
            if(e.getVehicle() instanceof Donkey) {
                Donkey donkey = (Donkey) e.getVehicle();
                if(donkey.getCustomName() != null) {
                    if(donkey.getCustomName().equals(ChatColor.GRAY + "Donkey")) {
                        donkey.remove();
                    }
                }
            }
            if(e.getVehicle() instanceof Horse) {
                Horse horse = (Horse) e.getVehicle();
                if(horse.getCustomName() != null) {
                    if(horse.getCustomName().equals(ChatColor.RED + "Horse")) {
                        horse.remove();
                    }
                    if(horse.getCustomName().equals(ChatColor.DARK_GRAY + "Horse")) {
                        horse.remove();
                    }
                    if(horse.getCustomName().equals(ChatColor.WHITE + "Horse")) {
                        horse.remove();
                    }
                }
            }
        }
    }
}

我已经做了大量研究,但是似乎并没有什么真正的帮助,你们有没有人知道如何用上面提到的自定义属性生成一匹马?

1 个答案:

答案 0 :(得分:0)

当您认为您已经研究了足够的东西之后,再进行一些研究,我便搜索了"bukkit change horse speed"one of the top answers works perfectly。话虽如此,让我们尝试解决您的问题。

您所需的一切都在horse API中,更具体地说,是#setColor()#setStyle()#setJumpStrength#setVariant()。由于弃用,更改变体可能无法使用该方法,而是需要创建其他实体,例如skeletonHorse

这些方法非常简单:

Horse horseblack = (Horse) p.getWorld().spawn(p.getLocation(), Horse.class);
horseblack.setJumpStrength(2.0);
horseblack.setColor(Color.BLACK);

SkeletonHorse skeletonHorse = (SkeletonHorse) p.getWorld().spawn(p.getLocation(), SkeletonHorse.class);

更改速度略有不同,但是该方法适用于任何生物。

entity.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED).setBaseValue(yourValue);