Skip to content

Commit f25711f

Browse files
committed
完善一些内容
1 parent f6dca15 commit f25711f

File tree

74 files changed

+640
-288
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+640
-288
lines changed

src/main/java/cn/nukkit/Player.java

Lines changed: 110 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ public class Player extends EntityHuman implements CommandSender, InventoryHolde
317317
protected boolean swinging;
318318
protected int swingTime;
319319

320+
protected int damageNearbyMobsTick;
321+
320322
protected int shieldBlockingTick;
321323
protected int prevShieldBlockingTick;
322324
protected int shieldCooldown;
@@ -1090,9 +1092,8 @@ protected void doFirstSpawn() {
10901092

10911093
this.noDamageTicks = 60;
10921094

1093-
this.sendRecipeList();
1094-
10951095
this.sendCreativeContents();
1096+
this.sendRecipeList();
10961097

10971098
for (long index : this.usedChunks.keySet()) {
10981099
int chunkX = Level.getHashX(index);
@@ -1440,7 +1441,7 @@ public boolean setGamemode(int gamemode, boolean clientSide, AdventureSettings n
14401441
this.inventory.sendContents(this);
14411442
this.inventory.sendHeldItem(this.hasSpawned.values());
14421443

1443-
this.sendCreativeContents();
1444+
// this.sendCreativeContents();
14441445
}
14451446

14461447
return true;
@@ -2027,9 +2028,7 @@ public boolean onUpdate(int currentTick) {
20272028
if (chestplate.getId() == Item.ELYTRA) {
20282029
int damage = chestplate.getDamage();
20292030
if (damage < chestplate.getMaxDurability() - 1) {
2030-
Enchantment durability = chestplate.getEnchantment(Enchantment.UNBREAKING);
2031-
if (durability == null || durability.getLevel() <= 0 || ThreadLocalRandom.current().nextInt(100) < chestplate.getDamageChance(durability.getLevel())) {
2032-
chestplate.setDamage(++damage);
2031+
if (chestplate.hurtAndBreak(1) != 0) {
20332032
inventory.setChestplate(chestplate, true);
20342033
}
20352034
} else {
@@ -2096,6 +2095,70 @@ public boolean onUpdate(int currentTick) {
20962095
} else {
20972096
this.swingTime = 0;
20982097
}
2098+
2099+
if (damageNearbyMobsTick > 0 && !isSpectator()) {
2100+
damageNearbyMobsTick--;
2101+
2102+
for (Entity entity : level.getNearbyEntities(boundingBox, this)) {
2103+
if (!(entity instanceof EntityLiving)) {
2104+
continue;
2105+
}
2106+
2107+
if (entity instanceof Player player && (player.isCreativeLike() || server.getDifficulty() == 0 || !server.getConfiguration().isPvp() || !level.getGameRules().getBoolean(GameRule.PVP))) {
2108+
continue;
2109+
}
2110+
2111+
Item item = inventory.getItemInHand();
2112+
Enchantment[] enchantments = item.getId() != Item.ENCHANTED_BOOK ? item.getEnchantments() : Enchantment.EMPTY;
2113+
2114+
ItemAttackDamageEvent event = new ItemAttackDamageEvent(item);
2115+
event.call();
2116+
float damage = event.getAttackDamage();
2117+
2118+
float damageBonus = 0;
2119+
for (Enchantment enchantment : enchantments) {
2120+
damageBonus += enchantment.getDamageBonus(entity);
2121+
}
2122+
damage += Mth.floor(damageBonus);
2123+
2124+
Map<DamageModifier, Float> modifiers = new EnumMap<>(DamageModifier.class);
2125+
modifiers.put(DamageModifier.BASE, damage);
2126+
2127+
float knockbackH = EntityDamageByEntityEvent.GLOBAL_KNOCKBACK_H;
2128+
float knockbackV = EntityDamageByEntityEvent.GLOBAL_KNOCKBACK_V;
2129+
int knockbackEnchant = item.getEnchantmentLevel(Enchantment.KNOCKBACK);
2130+
if (knockbackEnchant > 0) {
2131+
knockbackH += knockbackEnchant * 0.1f;
2132+
knockbackV += knockbackEnchant * 0.1f;
2133+
}
2134+
2135+
if (!entity.attack(new EntityDamageByEntityEvent(this, entity, DamageCause.ENTITY_ATTACK, modifiers, knockbackH, knockbackV, enchantments))) {
2136+
continue;
2137+
}
2138+
2139+
for (Enchantment enchantment : enchantments) {
2140+
enchantment.doPostAttack(item, this, entity, null);
2141+
}
2142+
2143+
if (isSurvivalLike() && item.isTool() && item.useOn(entity)) {
2144+
if (item.getDamage() > item.getMaxDurability()) {
2145+
inventory.setItemInHand(Items.air());
2146+
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_BREAK);
2147+
} else {
2148+
inventory.setItemInHand(item);
2149+
}
2150+
}
2151+
2152+
setMotion(getMotion().multiply(-0.2));
2153+
2154+
damageNearbyMobsTick = 0;
2155+
setDataFlag(DATA_FLAG_SPIN_ATTACK, false);
2156+
break;
2157+
}
2158+
} else {
2159+
damageNearbyMobsTick = 0;
2160+
setDataFlag(DATA_FLAG_SPIN_ATTACK, false);
2161+
}
20992162
}
21002163

21012164
this.checkTeleportPosition();
@@ -3340,7 +3403,8 @@ public void onCompletion(Server server) {
33403403
case ProtocolInfo.REQUEST_CHUNK_RADIUS_PACKET:
33413404
RequestChunkRadiusPacket requestChunkRadiusPacket = (RequestChunkRadiusPacket) packet;
33423405
ChunkRadiusUpdatedPacket chunkRadiusUpdatePacket = new ChunkRadiusUpdatedPacket();
3343-
this.chunkRadius = Math.max(4, Math.min(requestChunkRadiusPacket.radius, this.viewDistance));
3406+
this.chunkRadius = Math.max(4, Math.min(requestChunkRadiusPacket.radius, this.server.getViewDistance()));
3407+
this.viewDistance = this.chunkRadius;
33443408
chunkRadiusUpdatePacket.radius = this.chunkRadius;
33453409
this.dataPacket(chunkRadiusUpdatePacket);
33463410
break;
@@ -3620,7 +3684,7 @@ public void onCompletion(Server server) {
36203684
}
36213685

36223686
if (item.onClickAir(this, directionVector)) {
3623-
if (this.isSurvival()) {
3687+
if (this.isSurvivalLike()) {
36243688
this.inventory.setItemInHand(item);
36253689
}
36263690

@@ -3668,8 +3732,9 @@ public void onCompletion(Server server) {
36683732

36693733
if (target.onInteract(this, item) && this.isSurvival()) {
36703734
if (item.isTool()) {
3671-
if (item.useOn(target) && item.getDamage() >= item.getMaxDurability()) {
3735+
if (item.useOn(target) && item.getDamage() > item.getMaxDurability()) {
36723736
item = Items.air();
3737+
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_BREAK);
36733738
}
36743739
} else {
36753740
if (item.count > 1) {
@@ -3697,10 +3762,14 @@ public void onCompletion(Server server) {
36973762

36983763
ItemAttackDamageEvent event = new ItemAttackDamageEvent(item);
36993764
this.server.getPluginManager().callEvent(event);
3765+
37003766
float itemDamage = event.getAttackDamage();
3767+
3768+
float damageBonus = 0;
37013769
for (Enchantment enchantment : enchantments) {
3702-
itemDamage += enchantment.getDamageBonus(target);
3770+
damageBonus += enchantment.getDamageBonus(target);
37033771
}
3772+
itemDamage += Mth.floor(damageBonus);
37043773

37053774
Map<DamageModifier, Float> damage = new EnumMap<>(DamageModifier.class);
37063775
damage.put(DamageModifier.BASE, itemDamage);
@@ -3727,14 +3796,19 @@ public void onCompletion(Server server) {
37273796
}
37283797

37293798
for (Enchantment enchantment : enchantments) {
3730-
enchantment.doPostAttack(this, target, null);
3799+
enchantment.doPostAttack(item, this, target, null);
37313800
}
37323801

37333802
if (item.isTool() && this.isSurvivalLike()) {
3734-
if (item.useOn(target) && item.getDamage() >= item.getMaxDurability()) {
3735-
this.inventory.setItemInHand(Item.get(0));
3803+
if (item.useOn(target)) {
3804+
if (item.getDamage() > item.getMaxDurability()) {
3805+
this.inventory.setItemInHand(Item.get(Item.AIR));
3806+
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_BREAK);
3807+
} else {
3808+
this.inventory.setItemInHand(item);
3809+
}
37363810
} else {
3737-
if (item.getId() == 0 || this.inventory.getItemInHand().getId() == item.getId()) {
3811+
if (item.getId() == Item.AIR || this.inventory.getItemInHand().getId() == item.getId()) {
37383812
this.inventory.setItemInHand(item);
37393813
} else {
37403814
server.getLogger().debug("Tried to set item " + item.getId() + " but " + this.username + " had item " + this.inventory.getItemInHand().getId() + " in their hand slot");
@@ -4493,10 +4567,12 @@ public void kill() {
44934567
message = "death.attack.magma";
44944568
break;
44954569
case FIRE:
4496-
message = "death.attack.onFire";
4570+
case CAMPFIRE:
4571+
case SOUL_CAMPFIRE:
4572+
message = "death.attack.inFire";
44974573
break;
44984574
case FIRE_TICK:
4499-
message = "death.attack.inFire";
4575+
message = "death.attack.onFire";
45004576
break;
45014577
case DROWNING:
45024578
message = "death.attack.drown";
@@ -4808,7 +4884,7 @@ public boolean attack(EntityDamageEvent source) {
48084884
}
48094885

48104886
//Critical hit
4811-
if (!damager.onGround && damager instanceof Player player && !damager.hasEffect(Effect.BLINDNESS)) {
4887+
if (!damager.onGround && damager instanceof Player player && !damager.hasEffect(Effect.BLINDNESS) && !damager.isRiding() && !damager.isInsideOfWater(false)) {
48124888
if (player.speed != null && player.speed.y > 0) {
48134889
if (player.attackCriticalThisJump <= 0) {
48144890
critical = true;
@@ -4912,10 +4988,7 @@ protected boolean blockUsingShield(EntityDamageEvent source) {
49124988
float damage = source.getDamage();
49134989
if (damage >= 3) {
49144990
Item shield = itemStack.value();
4915-
int itemDamage = shield.getDamage() + Mth.floor(damage + 1);
4916-
if (itemDamage < shield.getMaxDurability()) {
4917-
shield.setDamage(itemDamage);
4918-
} else {
4991+
if (shield.hurtAndBreak(Mth.floor(damage) + 1) < 0) {
49194992
shield.pop();
49204993
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_BREAK);
49214994
}
@@ -5824,14 +5897,16 @@ public void startFishing(Item fishingRod) {
58245897
* Stop fishing
58255898
* @param click clicked or forced
58265899
*/
5827-
public void stopFishing(boolean click) {
5900+
public int stopFishing(boolean click) {
5901+
int itemDamage = 0;
58285902
if (this.fishing != null && click) {
5829-
fishing.reelLine();
5903+
itemDamage = fishing.reelLine();
58305904
} else if (this.fishing != null) {
58315905
this.fishing.close();
58325906
}
58335907

58345908
this.fishing = null;
5909+
return itemDamage;
58355910
}
58365911

58375912
@Override
@@ -6290,4 +6365,16 @@ public void showHud() {
62906365
*/
62916366
public void showHudElements(int... elements) {
62926367
}
6368+
6369+
public void swingArm() {
6370+
AnimatePacket pk = new AnimatePacket();
6371+
pk.eid = getId();
6372+
pk.action = AnimatePacket.Action.SWING_ARM;
6373+
dataPacket(pk);
6374+
6375+
AnimatePacket pkBroadcast = new AnimatePacket();
6376+
pkBroadcast.eid = getId();
6377+
pkBroadcast.action = AnimatePacket.Action.SWING_ARM;
6378+
Server.broadcastPacket(getViewers().values(), pkBroadcast);
6379+
}
62936380
}

src/main/java/cn/nukkit/block/BlockBeehive.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ public boolean onActivate(Item item, BlockFace face, Player player) {
121121
setHoneyLevel(0);
122122
level.setBlock(this, this, true, true);
123123

124-
if (player != null && !player.isCreative()) {
125-
item.useOn(this);
126-
}
127-
128124
if (level.getGameRules().getBoolean(GameRule.DO_TILE_DROPS)) {
129125
level.dropItem(blockCenter(), Item.get(Item.HONEYCOMB, 0, 3));
130126
}

src/main/java/cn/nukkit/block/BlockCake.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private boolean tryAddCandle(Item item, @Nullable Player player, int candleCakeB
239239
item.pop();
240240
}
241241

242-
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_AMBIENT_CANDLE);
242+
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_CAKE_ADD_CANDLE);
243243

244244
level.setBlock(this, get(candleCakeBlockId), true);
245245
return true;

src/main/java/cn/nukkit/block/BlockCakeCandle.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import cn.nukkit.item.food.Food;
1010
import cn.nukkit.level.Level;
1111
import cn.nukkit.math.BlockFace;
12+
import cn.nukkit.network.protocol.LevelEventPacket;
1213
import cn.nukkit.network.protocol.LevelSoundEventPacket;
1314

1415
public class BlockCakeCandle extends BlockCake {
@@ -66,31 +67,13 @@ public boolean onActivate(Item item, BlockFace face, Player player) {
6667
}
6768

6869
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_IGNITE);
70+
level.addLevelEvent(this, LevelEventPacket.EVENT_SOUND_GHAST_SHOOT, 78642);
6971

7072
setLit(true);
7173
level.setBlock(this, this, true);
7274
return true;
7375
}
74-
if (id == Item.FLINT_AND_STEEL) {
75-
if (isLit()) {
76-
return true;
77-
}
78-
79-
if (player != null && !player.isCreative()) {
80-
item.useOn(this);
81-
}
82-
83-
if (level.getExtraBlock(this).isWaterSource()) {
84-
return true;
85-
}
86-
87-
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_IGNITE);
88-
89-
setLit(true);
90-
level.setBlock(this, this, true);
91-
return true;
92-
}
93-
if (item.hasEnchantment(Enchantment.FIRE_ASPECT)) {
76+
if (id == Item.FLINT_AND_STEEL || item.isSword() && item.hasEnchantment(Enchantment.FIRE_ASPECT)) {
9477
if (isLit()) {
9578
return true;
9679
}
@@ -99,8 +82,9 @@ public boolean onActivate(Item item, BlockFace face, Player player) {
9982
return true;
10083
}
10184

102-
if (player != null && !player.isCreative()) {
103-
item.useOn(this);
85+
if (player != null && player.isSurvivalLike() && item.hurtAndBreak(1) < 0) {
86+
item.pop();
87+
player.level.addLevelSoundEvent(player, LevelSoundEventPacket.SOUND_BREAK);
10488
}
10589

10690
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_IGNITE);
@@ -124,6 +108,8 @@ public boolean onActivate(Item item, BlockFace face, Player player) {
124108
return false;
125109
}
126110

111+
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_EXTINGUISH_CANDLE);
112+
127113
setLit(false);
128114
level.setBlock(this, this, true);
129115
return true;

0 commit comments

Comments
 (0)