结城希亚 发表于 2021-1-27 08:26
evt.getEntity();
Hoglin是什么
- @EventHandler
- public void onEntityDamageByEntity(EntityDamageByEntityEvent evt) {
- ItemStack itemStack = new ItemStack(Material.IRON_SWORD);
- ItemMeta itemMeta = itemStack.getItemMeta();
- itemMeta.setDisplayName(ChatColor.GOLD + "Piglin" + " " + ChatColor.RED + "Friend");
- ArrayList<String> itemMeta_lore = new ArrayList<>();
- itemMeta_lore.add(ChatColor.YELLOW +"Make Hoglin baby");
- itemMeta_lore.add(ChatColor.LIGHT_PURPLE + "Probability" + ChatColor.DARK_RED + "25%");
- itemMeta.setLore(itemMeta_lore);
- itemStack.setItemMeta(itemMeta);
- Hoglin hoglin = (Hoglin) evt.getEntity();
- if (evt.getDamager() instanceof Player) {
- Player player = (Player) evt.getDamager();
- if (player.getInventory().getItemInMainHand().equals(itemStack)) {
- hoglin.setBaby();
- }
- }
- }
- 纵览一下我的代码看看可以不
不能直接强转,要判断下是否符合这个类型
结城希亚 发表于 2021-1-27 09:01
不能直接强转,要判断下是否符合这个类型
能帮忙改一下吗?谢谢
mc-hipower 发表于 2021-1-27 09:02
能帮忙改一下吗?谢谢
- @EventHandler
- public void onEntityDamageByEntity(EntityDamageByEntityEvent evt) {
- ItemStack itemStack = new ItemStack(Material.IRON_SWORD);
- ItemMeta itemMeta = itemStack.getItemMeta();
- itemMeta.setDisplayName(ChatColor.GOLD + "Piglin" + " " + ChatColor.RED + "Friend");
- ArrayList<String> itemMeta_lore = new ArrayList<>();
- itemMeta_lore.add(ChatColor.YELLOW +"Make Hoglin baby");
- itemMeta_lore.add(ChatColor.LIGHT_PURPLE + "Probability" + ChatColor.DARK_RED + "25%");
- itemMeta.setLore(itemMeta_lore);
- itemStack.setItemMeta(itemMeta);
- if (evt.getDamager() instanceof Player && evt.getEntity() instanceof Hoglin) {
- Player player = (Player) evt.getDamager();
- Hoglin hoglin = (Hoglin) evt.getEntity();
- if (player.getInventory().getItemInMainHand().equals(itemStack)) {
- hoglin.setBaby();
- }
- }
- }