From 26ab9298f52e46ea1e35eea0aa117f9496b26968 Mon Sep 17 00:00:00 2001 From: CreeperFace Date: Sun, 29 Jan 2017 10:31:55 +0100 Subject: [PATCH 1/2] Improved sheep and Ageable --- .../me/onebone/actaeon/entity/Ageable.java | 58 ++++++++++ .../onebone/actaeon/entity/animal/Animal.java | 8 +- .../onebone/actaeon/entity/animal/Sheep.java | 107 +++++++++++++++--- 3 files changed, 154 insertions(+), 19 deletions(-) create mode 100644 src/main/java/me/onebone/actaeon/entity/Ageable.java diff --git a/src/main/java/me/onebone/actaeon/entity/Ageable.java b/src/main/java/me/onebone/actaeon/entity/Ageable.java new file mode 100644 index 0000000..b1a4e67 --- /dev/null +++ b/src/main/java/me/onebone/actaeon/entity/Ageable.java @@ -0,0 +1,58 @@ +package me.onebone.actaeon.entity; + +import cn.nukkit.entity.EntityAgeable; +import cn.nukkit.level.format.FullChunk; +import cn.nukkit.nbt.tag.CompoundTag; + +/** + * Created by CreeperFace on 29. 1. 2017. + */ +public abstract class Ageable extends MovingEntity implements EntityAgeable { + + public int forcedAge = 0; + + public Ageable(FullChunk chunk, CompoundTag nbt){ + super(chunk, nbt); + } + + @Override + protected void initEntity() { + super.initEntity(); + + if(!this.namedTag.contains("Age")){ + this.namedTag.putInt("Age", -24000); + } + this.age = this.namedTag.getInt("Age"); + + this.forcedAge = this.namedTag.getInt("ForcedAge"); + } + + @Override + public boolean onUpdate(int currentTick) { + boolean hasUpdate = super.onUpdate(currentTick); + + this.setGrowingAge(this.age); + + return hasUpdate; + } + + @Override + public boolean isBaby() { + return this.age < 0; + } + + public void setGrowingAge(int age) { + this.age = age; + + this.setDataFlag(DATA_FLAGS, DATA_FLAG_BABY, age < 0); + this.setScale(isBaby() ? 0.5f : 1f); + } + + @Override + public void saveNBT() { + super.saveNBT(); + + this.namedTag.putInt("Age", this.age); + this.namedTag.putInt("ForcedAge", this.forcedAge); + } +} diff --git a/src/main/java/me/onebone/actaeon/entity/animal/Animal.java b/src/main/java/me/onebone/actaeon/entity/animal/Animal.java index 9c9c8d9..397fc07 100644 --- a/src/main/java/me/onebone/actaeon/entity/animal/Animal.java +++ b/src/main/java/me/onebone/actaeon/entity/animal/Animal.java @@ -3,12 +3,14 @@ import cn.nukkit.Player; import cn.nukkit.entity.Entity; import cn.nukkit.entity.EntityAgeable; +import cn.nukkit.item.Item; import cn.nukkit.level.format.FullChunk; import cn.nukkit.nbt.tag.CompoundTag; import cn.nukkit.network.protocol.AddEntityPacket; +import me.onebone.actaeon.entity.Ageable; import me.onebone.actaeon.entity.MovingEntity; -abstract public class Animal extends MovingEntity implements EntityAgeable{ +abstract public class Animal extends Ageable implements EntityAgeable{ public Animal(FullChunk chunk, CompoundTag nbt){ super(chunk, nbt); } @@ -35,4 +37,8 @@ public void spawnTo(Player player){ super.spawnTo(player); } + + public boolean isBreedingItem(Item item) { + return item.getId() == Item.WHEAT; + } } diff --git a/src/main/java/me/onebone/actaeon/entity/animal/Sheep.java b/src/main/java/me/onebone/actaeon/entity/animal/Sheep.java index 7066bb8..2bb60c3 100644 --- a/src/main/java/me/onebone/actaeon/entity/animal/Sheep.java +++ b/src/main/java/me/onebone/actaeon/entity/animal/Sheep.java @@ -1,66 +1,79 @@ package me.onebone.actaeon.entity.animal; import cn.nukkit.Player; +import cn.nukkit.block.BlockAir; +import cn.nukkit.block.BlockWool; import cn.nukkit.entity.Entity; +import cn.nukkit.entity.EntityHumanType; +import cn.nukkit.entity.data.ByteEntityData; +import cn.nukkit.event.entity.EntityDamageByEntityEvent; import cn.nukkit.item.Item; +import cn.nukkit.item.ItemBlock; +import cn.nukkit.item.ItemDye; import cn.nukkit.level.format.FullChunk; import cn.nukkit.math.AxisAlignedBB; import cn.nukkit.nbt.tag.CompoundTag; +import cn.nukkit.utils.DyeColor; import java.util.Random; -public class Sheep extends Animal{ +public class Sheep extends Animal { public static final int NETWORK_ID = 13; - public Sheep(FullChunk chunk, CompoundTag nbt){ + public boolean sheared = false; + public int color = 0; + + private int sheepTimer; + + public Sheep(FullChunk chunk, CompoundTag nbt) { super(chunk, nbt); } @Override - public float getWidth(){ + public float getWidth() { return 0.9f; } @Override - public float getHeight(){ - if (isBaby()){ + public float getHeight() { + if (isBaby()) { return 0.9f; // No have information } return 1.3f; } @Override - public float getEyeHeight(){ - if (isBaby()){ + public float getEyeHeight() { + if (isBaby()) { return 0.95f * 0.9f; // No have information } return 0.95f * getHeight(); } @Override - public String getName(){ + public String getName() { return this.getNameTag(); } @Override - public Item[] getDrops(){ - return new Item[]{Item.get(Item.WOOL, 0, new Random().nextInt(2) + 1)}; + public Item[] getDrops() { + return new Item[]{new ItemBlock(new BlockWool(getColor()))}; } @Override - public int getNetworkId(){ + public int getNetworkId() { return NETWORK_ID; } @Override - public boolean entityBaseTick(int tickDiff){ - if(!this.hasTarget()){ + public boolean entityBaseTick(int tickDiff) { + if (!this.hasTarget()) { Entity[] entities = this.level.getNearbyEntities(new AxisAlignedBB(this.x, this.y, this.z, this.x, this.y, this.z).expand(7, 7, 7)); Entity near = null; - for(Entity entity : entities){ - if(entity instanceof Player && (near == null || this.distance(near) < this.distance(entity))){ - if(((Player) entity).getInventory().getItemInHand().getId() == Item.WHEAT){ + for (Entity entity : entities) { + if (entity instanceof Player && (near == null || this.distance(near) < this.distance(entity))) { + if (((Player) entity).getInventory().getItemInHand().getId() == Item.WHEAT) { near = entity; } } @@ -72,13 +85,71 @@ public boolean entityBaseTick(int tickDiff){ return super.entityBaseTick(tickDiff); } - public boolean hasTarget(){ + public boolean hasTarget() { return super.hasFollowingTarget() && this.getTarget() instanceof Player && ((Player) this.getTarget()).getInventory().getItemInHand().getId() == Item.WHEAT; } @Override - protected void initEntity(){ + protected void initEntity() { super.initEntity(); this.setMaxHealth(8); + + if (!this.namedTag.contains("Color")) { + this.setColor(getRandomSheepColor().getWoolData()); + } else { + this.setColor(this.namedTag.getByte("Color")); + } + + if (!this.namedTag.contains("Sheared")) { + this.namedTag.putByte("Sheared", 0); + } else { + this.sheared = this.namedTag.getBoolean("Sheared"); + } + + this.setDataFlag(DATA_FLAGS, DATA_FLAG_SHEARED, this.sheared); + } + + @Override + public boolean onInteract(Entity entity, Item item) { + if (item.getId() == Item.DYE) { + this.setColor(((ItemDye) item).getDyeColor().getWoolData()); + return true; + } + + if (item.getId() != Item.SHEARS) { + return super.onInteract(entity, item); + } + + return shear(); + } + + public boolean shear() { + if (sheared) { + return false; + } + + this.setSheared(true); + this.level.dropItem(this, new ItemBlock(new BlockWool(this.getColor()), 0, this.level.rand.nextInt(2) + 1)); + return true; + } + + public void setSheared(boolean value) { + this.sheared = value; + this.setDataFlag(DATA_FLAGS, DATA_FLAG_SHEARED, value); + } + + public void setColor(int color) { + this.color = color; + this.setDataProperty(new ByteEntityData(DATA_COLOUR, color)); + } + + public int getColor() { + return this.color; + } + + public static DyeColor getRandomSheepColor() { + Random random = new Random(); + int i = random.nextInt(100); + return i < 5 ? DyeColor.BLACK : (i < 10 ? DyeColor.LIGHT_GRAY : (i < 15 ? DyeColor.GRAY : (i < 18 ? DyeColor.BROWN : (random.nextInt(500) == 0 ? DyeColor.PINK : DyeColor.WHITE)))); } } From fdbf6e63461850e7c7ada07390b1ae4d54b48bb9 Mon Sep 17 00:00:00 2001 From: CreeperFace Date: Sun, 29 Jan 2017 11:04:47 +0100 Subject: [PATCH 2/2] fix --- .../me/onebone/actaeon/entity/Tameable.java | 15 +++++ .../onebone/actaeon/entity/animal/Animal.java | 60 +++++++++++++++++++ .../onebone/actaeon/entity/animal/Sheep.java | 4 +- 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/main/java/me/onebone/actaeon/entity/Tameable.java diff --git a/src/main/java/me/onebone/actaeon/entity/Tameable.java b/src/main/java/me/onebone/actaeon/entity/Tameable.java new file mode 100644 index 0000000..349ede3 --- /dev/null +++ b/src/main/java/me/onebone/actaeon/entity/Tameable.java @@ -0,0 +1,15 @@ +package me.onebone.actaeon.entity; + +import cn.nukkit.level.format.FullChunk; +import cn.nukkit.nbt.tag.CompoundTag; +import me.onebone.actaeon.entity.animal.Animal; + +/** + * Created by CreeperFace on 29. 1. 2017. + */ +public abstract class Tameable extends Animal { + + public Tameable(FullChunk chunk, CompoundTag nbt){ + super(chunk, nbt); + } +} diff --git a/src/main/java/me/onebone/actaeon/entity/animal/Animal.java b/src/main/java/me/onebone/actaeon/entity/animal/Animal.java index 397fc07..c3d759e 100644 --- a/src/main/java/me/onebone/actaeon/entity/animal/Animal.java +++ b/src/main/java/me/onebone/actaeon/entity/animal/Animal.java @@ -5,16 +5,56 @@ import cn.nukkit.entity.EntityAgeable; import cn.nukkit.item.Item; import cn.nukkit.level.format.FullChunk; +import cn.nukkit.level.particle.HeartParticle; +import cn.nukkit.math.Vector3; import cn.nukkit.nbt.tag.CompoundTag; import cn.nukkit.network.protocol.AddEntityPacket; import me.onebone.actaeon.entity.Ageable; import me.onebone.actaeon.entity.MovingEntity; +import java.util.Random; + abstract public class Animal extends Ageable implements EntityAgeable{ + + public int inLove = 0; + public Player playerInLove = null; + public Animal(FullChunk chunk, CompoundTag nbt){ super(chunk, nbt); } + @Override + protected void initEntity() { + super.initEntity(); + + this.inLove = this.namedTag.getInt("InLove"); + } + + @Override + public void saveNBT() { + super.saveNBT(); + + this.namedTag.putInt("InLove", this.inLove); + } + + @Override + public boolean onUpdate(int currentTick) { + boolean hasUpdate = super.onUpdate(currentTick); + + if(inLove > 0) { + hasUpdate = true; + + if(inLove % 10 == 0) { + Random rand = this.level.rand; + this.level.addParticle(new HeartParticle(new Vector3(this.x + (rand.nextFloat() * this.getWidth() * 2.0F) - this.getWidth(), this.y + 0.5D + (rand.nextFloat() * this.getHeight()), this.z + (rand.nextFloat() * this.getWidth() * 2.0F) - this.getWidth()))); + } + + inLove--; + } + + return hasUpdate; + } + @Override public boolean isBaby(){ return this.getDataFlag(DATA_FLAGS, Entity.DATA_FLAG_BABY); @@ -41,4 +81,24 @@ public void spawnTo(Player player){ public boolean isBreedingItem(Item item) { return item.getId() == Item.WHEAT; } + + public void setInLove(Player player) + { + this.inLove = 600; + this.playerInLove = player; + this.setDataFlag(DATA_FLAGS, DATA_FLAG_INLOVE); + } + + @Override + public boolean onInteract(Player player, Item item) { + if(isBreedingItem(item)) { + if(this.inLove <= 0) { + setInLove(player); + } + + return true; + } + + return super.onInteract(player, item); + } } diff --git a/src/main/java/me/onebone/actaeon/entity/animal/Sheep.java b/src/main/java/me/onebone/actaeon/entity/animal/Sheep.java index 2bb60c3..662a2e0 100644 --- a/src/main/java/me/onebone/actaeon/entity/animal/Sheep.java +++ b/src/main/java/me/onebone/actaeon/entity/animal/Sheep.java @@ -110,14 +110,14 @@ protected void initEntity() { } @Override - public boolean onInteract(Entity entity, Item item) { + public boolean onInteract(Player player, Item item) { if (item.getId() == Item.DYE) { this.setColor(((ItemDye) item).getDyeColor().getWoolData()); return true; } if (item.getId() != Item.SHEARS) { - return super.onInteract(entity, item); + return super.onInteract(player, item); } return shear();