UID382645性别保密经验 EP铁粒 粒回帖0主题精华在线时间 小时注册时间2022-4-24最后登录1970-1-1
|
这是一只会变色的羊
当它受到伤害的时候就会变色
实现过程
- <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[i])<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>
复制代码
|
|