Skip to content

Commit

Permalink
Update effect fast and slowness
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jan 28, 2016
1 parent 3c097be commit 4b52cfa
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 47 deletions.
67 changes: 27 additions & 40 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import cn.nukkit.level.format.Chunk;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.level.format.generic.BaseFullChunk;
import cn.nukkit.level.particle.CriticalParticle;
import cn.nukkit.level.sound.ClickSound;
import cn.nukkit.level.sound.LaunchSound;
import cn.nukkit.math.*;
Expand Down Expand Up @@ -166,8 +167,6 @@ public class Player extends Human implements CommandSender, InventoryHolder, Chu

private PlayerFood foodData = null;

private float movementSpeed = 0.1f;

private Entity killer = null;

public TranslationContainer getLeaveMessage() {
Expand Down Expand Up @@ -3345,21 +3344,19 @@ public void addExperience(int add) {
int now = this.getExperience();
int added = now + add;
int level = this.getExperienceLevel();
int most = this.getNeedExpToNextLevel(level);
int most = this.calculateRequireExperience(level);
while (added >= most) { //Level Up!
added = added - most;
level++;
this.sendExperienceLevelUp();
getServer().getLogger().debug("Level of " + getName() + " has been risen to " + level + " .");
most = this.getNeedExpToNextLevel(level);
most = this.calculateRequireExperience(level);
}
getServer().getLogger().debug("Added " + add + " EXP to " + getName() + ", now lv:" + level + " (" + added + "/" + most + ") .");
this.setExperience(added, level);
//$sound = new ZombieInfectSound($this);
//$this->getLevel()->addSound($sound);
}

public int getNeedExpToNextLevel(int level) {
public int calculateRequireExperience(int level) {
if (level < 16) {
return 2 * level + 7;
} else if (level >= 17 && level <= 31) {
Expand All @@ -3378,35 +3375,26 @@ public void setExperience(int exp) {
public void setExperience(int exp, int level) {
this.exp = exp;
this.expLevel = level;
sendExperienceLevel(level);
sendExperience(exp);

this.sendExperienceLevel(level);
this.sendExperience(exp);
}

public void sendExperience() {
sendExperience(0);
sendExperience(this.getExperience());
}

public void sendExperience(int exp) {
UpdateAttributesPacket pk = new UpdateAttributesPacket();
pk.entityId = 0;
float ee = ((float) exp) / (float) this.getNeedExpToNextLevel(this.getExperienceLevel());
pk.entries = new Attribute[]{
Attribute.addAttribute(Attribute.EXPERIENCE, "player.experience", 0, 1, ee, true).setValue(ee)
};
this.dataPacket(pk);
float precent = ((float) exp) / this.calculateRequireExperience(this.getExperienceLevel());
this.setAttribute(Attribute.addAttribute(Attribute.EXPERIENCE, "player.experience", 0, 1, precent, true).setValue(precent));
}

public void sendExperienceLevel() {
sendExperienceLevel(0);
sendExperienceLevel(this.getExperienceLevel());
}

public void sendExperienceLevel(int level) {
UpdateAttributesPacket pk = new UpdateAttributesPacket();
pk.entityId = 0;
pk.entries = new Attribute[]{
Attribute.addAttribute(Attribute.EXPERIENCE_LEVEL, "player.level", 0, 24791, level, true).setValue(level)
};
this.dataPacket(pk);
this.setAttribute(Attribute.getAttribute(Attribute.EXPERIENCE_LEVEL).setValue(level));
}

public void sendExperienceLevelUp() {
Expand All @@ -3420,23 +3408,22 @@ public void sendExperienceLevelUp() {
//this.dataPacket(pk);
}

//@Override
public void setAttribute(Attribute attribute) {
UpdateAttributesPacket pk = new UpdateAttributesPacket();
pk.entries = new Attribute[]{attribute};
pk.entityId = 0;
this.dataPacket(pk);
}

@Override
public void setMovementSpeed(float speed) {
this.movementSpeed = speed;
Attribute attr = Attribute.getAttribute(Attribute.MOVEMENT_SPEED).setValue(speed);
super.setMovementSpeed(speed);
if (this.spawned) {
UpdateAttributesPacket pk = new UpdateAttributesPacket();
pk.entries = new Attribute[]{attr};
pk.entityId = 0;
this.dataPacket(pk);
Attribute attribute = Attribute.getAttribute(Attribute.MOVEMENT_SPEED).setValue(speed);
this.setAttribute(attribute);
}
}

//@Override
public float getMovementSpeed() {
return this.movementSpeed;
}

public Entity getKiller() {
return killer;
}
Expand Down Expand Up @@ -3465,12 +3452,12 @@ public void attack(EntityDamageEvent source) {
//暴击
boolean add = false;
if (!damager.onGround) {
/* TODO
for (int i = 0; i < 5); i++) {
CriticalParticle par = new CriticalPartice(new Vector3(this.x + mt_rand(-15, 15) / 10, this.y + mt_rand(0, 20) / 10, this.z + mt_rand(-15, 15) / 10));
NukkitRandom random = new NukkitRandom();
for (int i = 0; i < 5; i++) {
CriticalParticle par = new CriticalParticle(new Vector3(this.x + random.nextRange(-15, 15) / 10, this.y + random.nextRange(0, 20) / 10, this.z + random.nextRange(-15, 15) / 10));
this.getLevel().addParticle(par);
}
*/

add = true;
}
if (add) source.setDamage((float) (source.getDamage() * 1.5));
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/cn/nukkit/entity/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* @since Nukkit 1.0 | Nukkit API 1.0.0
*/

import cn.nukkit.utils.ServerException;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -45,7 +47,10 @@ public static Attribute addAttribute(int id, String name, float minValue, float
}

public static Attribute getAttribute(int id) {
return attributes.containsKey(id) ? attributes.get(id).clone() : null;
if (attributes.containsKey(id)) {
return attributes.get(id).clone();
}
throw new ServerException("Attribute id: " + id + " not found");
}

/**
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/cn/nukkit/entity/Effect.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,14 @@ public void applyEffect(Entity entity) {
}
break;
case Effect.SPEED:
if (entity instanceof Player) ((Player) entity).setMovementSpeed(0.1f + (this.amplifier + 1) * 0.01f);
if (entity instanceof Player) {
((Player) entity).setMovementSpeed((float) (((this.amplifier + 1) * 0.2 + 1) * 0.1));
}
break;
case Effect.SLOWNESS:
if (entity instanceof Player) ((Player) entity).setMovementSpeed(0.1f - (this.amplifier + 1) * 0.01f);
if (entity instanceof Player) {
((Player) entity).setMovementSpeed((float) (((this.amplifier + 1) * -0.15 + 1) * 0.1));
}
break;
}
}
Expand Down Expand Up @@ -247,13 +251,13 @@ public void add(Entity entity, boolean modify) {
}

((Player) entity).dataPacket(pk);

if (this.id == Effect.SPEED) {
((Player) entity).setMovementSpeed(0.1f + (this.amplifier + 1) * 0.01f);
((Player) entity).setMovementSpeed((float) (((this.amplifier + 1) * 0.2 + 1) * 0.1));
}

if (this.id == Effect.SLOWNESS) {
((Player) entity).setMovementSpeed(0.1f - (this.amplifier + 1) * 0.01f);
((Player) entity).setMovementSpeed((float) (((this.amplifier + 1) * -0.15 + 1) * 0.1));
}
}

Expand All @@ -271,7 +275,7 @@ public void remove(Entity entity) {
pk.eventId = MobEffectPacket.EVENT_REMOVE;

((Player) entity).dataPacket(pk);

if (this.id == Effect.SPEED || this.id == Effect.SLOWNESS) {
((Player) entity).setMovementSpeed(0.1f);
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/cn/nukkit/entity/Living.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.ShortTag;
import cn.nukkit.network.protocol.EntityEventPacket;
import cn.nukkit.network.protocol.UpdateAttributesPacket;
import cn.nukkit.utils.BlockIterator;

import java.util.ArrayList;
Expand Down Expand Up @@ -40,6 +41,8 @@ protected float getDrag() {

protected boolean invisible = false;

protected float movementSpeed = 0.1f;

@Override
protected void initEntity() {
super.initEntity();
Expand Down Expand Up @@ -274,4 +277,13 @@ public Block[] getLineOfSight(int maxDistance, int maxLength, Map<Integer, Objec

return blocks.stream().toArray(Block[]::new);
}

public void setMovementSpeed(float speed) {
this.movementSpeed = speed;
}

public float getMovementSpeed() {
return this.movementSpeed;
}

}

0 comments on commit 4b52cfa

Please sign in to comment.