2019-11-28 01:30:30 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2019-11-28 01:30:30 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.entity.type;
|
2019-11-28 01:30:30 +00:00
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
|
|
|
|
import com.nukkitx.math.vector.Vector3f;
|
2021-07-22 01:48:59 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3i;
|
2021-07-22 13:16:19 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityEventType;
|
2021-07-22 01:48:59 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
|
2021-06-16 23:23:01 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
|
2019-11-28 01:30:30 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.AddItemEntityPacket;
|
2021-07-22 13:16:19 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.EntityEventPacket;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.entity.EntityDefinition;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.translator.inventory.item.ItemTranslator;
|
|
|
|
import org.geysermc.geyser.level.block.BlockStateValues;
|
2019-11-28 01:30:30 +00:00
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2021-07-22 01:48:59 +00:00
|
|
|
public class ItemEntity extends ThrowableEntity {
|
2021-06-16 23:23:01 +00:00
|
|
|
protected ItemData item;
|
|
|
|
|
2021-07-22 01:48:59 +00:00
|
|
|
private int waterLevel = -1;
|
2020-12-09 20:09:14 +00:00
|
|
|
|
2021-12-21 00:25:11 +00:00
|
|
|
public ItemEntity(GeyserSession session, int entityId, long geyserId, UUID uuid, EntityDefinition<?> definition, Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
|
2021-11-18 03:02:38 +00:00
|
|
|
super(session, entityId, geyserId, uuid, definition, position, motion, yaw, pitch, headYaw);
|
2019-11-28 01:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 23:23:01 +00:00
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
public void spawnEntity() {
|
2021-06-16 23:23:01 +00:00
|
|
|
if (item == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
valid = true;
|
|
|
|
AddItemEntityPacket itemPacket = new AddItemEntityPacket();
|
|
|
|
itemPacket.setRuntimeEntityId(geyserId);
|
2021-07-22 01:48:59 +00:00
|
|
|
itemPacket.setUniqueEntityId(geyserId);
|
2021-11-18 03:02:38 +00:00
|
|
|
itemPacket.setPosition(position.add(0d, this.definition.offset(), 0d));
|
2021-06-16 23:23:01 +00:00
|
|
|
itemPacket.setMotion(motion);
|
|
|
|
itemPacket.setFromFishing(false);
|
|
|
|
itemPacket.setItemInHand(item);
|
2021-11-19 01:44:03 +00:00
|
|
|
itemPacket.getMetadata().putFlags(this.flags);
|
|
|
|
dirtyMetadata.apply(itemPacket.getMetadata());
|
|
|
|
|
|
|
|
setFlagsDirty(false);
|
|
|
|
|
2021-06-16 23:23:01 +00:00
|
|
|
session.sendUpstreamPacket(itemPacket);
|
|
|
|
}
|
|
|
|
|
2021-07-22 01:48:59 +00:00
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
public void tick() {
|
|
|
|
if (isInWater()) {
|
2021-07-22 01:48:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!onGround || (motion.getX() * motion.getX() + motion.getZ() * motion.getZ()) > 0.00001) {
|
2021-11-18 03:02:38 +00:00
|
|
|
float gravity = getGravity();
|
2021-07-22 01:48:59 +00:00
|
|
|
motion = motion.down(gravity);
|
2021-11-18 03:02:38 +00:00
|
|
|
moveAbsoluteImmediate(position.add(motion), yaw, pitch, headYaw, onGround, false);
|
|
|
|
float drag = getDrag();
|
2021-07-22 01:48:59 +00:00
|
|
|
motion = motion.mul(drag, 0.98f, drag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 19:25:21 +00:00
|
|
|
public void setItem(EntityMetadata<ItemStack, ?> entityMetadata) {
|
2021-11-18 03:02:38 +00:00
|
|
|
ItemData item = ItemTranslator.translateToBedrock(session, entityMetadata.getValue());
|
|
|
|
if (this.item == null) {
|
|
|
|
this.item = item;
|
|
|
|
spawnEntity();
|
|
|
|
} else if (item.equals(this.item, false, true, true)) {
|
|
|
|
// Don't bother respawning the entity if items are equal
|
|
|
|
if (this.item.getCount() != item.getCount()) {
|
|
|
|
// Just item count updated; let's make this easy
|
|
|
|
this.item = item;
|
|
|
|
EntityEventPacket packet = new EntityEventPacket();
|
|
|
|
packet.setRuntimeEntityId(geyserId);
|
|
|
|
packet.setType(EntityEventType.UPDATE_ITEM_STACK_SIZE);
|
|
|
|
packet.setData(this.item.getCount());
|
|
|
|
session.sendUpstreamPacket(packet);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.item = item;
|
|
|
|
despawnEntity();
|
|
|
|
spawnEntity();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 01:48:59 +00:00
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
protected void moveAbsoluteImmediate(Vector3f position, float yaw, float pitch, float headYaw, boolean isOnGround, boolean teleported) {
|
|
|
|
float offset = definition.offset();
|
2021-07-22 01:48:59 +00:00
|
|
|
if (waterLevel == 0) { // Item is in a full block of water
|
|
|
|
// Move the item entity down so it doesn't float above the water
|
2021-11-18 03:02:38 +00:00
|
|
|
offset = -definition.offset();
|
2021-07-22 01:48:59 +00:00
|
|
|
}
|
2021-11-18 03:02:38 +00:00
|
|
|
super.moveAbsoluteImmediate(position.add(0, offset, 0), 0, 0, 0, isOnGround, teleported);
|
2021-07-22 01:48:59 +00:00
|
|
|
this.position = position;
|
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
int block = session.getGeyser().getWorldManager().getBlockAt(session, position.toInt());
|
2021-07-22 01:48:59 +00:00
|
|
|
waterLevel = BlockStateValues.getWaterLevel(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
protected float getGravity() {
|
|
|
|
if (getFlag(EntityFlag.HAS_GRAVITY) && !onGround && !isInWater()) {
|
2021-07-22 01:48:59 +00:00
|
|
|
// Gravity can change if the item is in water/lava, but
|
|
|
|
// the server calculates the motion & position for us
|
|
|
|
return 0.04f;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
protected float getDrag() {
|
2021-07-22 01:48:59 +00:00
|
|
|
if (onGround) {
|
|
|
|
Vector3i groundBlockPos = position.toInt().down(1);
|
2021-11-20 21:34:30 +00:00
|
|
|
int blockState = session.getGeyser().getWorldManager().getBlockAt(session, groundBlockPos);
|
2021-07-22 01:48:59 +00:00
|
|
|
return BlockStateValues.getSlipperiness(blockState) * 0.98f;
|
|
|
|
}
|
|
|
|
return 0.98f;
|
|
|
|
}
|
|
|
|
|
2019-11-28 01:30:30 +00:00
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
protected boolean isInWater() {
|
2021-07-22 01:48:59 +00:00
|
|
|
return waterLevel != -1;
|
|
|
|
}
|
2019-11-28 01:30:30 +00:00
|
|
|
}
|