Skip to content

Commit

Permalink
完善一些内容
Browse files Browse the repository at this point in the history
  • Loading branch information
wode490390 committed Apr 4, 2024
1 parent f6dca15 commit f25711f
Show file tree
Hide file tree
Showing 74 changed files with 640 additions and 288 deletions.
133 changes: 110 additions & 23 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ public class Player extends EntityHuman implements CommandSender, InventoryHolde
protected boolean swinging;
protected int swingTime;

protected int damageNearbyMobsTick;

protected int shieldBlockingTick;
protected int prevShieldBlockingTick;
protected int shieldCooldown;
Expand Down Expand Up @@ -1090,9 +1092,8 @@ protected void doFirstSpawn() {

this.noDamageTicks = 60;

this.sendRecipeList();

this.sendCreativeContents();
this.sendRecipeList();

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

this.sendCreativeContents();
// this.sendCreativeContents();
}

return true;
Expand Down Expand Up @@ -2027,9 +2028,7 @@ public boolean onUpdate(int currentTick) {
if (chestplate.getId() == Item.ELYTRA) {
int damage = chestplate.getDamage();
if (damage < chestplate.getMaxDurability() - 1) {
Enchantment durability = chestplate.getEnchantment(Enchantment.UNBREAKING);
if (durability == null || durability.getLevel() <= 0 || ThreadLocalRandom.current().nextInt(100) < chestplate.getDamageChance(durability.getLevel())) {
chestplate.setDamage(++damage);
if (chestplate.hurtAndBreak(1) != 0) {
inventory.setChestplate(chestplate, true);
}
} else {
Expand Down Expand Up @@ -2096,6 +2095,70 @@ public boolean onUpdate(int currentTick) {
} else {
this.swingTime = 0;
}

if (damageNearbyMobsTick > 0 && !isSpectator()) {
damageNearbyMobsTick--;

for (Entity entity : level.getNearbyEntities(boundingBox, this)) {
if (!(entity instanceof EntityLiving)) {
continue;
}

if (entity instanceof Player player && (player.isCreativeLike() || server.getDifficulty() == 0 || !server.getConfiguration().isPvp() || !level.getGameRules().getBoolean(GameRule.PVP))) {
continue;
}

Item item = inventory.getItemInHand();
Enchantment[] enchantments = item.getId() != Item.ENCHANTED_BOOK ? item.getEnchantments() : Enchantment.EMPTY;

ItemAttackDamageEvent event = new ItemAttackDamageEvent(item);
event.call();
float damage = event.getAttackDamage();

float damageBonus = 0;
for (Enchantment enchantment : enchantments) {
damageBonus += enchantment.getDamageBonus(entity);
}
damage += Mth.floor(damageBonus);

Map<DamageModifier, Float> modifiers = new EnumMap<>(DamageModifier.class);
modifiers.put(DamageModifier.BASE, damage);

float knockbackH = EntityDamageByEntityEvent.GLOBAL_KNOCKBACK_H;
float knockbackV = EntityDamageByEntityEvent.GLOBAL_KNOCKBACK_V;
int knockbackEnchant = item.getEnchantmentLevel(Enchantment.KNOCKBACK);
if (knockbackEnchant > 0) {
knockbackH += knockbackEnchant * 0.1f;
knockbackV += knockbackEnchant * 0.1f;
}

if (!entity.attack(new EntityDamageByEntityEvent(this, entity, DamageCause.ENTITY_ATTACK, modifiers, knockbackH, knockbackV, enchantments))) {
continue;
}

for (Enchantment enchantment : enchantments) {
enchantment.doPostAttack(item, this, entity, null);
}

if (isSurvivalLike() && item.isTool() && item.useOn(entity)) {
if (item.getDamage() > item.getMaxDurability()) {
inventory.setItemInHand(Items.air());
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_BREAK);
} else {
inventory.setItemInHand(item);
}
}

setMotion(getMotion().multiply(-0.2));

damageNearbyMobsTick = 0;
setDataFlag(DATA_FLAG_SPIN_ATTACK, false);
break;
}
} else {
damageNearbyMobsTick = 0;
setDataFlag(DATA_FLAG_SPIN_ATTACK, false);
}
}

this.checkTeleportPosition();
Expand Down Expand Up @@ -3340,7 +3403,8 @@ public void onCompletion(Server server) {
case ProtocolInfo.REQUEST_CHUNK_RADIUS_PACKET:
RequestChunkRadiusPacket requestChunkRadiusPacket = (RequestChunkRadiusPacket) packet;
ChunkRadiusUpdatedPacket chunkRadiusUpdatePacket = new ChunkRadiusUpdatedPacket();
this.chunkRadius = Math.max(4, Math.min(requestChunkRadiusPacket.radius, this.viewDistance));
this.chunkRadius = Math.max(4, Math.min(requestChunkRadiusPacket.radius, this.server.getViewDistance()));
this.viewDistance = this.chunkRadius;
chunkRadiusUpdatePacket.radius = this.chunkRadius;
this.dataPacket(chunkRadiusUpdatePacket);
break;
Expand Down Expand Up @@ -3620,7 +3684,7 @@ public void onCompletion(Server server) {
}

if (item.onClickAir(this, directionVector)) {
if (this.isSurvival()) {
if (this.isSurvivalLike()) {
this.inventory.setItemInHand(item);
}

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

if (target.onInteract(this, item) && this.isSurvival()) {
if (item.isTool()) {
if (item.useOn(target) && item.getDamage() >= item.getMaxDurability()) {
if (item.useOn(target) && item.getDamage() > item.getMaxDurability()) {
item = Items.air();
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_BREAK);
}
} else {
if (item.count > 1) {
Expand Down Expand Up @@ -3697,10 +3762,14 @@ public void onCompletion(Server server) {

ItemAttackDamageEvent event = new ItemAttackDamageEvent(item);
this.server.getPluginManager().callEvent(event);

float itemDamage = event.getAttackDamage();

float damageBonus = 0;
for (Enchantment enchantment : enchantments) {
itemDamage += enchantment.getDamageBonus(target);
damageBonus += enchantment.getDamageBonus(target);
}
itemDamage += Mth.floor(damageBonus);

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

for (Enchantment enchantment : enchantments) {
enchantment.doPostAttack(this, target, null);
enchantment.doPostAttack(item, this, target, null);
}

if (item.isTool() && this.isSurvivalLike()) {
if (item.useOn(target) && item.getDamage() >= item.getMaxDurability()) {
this.inventory.setItemInHand(Item.get(0));
if (item.useOn(target)) {
if (item.getDamage() > item.getMaxDurability()) {
this.inventory.setItemInHand(Item.get(Item.AIR));
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_BREAK);
} else {
this.inventory.setItemInHand(item);
}
} else {
if (item.getId() == 0 || this.inventory.getItemInHand().getId() == item.getId()) {
if (item.getId() == Item.AIR || this.inventory.getItemInHand().getId() == item.getId()) {
this.inventory.setItemInHand(item);
} else {
server.getLogger().debug("Tried to set item " + item.getId() + " but " + this.username + " had item " + this.inventory.getItemInHand().getId() + " in their hand slot");
Expand Down Expand Up @@ -4493,10 +4567,12 @@ public void kill() {
message = "death.attack.magma";
break;
case FIRE:
message = "death.attack.onFire";
case CAMPFIRE:
case SOUL_CAMPFIRE:
message = "death.attack.inFire";
break;
case FIRE_TICK:
message = "death.attack.inFire";
message = "death.attack.onFire";
break;
case DROWNING:
message = "death.attack.drown";
Expand Down Expand Up @@ -4808,7 +4884,7 @@ public boolean attack(EntityDamageEvent source) {
}

//Critical hit
if (!damager.onGround && damager instanceof Player player && !damager.hasEffect(Effect.BLINDNESS)) {
if (!damager.onGround && damager instanceof Player player && !damager.hasEffect(Effect.BLINDNESS) && !damager.isRiding() && !damager.isInsideOfWater(false)) {
if (player.speed != null && player.speed.y > 0) {
if (player.attackCriticalThisJump <= 0) {
critical = true;
Expand Down Expand Up @@ -4912,10 +4988,7 @@ protected boolean blockUsingShield(EntityDamageEvent source) {
float damage = source.getDamage();
if (damage >= 3) {
Item shield = itemStack.value();
int itemDamage = shield.getDamage() + Mth.floor(damage + 1);
if (itemDamage < shield.getMaxDurability()) {
shield.setDamage(itemDamage);
} else {
if (shield.hurtAndBreak(Mth.floor(damage) + 1) < 0) {
shield.pop();
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_BREAK);
}
Expand Down Expand Up @@ -5824,14 +5897,16 @@ public void startFishing(Item fishingRod) {
* Stop fishing
* @param click clicked or forced
*/
public void stopFishing(boolean click) {
public int stopFishing(boolean click) {
int itemDamage = 0;
if (this.fishing != null && click) {
fishing.reelLine();
itemDamage = fishing.reelLine();
} else if (this.fishing != null) {
this.fishing.close();
}

this.fishing = null;
return itemDamage;
}

@Override
Expand Down Expand Up @@ -6290,4 +6365,16 @@ public void showHud() {
*/
public void showHudElements(int... elements) {
}

public void swingArm() {
AnimatePacket pk = new AnimatePacket();
pk.eid = getId();
pk.action = AnimatePacket.Action.SWING_ARM;
dataPacket(pk);

AnimatePacket pkBroadcast = new AnimatePacket();
pkBroadcast.eid = getId();
pkBroadcast.action = AnimatePacket.Action.SWING_ARM;
Server.broadcastPacket(getViewers().values(), pkBroadcast);
}
}
4 changes: 0 additions & 4 deletions src/main/java/cn/nukkit/block/BlockBeehive.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ public boolean onActivate(Item item, BlockFace face, Player player) {
setHoneyLevel(0);
level.setBlock(this, this, true, true);

if (player != null && !player.isCreative()) {
item.useOn(this);
}

if (level.getGameRules().getBoolean(GameRule.DO_TILE_DROPS)) {
level.dropItem(blockCenter(), Item.get(Item.HONEYCOMB, 0, 3));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/block/BlockCake.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private boolean tryAddCandle(Item item, @Nullable Player player, int candleCakeB
item.pop();
}

level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_AMBIENT_CANDLE);
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_CAKE_ADD_CANDLE);

level.setBlock(this, get(candleCakeBlockId), true);
return true;
Expand Down
30 changes: 8 additions & 22 deletions src/main/java/cn/nukkit/block/BlockCakeCandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import cn.nukkit.item.food.Food;
import cn.nukkit.level.Level;
import cn.nukkit.math.BlockFace;
import cn.nukkit.network.protocol.LevelEventPacket;
import cn.nukkit.network.protocol.LevelSoundEventPacket;

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

level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_IGNITE);
level.addLevelEvent(this, LevelEventPacket.EVENT_SOUND_GHAST_SHOOT, 78642);

setLit(true);
level.setBlock(this, this, true);
return true;
}
if (id == Item.FLINT_AND_STEEL) {
if (isLit()) {
return true;
}

if (player != null && !player.isCreative()) {
item.useOn(this);
}

if (level.getExtraBlock(this).isWaterSource()) {
return true;
}

level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_IGNITE);

setLit(true);
level.setBlock(this, this, true);
return true;
}
if (item.hasEnchantment(Enchantment.FIRE_ASPECT)) {
if (id == Item.FLINT_AND_STEEL || item.isSword() && item.hasEnchantment(Enchantment.FIRE_ASPECT)) {
if (isLit()) {
return true;
}
Expand All @@ -99,8 +82,9 @@ public boolean onActivate(Item item, BlockFace face, Player player) {
return true;
}

if (player != null && !player.isCreative()) {
item.useOn(this);
if (player != null && player.isSurvivalLike() && item.hurtAndBreak(1) < 0) {
item.pop();
player.level.addLevelSoundEvent(player, LevelSoundEventPacket.SOUND_BREAK);
}

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

level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_EXTINGUISH_CANDLE);

setLit(false);
level.setBlock(this, this, true);
return true;
Expand Down
Loading

0 comments on commit f25711f

Please sign in to comment.