Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved sheep and Ageable #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/main/java/me/onebone/actaeon/entity/Ageable.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
15 changes: 15 additions & 0 deletions src/main/java/me/onebone/actaeon/entity/Tameable.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
68 changes: 67 additions & 1 deletion src/main/java/me/onebone/actaeon/entity/animal/Animal.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,58 @@
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.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;

abstract public class Animal extends MovingEntity implements EntityAgeable{
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);
Expand All @@ -35,4 +77,28 @@ public void spawnTo(Player player){

super.spawnTo(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);
}
}
107 changes: 89 additions & 18 deletions src/main/java/me/onebone/actaeon/entity/animal/Sheep.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Expand All @@ -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(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(player, 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))));
}
}