Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for trident #1893

Merged
merged 2 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -4941,14 +4941,14 @@ public boolean pickupEntity(Entity entity, boolean near) {
if (near) {
if (entity instanceof EntityArrow && ((EntityArrow) entity).hadCollision) {
ItemArrow item = new ItemArrow();
if (this.isSurvival() && !this.inventory.canAddItem(item)) {
if (!this.isCreative() && !this.inventory.canAddItem(item)) {
return false;
}

InventoryPickupArrowEvent ev = new InventoryPickupArrowEvent(this.inventory, (EntityArrow) entity);

int pickupMode = ((EntityArrow) entity).getPickupMode();
if (pickupMode == EntityArrow.PICKUP_NONE || pickupMode == EntityArrow.PICKUP_CREATIVE && !this.isCreative()) {
if (pickupMode == EntityArrow.PICKUP_NONE || (pickupMode == EntityArrow.PICKUP_CREATIVE && !this.isCreative())) {
ev.setCancelled();
}

Expand All @@ -4970,11 +4970,17 @@ public boolean pickupEntity(Entity entity, boolean near) {
return true;
} else if (entity instanceof EntityThrownTrident && ((EntityThrownTrident) entity).hadCollision) {
Item item = ((EntityThrownTrident) entity).getItem();
if (this.isSurvival() && !this.inventory.canAddItem(item)) {
if (!this.isCreative() && !this.inventory.canAddItem(item)) {
return false;
}

InventoryPickupTridentEvent ev = new InventoryPickupTridentEvent(this.inventory, (EntityThrownTrident) entity);

int pickupMode = ((EntityThrownTrident) entity).getPickupMode();
if (pickupMode == EntityThrownTrident.PICKUP_NONE || (pickupMode == EntityThrownTrident.PICKUP_CREATIVE && !this.isCreative())) {
ev.setCancelled();
}

this.server.getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
return false;
Expand All @@ -4996,7 +5002,7 @@ public boolean pickupEntity(Entity entity, boolean near) {
Item item = ((EntityItem) entity).getItem();

if (item != null) {
if (this.isSurvival() && !this.inventory.canAddItem(item)) {
if (!this.isCreative() && !this.inventory.canAddItem(item)) {
return false;
}

Expand All @@ -5022,8 +5028,8 @@ public boolean pickupEntity(Entity entity, boolean near) {
Server.broadcastPacket(entity.getViewers().values(), pk);
this.dataPacket(pk);

entity.close();
this.inventory.addItem(item.clone());
entity.close();
return true;
}
}
Expand Down Expand Up @@ -5056,8 +5062,9 @@ public boolean pickupEntity(Entity entity, boolean near) {
if (toRepair instanceof ItemTool || toRepair instanceof ItemArmor) {
if (toRepair.getDamage() > 0) {
int dmg = toRepair.getDamage() - 2;
if (dmg < 0)
if (dmg < 0) {
dmg = 0;
}
toRepair.setDamage(dmg);
inventory.setItem(itemToRepair, toRepair);
return true;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/cn/nukkit/entity/item/EntityItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
* @author MagicDroidX
*/
public class EntityItem extends Entity {
public static final int NETWORK_ID = 64;

public static final int DATA_SOURCE_ID = 17;
public static final int NETWORK_ID = 64;

public EntityItem(FullChunk chunk, CompoundTag nbt) {
super(chunk, nbt);
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/cn/nukkit/entity/projectile/EntityArrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@
* Nukkit Project
*/
public class EntityArrow extends EntityProjectile {
public static final int NETWORK_ID = 80;

public static final int DATA_SOURCE_ID = 17;

public static final int PICKUP_NONE = 0;
public static final int PICKUP_ANY = 1;
public static final int PICKUP_CREATIVE = 2;
public static final int NETWORK_ID = 80;

protected int pickupMode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ protected double getBaseDamage() {

protected double damage = 0;

public static final int PICKUP_NONE = 0;
public static final int PICKUP_ANY = 1;
public static final int PICKUP_CREATIVE = 2;

public EntityProjectile(FullChunk chunk, CompoundTag nbt) {
this(chunk, nbt, null);
}
Expand Down
135 changes: 40 additions & 95 deletions src/main/java/cn/nukkit/entity/projectile/EntityThrownTrident.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
package cn.nukkit.entity.projectile;

import cn.nukkit.Player;
import cn.nukkit.entity.Entity;
import cn.nukkit.event.entity.EntityDamageEvent;
import cn.nukkit.event.entity.EntityDamageEvent.DamageCause;
import cn.nukkit.entity.weather.EntityLightning;
import cn.nukkit.event.entity.EntityDamageByChildEntityEvent;
import cn.nukkit.event.entity.EntityDamageByEntityEvent;
import cn.nukkit.event.entity.EntityDamageEvent;
import cn.nukkit.event.entity.EntityDamageEvent.DamageCause;
import cn.nukkit.event.entity.ProjectileHitEvent;
import cn.nukkit.level.MovingObjectPosition;
import cn.nukkit.level.Position;
import cn.nukkit.event.weather.LightningStrikeEvent;
import cn.nukkit.item.Item;
import cn.nukkit.item.enchantment.Enchantment;
import cn.nukkit.level.MovingObjectPosition;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.math.Vector3;
import cn.nukkit.nbt.NBTIO;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.network.protocol.AddEntityPacket;
import cn.nukkit.network.protocol.LevelSoundEventPacket;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
* Created by PetteriM1
*/
public class EntityThrownTrident extends EntityProjectile {

public static final int NETWORK_ID = 73;

public static final int DATA_SOURCE_ID = 17;

protected Item trident;

protected int pickupMode;
public boolean alreadyCollided;

@Override
public int getNetworkId() {
return NETWORK_ID;
Expand All @@ -53,44 +50,41 @@ public float getHeight() {

@Override
public float getGravity() {
return 0.04f;
return 0.05f;
}

@Override
public float getDrag() {
return 0.01f;
}

protected float gravity = 0.04f;
protected float drag = 0.01f;

public EntityThrownTrident(FullChunk chunk, CompoundTag nbt) {
this(chunk, nbt, null);
}

public EntityThrownTrident(FullChunk chunk, CompoundTag nbt, Entity shootingEntity) {
this(chunk, nbt, shootingEntity, false);
super(chunk, nbt, shootingEntity);
}

@Deprecated
public EntityThrownTrident(FullChunk chunk, CompoundTag nbt, Entity shootingEntity, boolean critical) {
super(chunk, nbt, shootingEntity);
this(chunk, nbt, shootingEntity);
}

@Override
protected void initEntity() {
super.initEntity();

this.damage = namedTag.contains("damage") ? namedTag.getDouble("damage") : 8;
this.trident = namedTag.contains("Trident") ? NBTIO.getItemHelper(namedTag.getCompound("Trident")) : Item.get(0);

closeOnCollide = false;
this.pickupMode = namedTag.contains("pickup") ? namedTag.getByte("pickup") : PICKUP_ANY;
}

@Override
public void saveNBT() {
super.saveNBT();

this.namedTag.put("Trident", NBTIO.putItemHelper(this.trident));
this.namedTag.putByte("pickup", this.pickupMode);
}

public Item getItem() {
Expand All @@ -101,79 +95,18 @@ public void setItem(Item item) {
this.trident = item.clone();
}

public void setCritical() {
this.setCritical(true);
}

public void setCritical(boolean value) {
this.setDataFlag(DATA_FLAGS, DATA_FLAG_CRITICAL, value);
}

public boolean isCritical() {
return this.getDataFlag(DATA_FLAGS, DATA_FLAG_CRITICAL);
}

@Override
public int getResultDamage() {
int base = super.getResultDamage();

if (this.isCritical()) {
base += ThreadLocalRandom.current().nextInt(base / 2 + 2);
}

return base;
}

@Override
protected double getBaseDamage() {
return 8;
}

@Override
public boolean onUpdate(int currentTick) {
if (this.closed) {
return false;
}

this.timing.startTiming();

if (this.isCollided && !this.hadCollision) {
this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_ITEM_TRIDENT_HIT_GROUND);
}

boolean hasUpdate = super.onUpdate(currentTick);

if (this.onGround || this.hadCollision) {
this.setCritical(false);
public void onCollideWithEntity(Entity entity) {
if (this.alreadyCollided) {
this.move(this.motionX, this.motionY, this.motionZ);
return;
}

this.timing.stopTiming();

return hasUpdate;
}

@Override
public void spawnTo(Player player) {
AddEntityPacket pk = new AddEntityPacket();
pk.type = NETWORK_ID;
pk.entityUniqueId = this.getId();
pk.entityRuntimeId = this.getId();
pk.x = (float) this.x;
pk.y = (float) this.y;
pk.z = (float) this.z;
pk.speedX = (float) this.motionX;
pk.speedY = (float) this.motionY;
pk.speedZ = (float) this.motionZ;
pk.yaw = (float) this.yaw;
pk.pitch = (float) this.pitch;
pk.metadata = this.dataProperties;
player.dataPacket(pk);

super.spawnTo(player);
}

@Override
public void onCollideWithEntity(Entity entity) {
this.server.getPluginManager().callEvent(new ProjectileHitEvent(this, MovingObjectPosition.fromEntity(entity)));
float damage = this.getResultDamage();

Expand All @@ -184,20 +117,32 @@ public void onCollideWithEntity(Entity entity) {
ev = new EntityDamageByChildEntityEvent(this.shootingEntity, this, entity, DamageCause.PROJECTILE, damage);
}
entity.attack(ev);
this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_ITEM_TRIDENT_HIT);
this.hadCollision = true;
this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_ITEM_TRIDENT_HIT);
this.close();
Entity newTrident = create("ThrownTrident", this);
((EntityThrownTrident) newTrident).setItem(this.trident);
if (trident != null && level.isThundering() && trident.hasEnchantment(Enchantment.ID_TRIDENT_CHANNELING) && level.canBlockSeeSky(this)) {
EntityLightning bolt = new EntityLightning(this.getChunk(), getDefaultNBT(this));
LightningStrikeEvent strikeEvent = new LightningStrikeEvent(level, bolt);
server.getPluginManager().callEvent(strikeEvent);
if (!strikeEvent.isCancelled()) {
bolt.spawnToAll();
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_ITEM_TRIDENT_THUNDER);
} else {
bolt.setEffect(false);
}
}
EntityThrownTrident newTrident = (EntityThrownTrident) Entity.createEntity("ThrownTrident", this);
newTrident.alreadyCollided = true;
newTrident.pickupMode = this.pickupMode;
newTrident.setItem(this.trident);
newTrident.spawnToAll();
}

public Entity create(Object type, Position source, Object... args) {
FullChunk chunk = source.getLevel().getChunk((int) source.x >> 4, (int) source.z >> 4);
if (chunk == null) return null;

CompoundTag nbt = Entity.getDefaultNBT(source.add(0.5, 0, 0.5), new Vector3(), new Random().nextFloat() * 360, 0);
public int getPickupMode() {
return this.pickupMode;
}

return Entity.createEntity(type.toString(), chunk, nbt, args);
public void setPickupMode(int pickupMode) {
this.pickupMode = pickupMode;
}
}
Loading