WarWorld 发表于 2024-6-1 18:48:45

一个会变色的羊


[*]

介绍

[*]实现过程





这是一只会变色的羊
   
当它受到伤害的时候就会变色

实现过程
<div style="text-align: left;"><div style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono',monospace;font-size:9.8pt;"><pre><span style="color:#bbb529;">@EventHandler
</span><span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">PlayerAttackSheep</span>(EntityDamageByEntityEvent event){</pre><pre>//获取被伤害者</pre><pre>    Entity entity = (Entity) event.getEntity()<span style="color:#cc7832;">;</span></pre><pre><pre style="font-size: 13.0667px;">//获取伤害者</pre></pre><pre><span style="color:#cc7832;">    </span>Entity damager = (Entity) event.getDamager()<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">
</span><span style="color:#cc7832;">    </span>Sheep sheep = <span style="color:#cc7832;">null;
</span><span style="color:#cc7832;">    </span>Player player = <span style="color:#cc7832;">null;</span></pre><pre><span style="color:#cc7832;">//当伤害者是玩家,伤害对象是羊
</span><span style="color:#cc7832;">    if </span>(entity.getType() == EntityType.<span style="color:#9876aa;font-style:italic;">SHEEP </span>&& damager.getType() == EntityType.<span style="color:#9876aa;font-style:italic;">PLAYER</span>){
      sheep = (Sheep) entity<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">//定义数组
</span><span style="color:#cc7832;">      </span>DyeColor[] values = DyeColor.<span style="font-style:italic;">values</span>()<span style="color:#cc7832;">;</span></pre><pre><span style="color:#cc7832;">//随机颜色
</span><span style="color:#cc7832;">      int </span>i = <span style="color:#cc7832;">new </span>Random().nextInt(values.<span style="color:#9876aa;">length </span>- <span style="color:#6897bb;">1</span>)<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">      </span>sheep.setColor(values)<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">
</span><span style="color:#cc7832;">      </span>player = (Player) damager<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">      </span>player.sendMessage(<span style="color:#6a8759;">"</span><span style="color:#6a8759;font-family:'宋体',monospace;">不准伤害羊羊君</span><span style="color:#6a8759;">"</span>)<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">
</span><span style="color:#cc7832;">      </span>event.isCancelled()<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">      </span>event.setDamage(<span style="color:#6897bb;">0</span>)<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">    </span>}
}</pre></div></div>





FlashDragon 发表于 2024-6-1 18:56:10

感谢楼主分享!
另外我记得把羊命名为jeb_也可以导致不断变色,这是MC的一个彩蛋。

jlwfcbqdlq 发表于 2024-6-1 20:15:38

主任:好,我马上过去

Crigo 发表于 2024-6-1 21:16:18

wow
有点意思呀!赞一个!
页: [1]
查看完整版本: 一个会变色的羊