Skip to content

Commit

Permalink
Merge branch 'entity-damage'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/cn/nukkit/Player.java
#	src/main/java/cn/nukkit/entity/EntityLiving.java
  • Loading branch information
wode490390 committed Feb 24, 2024
2 parents 96781f9 + 2e3ee8b commit 5ff4243
Show file tree
Hide file tree
Showing 20 changed files with 245 additions and 299 deletions.
211 changes: 51 additions & 160 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import java.util.stream.Collectors;

import static cn.nukkit.SharedConstants.BREAKPOINT_DEBUGGING;
import static cn.nukkit.SharedConstants.EXPERIMENTAL_COMBAT_KNOCKBACK_TEST;
import static cn.nukkit.SharedConstants.RESOURCE_PACK_CHUNK_SIZE;

/**
Expand Down Expand Up @@ -175,13 +176,9 @@ public class Player extends EntityHuman implements CommandSender, InventoryHolde
protected IntSet permanentWindows = new IntOpenHashSet();
protected int messageCounter = 2;

private String clientSecret;

public Vector3 speed = null;
public int attackCriticalThisJump = 0;

// public final Set<String> achievements = new ObjectOpenHashSet<>();

public int craftingType = CRAFTING_SMALL;
public RecipeTag recipeTag = RecipeTag.CRAFTING_TABLE;

Expand Down Expand Up @@ -218,7 +215,6 @@ public class Player extends EntityHuman implements CommandSender, InventoryHolde

private Integer loaderId;

protected float stepHeight = 0.6f;

public Long2BooleanMap usedChunks = new Long2BooleanOpenHashMap();

Expand Down Expand Up @@ -385,10 +381,6 @@ public TranslationContainer getLeaveMessage() {
return new TranslationContainer(TextFormat.YELLOW + "%multiplayer.player.left", this.getDisplayName());
}

public String getClientSecret() {
return clientSecret;
}

/**
* This might disappear in the future.
* Please use getUniqueId() instead (IP + clientId + name combo, in the future it'll change to real UUID for online auth)
Expand Down Expand Up @@ -740,23 +732,6 @@ public boolean isPlayer() {
return true;
}

/**
* @deprecated will be removed in the future
*/
@Deprecated
public void removeAchievement(String achievementId) {
// achievements.remove(achievementId);
}

/**
* @deprecated will be removed in the future
*/
@Deprecated
public boolean hasAchievement(String achievementId) {
// return achievements.contains(achievementId);
return false;
}

public boolean isConnected() {
return connected;
}
Expand Down Expand Up @@ -1347,39 +1322,6 @@ public void stopSleep() {
}
}

/**
* @deprecated will be removed in the future
*/
@Deprecated
public boolean awardAchievement(String achievementId) {
return false;
/*if (!Server.getInstance().getPropertyBoolean("achievements", true)) {
return false;
}
Achievement achievement = Achievement.achievements.get(achievementId);
if (achievement == null || hasAchievement(achievementId)) {
return false;
}
for (String id : achievement.requires) {
if (!this.hasAchievement(id)) {
return false;
}
}
PlayerAchievementAwardedEvent event = new PlayerAchievementAwardedEvent(this, achievementId);
this.server.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return false;
}
this.achievements.add(achievementId);
achievement.broadcast(this);
return true;*/
}

public int getGamemode() {
return gamemode;
}
Expand Down Expand Up @@ -2008,6 +1950,13 @@ public boolean onUpdate(int currentTick) {
}
this.inAirTicks = 0;
this.highestPosition = this.y;

if (EXPERIMENTAL_COMBAT_KNOCKBACK_TEST) {
// 地面阻力
this.motionX *= 0.3;
this.motionY = 0;
this.motionZ *= 0.3;
}
} else {
if (this.checkMovement && !this.isGliding() && !server.getAllowFlight() && !this.getAdventureSettings().get(Type.ALLOW_FLIGHT) && this.inAirTicks > 20 && !this.isSleeping() && !this.isImmobile() && !this.isSwimming() && this.riding == null && !this.hasEffect(Effect.LEVITATION) && !this.hasEffect(Effect.SLOW_FALLING)) {
double expectedVelocity = (-this.getGravity()) / ((double) this.getDrag()) - ((-this.getGravity()) / ((double) this.getDrag())) * Math.exp(-((double) this.getDrag()) * ((double) (this.inAirTicks - this.startAirTicks)));
Expand Down Expand Up @@ -2035,8 +1984,20 @@ public boolean onUpdate(int currentTick) {

if (this.isGliding()) this.resetFallDistance();

if (EXPERIMENTAL_COMBAT_KNOCKBACK_TEST) {
// 空气阻力
this.motionX *= 0.9900000095367432;
this.motionY *= 0.9800000190734863;
this.motionZ *= 0.9900000095367432;
}

++this.inAirTicks;
}

if (EXPERIMENTAL_COMBAT_KNOCKBACK_TEST) {
if (Math.abs(this.motionX) < 0.0001) this.motionX = 0;
if (Math.abs(this.motionY) < 0.0001) this.motionY = 0;
if (Math.abs(this.motionZ) < 0.0001) this.motionZ = 0;
}

if (this.isSurvivalLike()) {
Expand Down Expand Up @@ -2329,16 +2290,6 @@ protected void processLogin() {
this.setLevel(level);
}

/*for (Tag achievement : nbt.getCompound("Achievements").getAllTags()) {
if (!(achievement instanceof ByteTag)) {
continue;
}
if (((ByteTag) achievement).getData() > 0) {
this.achievements.add(achievement.getName());
}
}*/

nbt.putLong("lastPlayed", System.currentTimeMillis() / 1000);

UUID uuid = getUniqueId();
Expand Down Expand Up @@ -4314,13 +4265,6 @@ public void save(boolean async) {
this.namedTag.putInt("SpawnBlockPositionZ", this.spawnBlockPosition.getFloorZ());
}

/*CompoundTag achievements = new CompoundTag();
for (String achievement : this.achievements) {
achievements.putByte(achievement, 1);
}
this.namedTag.putCompound("Achievements", achievements);*/

this.namedTag.putInt("playerGameType", this.gamemode);
this.namedTag.putLong("lastPlayed", System.currentTimeMillis() / 1000);

Expand Down Expand Up @@ -4771,18 +4715,16 @@ public boolean attack(EntityDamageEvent source) {
if (!this.isAlive()) {
return false;
}

if (this.isCreativeLike()
// && source.getCause() != DamageCause.SUICIDE
// && source.getCause() != DamageCause.VOID
) {
if (this.isCreativeLike()) {
source.setCancelled();
return false;
} else if (this.getAdventureSettings().get(Type.ALLOW_FLIGHT) && source.getCause() == DamageCause.FALL) {
}
if (this.getAdventureSettings().get(Type.ALLOW_FLIGHT) && source.getCause() == DamageCause.FALL) {
//source.setCancelled();
return false;
} else if (source.getCause() == DamageCause.FALL) {
if (this.getLevel().getBlock(this.getPosition().floor().add(0.5, -1, 0.5)).getId() == Block.SLIME) {
}
if (source.getCause() == DamageCause.FALL) {
if (this.getLevel().getBlock(floor().add(0.5, -1, 0.5)).getId() == Block.SLIME) {
if (!this.isSneaking()) {
//source.setCancelled();
this.resetFallDistance();
Expand All @@ -4795,92 +4737,51 @@ public boolean attack(EntityDamageEvent source) {
return false;
}

boolean add = false;
boolean doubleCritical = false;
if (source instanceof EntityDamageByEntityEvent && source.getCause() == DamageCause.ENTITY_ATTACK) {
Entity damager = ((EntityDamageByEntityEvent) source).getDamager();
if (damager instanceof Player) {
((Player) damager).getFoodData().updateFoodExpLevel(0.1f);
boolean critical = false;
if (source instanceof EntityDamageByEntityEvent event && source.getCause() == DamageCause.ENTITY_ATTACK) {
Entity damager = event.getDamager();
if (damager instanceof Player player) {
player.getFoodData().updateFoodExpLevel(0.1f);
}

//Critical hit

if (!damager.onGround && damager instanceof Player) {
if (((Player) damager).speed != null && ((Player) damager).speed.y > 0) {
//((Player) damager).sendMessage("speed = " + ((Player) damager).speed.y);
if (((Player) damager).attackCriticalThisJump <= 0) {
add = true;
} else if (((Player) damager).getLoginChainData().getCurrentInputMode() != ClientChainData.INPUT_MOUSE) { // 键鼠不允许叠刀
doubleCritical = true;
if (!damager.onGround && damager instanceof Player player) {
if (player.speed != null && player.speed.y > 0) {
if (player.attackCriticalThisJump <= 0) {
critical = true;
source.setDamage(source.getDamage() * 1.3f);
}
}
}

if (add) {
source.setDamage((float) (source.getDamage() * 1.3));

AnimatePacket animate = new AnimatePacket();
animate.action = AnimatePacket.Action.CRITICAL_HIT;
animate.eid = getId();
this.getLevel().addChunkPacket(damager.getChunkX(), damager.getChunkZ(), animate);

this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_ATTACK_STRONG);
}

if (doubleCritical) {
//正在叠刀
Player damagerPlayer = (Player)((EntityDamageByEntityEvent) source).getDamager();
if (damagerPlayer.attackCriticalThisJump < 2) {
this.nextAllowAttack = 0;
// this.attackTime = 0;
source.setDamage((float) (source.getDamage() * 0.2));
}

damagerPlayer.sendPopup("叠刀 × " + damagerPlayer.attackCriticalThisJump);

AnimatePacket animate = new AnimatePacket();
animate.action = AnimatePacket.Action.CRITICAL_HIT;
animate.eid = getId();
damagerPlayer.dataPacket(animate);

this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_ATTACK_STRONG, new Player[]{damagerPlayer});
}
}

if (super.attack(source)) { //!source.isCancelled()
this.stopSleep();

if (this.getLastDamageCause() == source && this.spawned) {
EntityEventPacket pk = new EntityEventPacket();
pk.eid = this.id;
pk.eid = this.getId();
pk.event = EntityEventPacket.HURT_ANIMATION;
Server.broadcastPacket(this.hasSpawned.values(), pk);
// 这边只发给自己,因为广播给他人的已经在EntityLiving中发送了
this.dataPacket(pk);
if (add) {
if (((EntityDamageByEntityEvent) source).getDamager() instanceof Player) {
/*if (((Player) ((EntityDamageByEntityEvent) source).getDamager()).getLoginChainData().getDeviceOS() == 7) //win10
(((EntityDamageByEntityEvent) source).getDamager()).addEffect(Effect.getEffect(Effect.SLOWNESS).setDuration(10).setAmplifier(1).setVisible(false));*/
((Player)(((EntityDamageByEntityEvent) source).getDamager())).attackCriticalThisJump++;
}
/*
Random random = ThreadLocalRandom.current();
for (int i = 0; i < 10; i++) {
CriticalParticle par = new CriticalParticle(new Vector3(this.x + random.nextDouble() * 2 - 1, this.y + random.nextDouble() * 2, this.z + random.nextDouble() * 2 - 1));
this.getLevel().addParticle(par);
}*/
}
if (doubleCritical) {
if (((EntityDamageByEntityEvent) source).getDamager() instanceof Player) {
Player damagerPlayer = (Player)((EntityDamageByEntityEvent) source).getDamager();
damagerPlayer.attackCriticalThisJump++;

if (critical) {
if (((EntityDamageByEntityEvent) source).getDamager() instanceof Player player) {
player.attackCriticalThisJump++;
}
}

// 在这里发送暴击,因为事件可能被取消
AnimatePacket animate = new AnimatePacket();
animate.action = AnimatePacket.Action.CRITICAL_HIT;
animate.eid = getId();
this.getLevel().addChunkPacket(this.getChunkX(), this.getChunkZ(), animate);

this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_ATTACK_STRONG);
}
}
return true;
} else {
return false;
}
return false;
}

protected boolean isDamageBlocked(EntityDamageEvent source) {
Expand Down Expand Up @@ -5720,16 +5621,6 @@ public boolean pickupEntity(Entity entity, boolean near) {
return false;
}

/*switch (item.getId()) {
case Item.LOG:
case Item.LOG2:
this.awardAchievement("mineWood");
break;
case Item.DIAMOND:
this.awardAchievement("diamond");
break;
}*/

TakeItemEntityPacket pk = new TakeItemEntityPacket();
pk.entityId = this.getId();
pk.target = entity.getId();
Expand Down
1 change: 0 additions & 1 deletion src/main/java/cn/nukkit/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,6 @@ public CompoundTag getOfflinePlayerData(String name) {
.putLong("lastPlayed", System.currentTimeMillis() / 1000)
.putString("Level", this.getDefaultLevel().getName())
.putList(new ListTag<>("Inventory"))
// .putCompound("Achievements", new CompoundTag())
.putInt("playerGameType", this.getGamemode())
.putFloat("FallDistance", 0)
.putShort("Fire", 0)
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cn/nukkit/SharedConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public final class SharedConstants {

// experimental

public static final boolean EXPERIMENTAL_COMBAT_KNOCKBACK_TEST = false;

public static final boolean NEXT_UPDATE_NEW_ENTITY_SYSTEM_PREVIEW = !PRODUCTION_ENVIRONMENT && false;

public static final boolean NEXT_UPDATE_EVENT_POOLING_PREVIEW = !PRODUCTION_ENVIRONMENT && false;
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/cn/nukkit/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockDripstonePointed;
import cn.nukkit.block.BlockID;
import cn.nukkit.block.BlockLiquid;
import cn.nukkit.block.BlockWater;
import cn.nukkit.block.*;
import cn.nukkit.blockentity.BlockEntityPistonArm;
import cn.nukkit.entity.attribute.Attribute;
import cn.nukkit.entity.data.*;
Expand Down Expand Up @@ -47,7 +43,10 @@
import javax.annotation.Nullable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.*;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;

Expand Down Expand Up @@ -1224,6 +1223,9 @@ public boolean attack(float damage) {
return this.attack(new EntityDamageEvent(this, DamageCause.CUSTOM, damage));
}

protected void onAttackSuccess(EntityDamageByEntityEvent source) {
}

public void heal(EntityRegainHealthEvent source) {
this.server.getPluginManager().callEvent(source);
if (source.isCancelled()) {
Expand Down
Loading

0 comments on commit 5ff4243

Please sign in to comment.