Skip to content

Commit

Permalink
Refactor riptide
Browse files Browse the repository at this point in the history
  • Loading branch information
PetteriM1 committed Oct 6, 2024
1 parent 26f1282 commit 4ba1a10
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 89 deletions.
66 changes: 9 additions & 57 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ public class Player extends EntityHuman implements CommandSender, InventoryHolde
private int fireworkBoostTicks;
private int fireworkBoostLevel;

@Setter
private boolean needSendData;
private boolean needSendAdventureSettings;
private boolean needSendFoodLevel;
Expand Down Expand Up @@ -370,6 +371,13 @@ public void onFireworkBoost(int boostLevel) {
this.fireworkBoostTicks = boostLevel == 3 ? 44 : boostLevel == 2 ? 29 : 23;
}

/**
* Set last spin attack tick to current tick
*/
public void onSpinAttack(int riptideLevel) {
this.riptideTicks = 50 + (riptideLevel << 5);
}

/**
* Get ender chest the player is viewing
* @return the ender chest player is viewing or null if player is not viewing an ender chest
Expand Down Expand Up @@ -3415,64 +3423,8 @@ public void onCompletion(Server server) {
break stopItemHold;
case PlayerActionPacket.ACTION_STOP_SWIMMING:
return;
case PlayerActionPacket.ACTION_START_SPIN_ATTACK:
if (this.inventory == null) {
break stopItemHold;
}

PlayerToggleSpinAttackEvent playerToggleSpinAttackEvent = new PlayerToggleSpinAttackEvent(this, true);

int riptideLevel = 0;
Item hand;
if ((hand = this.inventory.getItemInHandFast()).getId() != ItemID.TRIDENT) {
playerToggleSpinAttackEvent.setCancelled(true);
this.getServer().getLogger().debug(username + ": got ACTION_START_SPIN_ATTACK but hand item is not a trident");
} else {
Enchantment riptide = hand.getEnchantment(Enchantment.ID_TRIDENT_RIPTIDE);
if (riptide == null) {
playerToggleSpinAttackEvent.setCancelled(true);
} else {
riptideLevel = riptide.getLevel();
if (riptideLevel < 1) {
playerToggleSpinAttackEvent.setCancelled(true);
} else {
boolean inWater = false;
for (Block block : this.getCollisionBlocks()) {
if (block instanceof BlockWater) {
inWater = true;
break;
}
}
if (!(inWater || (this.getLevel().isRaining() && this.canSeeSky()))) {
playerToggleSpinAttackEvent.setCancelled(true);
}
}
}
}

this.server.getPluginManager().callEvent(playerToggleSpinAttackEvent);

if (playerToggleSpinAttackEvent.isCancelled()) {
this.needSendData = true;
} else {
this.setSpinAttack(true);
this.resetFallDistance();

this.riptideTicks = 50 + (riptideLevel << 5);

int riptideSound;
if (riptideLevel >= 3) {
riptideSound = LevelSoundEventPacket.SOUND_ITEM_TRIDENT_RIPTIDE_3;
} else if (riptideLevel == 2) {
riptideSound = LevelSoundEventPacket.SOUND_ITEM_TRIDENT_RIPTIDE_2;
} else {
riptideSound = LevelSoundEventPacket.SOUND_ITEM_TRIDENT_RIPTIDE_1;
}
this.level.addLevelSoundEvent(this, riptideSound);
}
break stopItemHold;
case PlayerActionPacket.ACTION_STOP_SPIN_ATTACK:
playerToggleSpinAttackEvent = new PlayerToggleSpinAttackEvent(this, false);
PlayerToggleSpinAttackEvent playerToggleSpinAttackEvent = new PlayerToggleSpinAttackEvent(this, false);
this.server.getPluginManager().callEvent(playerToggleSpinAttackEvent);
if (playerToggleSpinAttackEvent.isCancelled()) {
this.needSendData = true;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/cn/nukkit/command/simple/SimpleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import cn.nukkit.command.Command;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.ConsoleCommandSender;
import cn.nukkit.command.defaults.VanillaCommand;
import cn.nukkit.lang.TranslationContainer;

import java.lang.reflect.Method;
Expand Down
35 changes: 7 additions & 28 deletions src/main/java/cn/nukkit/entity/item/EntityFallingBlock.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.nukkit.entity.item;

import cn.nukkit.Player;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockID;
import cn.nukkit.block.BlockLiquid;
Expand All @@ -14,12 +13,10 @@
import cn.nukkit.item.Item;
import cn.nukkit.level.GameRule;
import cn.nukkit.level.GlobalBlockPalette;
import cn.nukkit.level.Level;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.math.BlockFace;
import cn.nukkit.math.Vector3;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.network.protocol.AddEntityPacket;
import cn.nukkit.network.protocol.LevelEventPacket;

/**
Expand Down Expand Up @@ -94,31 +91,8 @@ protected void initEntity() {
}

this.fireProof = true;
}

@Override
public void spawnTo(Player player) {
if (!this.hasSpawned.containsKey(player.getLoaderId())) {
Boolean hasChunk = player.usedChunks.get(Level.chunkHash(this.chunk.getX(), this.chunk.getZ()));
if (hasChunk != null && hasChunk) {
AddEntityPacket addEntity = new AddEntityPacket();
addEntity.type = this.getNetworkId();
addEntity.entityUniqueId = this.id;
addEntity.entityRuntimeId = this.id;
addEntity.yaw = (float) this.yaw;
addEntity.headYaw = (float) this.yaw;
addEntity.pitch = (float) this.pitch;
addEntity.x = (float) this.x;
addEntity.y = (float) this.y;
addEntity.z = (float) this.z;
addEntity.speedX = (float) this.motionX;
addEntity.speedY = (float) this.motionY;
addEntity.speedZ = (float) this.motionZ;
addEntity.metadata = this.dataProperties.clone().put(new IntEntityData(DATA_VARIANT, GlobalBlockPalette.getOrCreateRuntimeId(this.blockId, this.damage)));
player.dataPacket(addEntity);
this.hasSpawned.put(player.getLoaderId(), player);
}
}
this.setDataProperty(new IntEntityData(DATA_VARIANT, GlobalBlockPalette.getOrCreateRuntimeId(this.blockId, this.damage)));
}

public boolean canCollideWith(Entity entity) {
Expand Down Expand Up @@ -158,12 +132,14 @@ public boolean onUpdate(int currentTick) {

if (onGround && !closed) {
close();

Block floorBlock = level.getBlock(this.add(0, 0.0001, 0));
if (this.getBlock() == Block.SNOW_LAYER && floorBlock.getId() == Block.SNOW_LAYER && (floorBlock.getDamage() & 0x7) != 0x7) {
int mergedHeight = (floorBlock.getDamage() & 0x7) + 1 + (this.getDamage() & 0x7) + 1;
if (mergedHeight > 8) {
EntityBlockChangeEvent event = new EntityBlockChangeEvent(this, floorBlock, Block.get(Block.SNOW_LAYER, 0x7));
this.server.getPluginManager().callEvent(event);

if (!event.isCancelled()) {
this.level.setBlock(floorBlock, event.getTo(), true);

Expand All @@ -172,6 +148,7 @@ public boolean onUpdate(int currentTick) {
if (aboveBlock.getId() == Block.AIR) {
EntityBlockChangeEvent event2 = new EntityBlockChangeEvent(this, aboveBlock, Block.get(Block.SNOW_LAYER, mergedHeight - 9)); // -8-1
this.server.getPluginManager().callEvent(event2);

if (!event2.isCancelled()) {
this.level.setBlock(abovePos, event2.getTo(), true);
}
Expand All @@ -180,6 +157,7 @@ public boolean onUpdate(int currentTick) {
} else {
EntityBlockChangeEvent event = new EntityBlockChangeEvent(this, floorBlock, Block.get(Block.SNOW_LAYER, mergedHeight - 1));
this.server.getPluginManager().callEvent(event);

if (!event.isCancelled()) {
this.level.setBlock(floorBlock, event.getTo(), true);
}
Expand All @@ -188,9 +166,10 @@ public boolean onUpdate(int currentTick) {
if (this.getBlock() != Block.SNOW_LAYER ? this.level.getGameRules().getBoolean(GameRule.DO_ENTITY_DROPS) : this.level.getGameRules().getBoolean(GameRule.DO_TILE_DROPS)) {
getLevel().dropItem(this, Item.get(this.blockId, this.damage, 1));
}
} else if (floorBlock.getId() == 0) {
} else if (floorBlock.canBeReplaced()) {
EntityBlockChangeEvent event = new EntityBlockChangeEvent(this, floorBlock, Block.get(blockId, damage));
server.getPluginManager().callEvent(event);

if (!event.isCancelled()) {
int blockId = event.getTo().getId();
if (blockId != Item.POINTED_DRIPSTONE) {
Expand Down
48 changes: 45 additions & 3 deletions src/main/java/cn/nukkit/item/ItemTrident.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockWater;
import cn.nukkit.entity.projectile.EntityProjectile;
import cn.nukkit.entity.projectile.EntityThrownTrident;
import cn.nukkit.event.entity.EntityShootBowEvent;
import cn.nukkit.event.entity.ProjectileLaunchEvent;
import cn.nukkit.event.player.PlayerToggleSpinAttackEvent;
import cn.nukkit.item.enchantment.Enchantment;
import cn.nukkit.math.Vector3;
import cn.nukkit.nbt.tag.CompoundTag;
Expand All @@ -32,12 +35,12 @@ public ItemTrident(Integer meta, int count) {
public int getMaxDurability() {
return ItemTool.DURABILITY_TRIDENT;
}

@Override
public boolean isSword() {
return true;
}

@Override
public int getAttackDamage() {
return 9;
Expand All @@ -50,7 +53,46 @@ public boolean onClickAir(Player player, Vector3 directionVector) {

@Override
public boolean onRelease(Player player, int ticksUsed) {
if (this.hasEnchantment(Enchantment.ID_TRIDENT_RIPTIDE)) {
Enchantment riptide = this.getEnchantment(Enchantment.ID_TRIDENT_RIPTIDE);
if (riptide != null) {
PlayerToggleSpinAttackEvent playerToggleSpinAttackEvent = new PlayerToggleSpinAttackEvent(player, true);

int riptideLevel = riptide.getLevel();
if (riptideLevel < 1) {
playerToggleSpinAttackEvent.setCancelled(true);
} else {
boolean inWater = false;
for (Block block : player.getCollisionBlocks()) {
if (block instanceof BlockWater) {
inWater = true;
break;
}
}
if (!(inWater || (player.getLevel().isRaining() && player.canSeeSky()))) {
playerToggleSpinAttackEvent.setCancelled(true);
}
}

player.getServer().getPluginManager().callEvent(playerToggleSpinAttackEvent);

if (playerToggleSpinAttackEvent.isCancelled()) {
player.setNeedSendData(true);
} else {
player.onSpinAttack(riptideLevel);
player.setSpinAttack(true);
player.resetFallDistance();

int riptideSound;
if (riptideLevel >= 3) {
riptideSound = LevelSoundEventPacket.SOUND_ITEM_TRIDENT_RIPTIDE_3;
} else if (riptideLevel == 2) {
riptideSound = LevelSoundEventPacket.SOUND_ITEM_TRIDENT_RIPTIDE_2;
} else {
riptideSound = LevelSoundEventPacket.SOUND_ITEM_TRIDENT_RIPTIDE_1;
}

player.getLevel().addLevelSoundEvent(player, riptideSound);
}
return true;
}

Expand Down

0 comments on commit 4ba1a10

Please sign in to comment.