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

Implement basic entity pick #2020

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
134 changes: 88 additions & 46 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -2755,59 +2755,40 @@ public void onCompletion(Server server) {

PlayerBlockPickEvent pickEvent = new PlayerBlockPickEvent(this, block, item);
if (this.isSpectator()) {
log.debug("Got block-pick request from " + this.getName() + " when in spectator mode");
pickEvent.setCancelled();
}

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

if (!pickEvent.isCancelled()) {
boolean itemExists = false;
int itemSlot = -1;
for (int slot = 0; slot < this.inventory.getSize(); slot++) {
if (this.inventory.getItem(slot).equals(pickEvent.getItem())) {
if (slot < this.inventory.getHotbarSize()) {
this.inventory.setHeldItemSlot(slot);
} else {
itemSlot = slot;
}
itemExists = true;
break;
}
}
this.pickItem(pickEvent.getItem());
}
break;
case ProtocolInfo.ENTITY_PICK_REQUEST_PACKET:
EntityPickRequestPacket entityPickRequestPacket = (EntityPickRequestPacket) packet;
targetEntity = this.level.getEntity(entityPickRequestPacket.entityId);
if (!(targetEntity instanceof EntityPickable)) {
break;
}

for (int slot = 0; slot < this.inventory.getHotbarSize(); slot++) {
if (this.inventory.getItem(slot).isNull()) {
if (!itemExists && this.isCreative()) {
this.inventory.setHeldItemSlot(slot);
this.inventory.setItemInHand(pickEvent.getItem());
break packetswitch;
} else if (itemSlot > -1) {
this.inventory.setHeldItemSlot(slot);
this.inventory.setItemInHand(this.inventory.getItem(itemSlot));
this.inventory.clear(itemSlot, true);
break packetswitch;
}
}
}
if (this.distanceSquared(targetEntity) > 1000) {
this.getServer().getLogger().debug(this.getName() + ": Entity pick request for a entity too far away");
break;
}

if (!itemExists && this.isCreative()) {
Item itemInHand = this.inventory.getItemInHand();
this.inventory.setItemInHand(pickEvent.getItem());
if (!this.inventory.isFull()) {
for (int slot = 0; slot < this.inventory.getSize(); slot++) {
if (this.inventory.getItem(slot).isNull()) {
this.inventory.setItem(slot, itemInHand);
break;
}
}
}
} else if (itemSlot > -1) {
Item itemInHand = this.inventory.getItemInHand();
this.inventory.setItemInHand(this.inventory.getItem(itemSlot));
this.inventory.setItem(itemSlot, itemInHand);
}
item = ((EntityPickable) targetEntity).toItem();
if (item == null || item.isNull()) {
break;
}

PlayerEntityPickEvent entityPickEvent = new PlayerEntityPickEvent(this, targetEntity, item);
if (this.isSpectator()) {
entityPickEvent.setCancelled();
}
this.server.getPluginManager().callEvent(entityPickEvent);
if (entityPickEvent.isCancelled()) {
break;
}

this.pickItem(entityPickEvent.getItem());
break;
case ProtocolInfo.ANIMATE_PACKET:
if (!this.spawned || !this.isAlive()) {
Expand Down Expand Up @@ -5324,4 +5305,65 @@ public int getTimeSinceRest() {
public void setTimeSinceRest(int timeSinceRest) {
this.timeSinceRest = timeSinceRest;
}

protected void pickItem(Item item) {
if (item == null || item.isNull()) {
return;
}

int existingSlot = -1;
for (int slot = 0; slot < this.inventory.getSize(); slot++) {
if (!this.inventory.getItem(slot).equals(item)) {
continue;
}

if (slot < this.inventory.getHotbarSize()) {
this.inventory.setHeldItemSlot(slot);
return;
}

existingSlot = slot;
break;
}

if (existingSlot == -1 && !this.isCreative()) {
return;
}

for (int slot = 0; slot < this.inventory.getHotbarSize(); slot++) {
if (!this.inventory.getItem(slot).isNull()) {
continue;
}

this.inventory.setHeldItemSlot(slot);
if (existingSlot == -1) {
this.inventory.setItemInHand(item);
} else {
this.inventory.setItemInHand(this.inventory.getItem(existingSlot));
this.inventory.clear(existingSlot, true);
}
return;
}

Item itemInHand = this.inventory.getItemInHand();
if (existingSlot != -1) {
this.inventory.setItemInHand(this.inventory.getItem(existingSlot));
this.inventory.setItem(existingSlot, itemInHand);
return;
}

this.inventory.setItemInHand(item);
if (this.inventory.isFull()) {
return;
}

for (int slot = 0; slot < this.inventory.getSize(); slot++) {
if (!this.inventory.getItem(slot).isNull()) {
continue;
}

this.inventory.setItem(slot, itemInHand);
break;
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/cn/nukkit/entity/EntityPickable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cn.nukkit.entity;

import cn.nukkit.item.Item;

public interface EntityPickable {

Item toItem();
}
8 changes: 7 additions & 1 deletion src/main/java/cn/nukkit/entity/item/EntityBoat.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cn.nukkit.block.BlockWater;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.EntityLiving;
import cn.nukkit.entity.EntityPickable;
import cn.nukkit.entity.data.ByteEntityData;
import cn.nukkit.entity.data.FloatEntityData;
import cn.nukkit.entity.passive.EntityWaterAnimal;
Expand All @@ -29,7 +30,7 @@
/**
* Created by yescallop on 2016/2/13.
*/
public class EntityBoat extends EntityVehicle {
public class EntityBoat extends EntityVehicle implements EntityPickable {

public static final int NETWORK_ID = 90;

Expand Down Expand Up @@ -433,4 +434,9 @@ public boolean isFull() {
public String getInteractButtonText() {
return !this.isFull() ? "action.interact.ride.boat" : "";
}

@Override
public Item toItem() {
return Item.get(Item.BOAT, this.woodID);
}
}
9 changes: 8 additions & 1 deletion src/main/java/cn/nukkit/entity/item/EntityEndCrystal.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import cn.nukkit.entity.Entity;
import cn.nukkit.entity.EntityExplosive;
import cn.nukkit.entity.EntityPickable;
import cn.nukkit.event.entity.EntityDamageEvent;
import cn.nukkit.item.Item;
import cn.nukkit.level.Explosion;
import cn.nukkit.level.GameRule;
import cn.nukkit.level.Position;
Expand All @@ -12,7 +14,7 @@
/**
* Created by PetteriM1
*/
public class EntityEndCrystal extends Entity implements EntityExplosive {
public class EntityEndCrystal extends Entity implements EntityExplosive, EntityPickable {

public static final int NETWORK_ID = 71;

Expand Down Expand Up @@ -93,6 +95,11 @@ public boolean canCollideWith(Entity entity) {
return false;
}

@Override
public Item toItem() {
return Item.get(Item.END_CRYSTAL);
}

public boolean showBase() {
return this.getDataFlag(DATA_FLAGS, DATA_FLAG_SHOWBASE);
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/cn/nukkit/entity/item/EntityMinecartChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.nukkit.Player;
import cn.nukkit.block.Block;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.EntityPickable;
import cn.nukkit.inventory.InventoryHolder;
import cn.nukkit.inventory.MinecartChestInventory;
import cn.nukkit.item.Item;
Expand All @@ -17,7 +18,7 @@
* Created by Snake1999 on 2016/1/30.
* Package cn.nukkit.entity.item in project Nukkit.
*/
public class EntityMinecartChest extends EntityMinecartAbstract implements InventoryHolder {
public class EntityMinecartChest extends EntityMinecartAbstract implements InventoryHolder, EntityPickable {

public static final int NETWORK_ID = 98;

Expand Down Expand Up @@ -113,4 +114,9 @@ public void saveNBT() {
public String getInteractButtonText() {
return "action.interact.opencontainer";
}

@Override
public Item toItem() {
return Item.get(Item.MINECART_WITH_CHEST);
}
}
9 changes: 8 additions & 1 deletion src/main/java/cn/nukkit/entity/item/EntityMinecartEmpty.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import cn.nukkit.Player;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.EntityLiving;
import cn.nukkit.entity.EntityPickable;
import cn.nukkit.entity.passive.EntityWaterAnimal;
import cn.nukkit.event.entity.EntityDamageByBlockEvent;
import cn.nukkit.event.entity.EntityDamageEvent.DamageCause;
import cn.nukkit.item.Item;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.utils.MinecartType;
Expand All @@ -14,7 +16,7 @@
* Created by Snake1999 on 2016/1/30.
* Package cn.nukkit.entity.item in project Nukkit.
*/
public class EntityMinecartEmpty extends EntityMinecartAbstract {
public class EntityMinecartEmpty extends EntityMinecartAbstract implements EntityPickable {

public static final int NETWORK_ID = 84;

Expand Down Expand Up @@ -74,4 +76,9 @@ public boolean onUpdate(int currentTick) {
public String getInteractButtonText() {
return this.passengers.isEmpty() ? "action.interact.ride.minecart" : "";
}

@Override
public Item toItem() {
return Item.get(Item.MINECART);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.nukkit.Player;
import cn.nukkit.block.Block;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.EntityPickable;
import cn.nukkit.inventory.InventoryHolder;
import cn.nukkit.inventory.MinecartHopperInventory;
import cn.nukkit.item.Item;
Expand All @@ -13,7 +14,7 @@
import cn.nukkit.nbt.tag.ListTag;
import cn.nukkit.utils.MinecartType;

public class EntityMinecartHopper extends EntityMinecartAbstract implements InventoryHolder {
public class EntityMinecartHopper extends EntityMinecartAbstract implements InventoryHolder, EntityPickable {

public static final int NETWORK_ID = 96;

Expand Down Expand Up @@ -109,4 +110,9 @@ public void saveNBT() {
public String getInteractButtonText() {
return "action.interact.opencontainer";
}

@Override
public Item toItem() {
return Item.get(Item.MINECART_WITH_HOPPER);
}
}
8 changes: 7 additions & 1 deletion src/main/java/cn/nukkit/entity/item/EntityMinecartTNT.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cn.nukkit.block.BlockID;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.EntityExplosive;
import cn.nukkit.entity.EntityPickable;
import cn.nukkit.entity.data.IntEntityData;
import cn.nukkit.event.entity.EntityExplosionPrimeEvent;
import cn.nukkit.item.Item;
Expand All @@ -24,7 +25,7 @@
* <p>
* Nukkit Project.
*/
public class EntityMinecartTNT extends EntityMinecartAbstract implements EntityExplosive {
public class EntityMinecartTNT extends EntityMinecartAbstract implements EntityExplosive, EntityPickable {

public static final int NETWORK_ID = 97;
private int fuse;
Expand Down Expand Up @@ -156,6 +157,11 @@ public boolean onInteract(Player player, Item item, Vector3 clickedPos) {
return interact;
}

@Override
public Item toItem() {
return Item.get(Item.MINECART_WITH_TNT);
}

@Override
public boolean mountEntity(Entity entity, byte mode) {
return false;
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/cn/nukkit/entity/item/EntityPainting.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import cn.nukkit.Player;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.EntityHanging;
import cn.nukkit.entity.EntityPickable;
import cn.nukkit.event.entity.EntityDamageByEntityEvent;
import cn.nukkit.event.entity.EntityDamageEvent;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemPainting;
import cn.nukkit.level.GameRule;
import cn.nukkit.level.format.FullChunk;
Expand All @@ -19,7 +21,7 @@
* author: MagicDroidX
* Nukkit Project
*/
public class EntityPainting extends EntityHanging {
public class EntityPainting extends EntityHanging implements EntityPickable {

public static final int NETWORK_ID = 83;

Expand Down Expand Up @@ -88,6 +90,11 @@ public Motive getMotive() {
return Motive.BY_NAME.get(namedTag.getString("Motive"));
}

@Override
public Item toItem() {
return Item.get(Item.PAINTING);
}

public enum Motive {
KEBAB("Kebab", 1, 1),
AZTEC("Aztec", 1, 1),
Expand Down
Loading