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
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector.entity;
|
|
|
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
import org.geysermc.connector.entity.type.EntityType;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2020-05-25 01:07:05 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.ItemTranslator;
|
2021-07-22 01:48:59 +00:00
|
|
|
import org.geysermc.connector.network.translators.world.block.BlockStateValues;
|
2019-11-28 01:30:30 +00:00
|
|
|
|
2021-07-22 01:48:59 +00:00
|
|
|
public class ItemEntity extends ThrowableEntity {
|
2019-11-28 01:30:30 +00:00
|
|
|
|
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-07-22 01:48:59 +00:00
|
|
|
public ItemEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
|
|
|
|
super(entityId, geyserId, entityType, position, motion, rotation);
|
2019-11-28 01:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 23:23:01 +00:00
|
|
|
@Override
|
|
|
|
public void spawnEntity(GeyserSession session) {
|
|
|
|
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-06-16 23:23:01 +00:00
|
|
|
itemPacket.setPosition(position.add(0d, this.entityType.getOffset(), 0d));
|
|
|
|
itemPacket.setMotion(motion);
|
|
|
|
itemPacket.setFromFishing(false);
|
|
|
|
itemPacket.setItemInHand(item);
|
2021-07-22 01:48:59 +00:00
|
|
|
itemPacket.getMetadata().putAll(metadata);
|
2021-06-16 23:23:01 +00:00
|
|
|
session.sendUpstreamPacket(itemPacket);
|
|
|
|
}
|
|
|
|
|
2021-07-22 01:48:59 +00:00
|
|
|
@Override
|
|
|
|
public void tick(GeyserSession session) {
|
|
|
|
if (isInWater(session)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!onGround || (motion.getX() * motion.getX() + motion.getZ() * motion.getZ()) > 0.00001) {
|
|
|
|
float gravity = getGravity(session);
|
|
|
|
motion = motion.down(gravity);
|
|
|
|
moveAbsoluteImmediate(session, position.add(motion), rotation, onGround, false);
|
|
|
|
float drag = getDrag(session);
|
|
|
|
motion = motion.mul(drag, 0.98f, drag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void moveAbsoluteImmediate(GeyserSession session, Vector3f position, Vector3f rotation, boolean isOnGround, boolean teleported) {
|
|
|
|
float offset = entityType.getOffset();
|
|
|
|
if (waterLevel == 0) { // Item is in a full block of water
|
|
|
|
// Move the item entity down so it doesn't float above the water
|
|
|
|
offset = -entityType.getOffset();
|
|
|
|
}
|
|
|
|
super.moveAbsoluteImmediate(session, position.add(0, offset, 0), Vector3f.ZERO, isOnGround, teleported);
|
|
|
|
this.position = position;
|
|
|
|
|
|
|
|
int block = session.getConnector().getWorldManager().getBlockAt(session, position.toInt());
|
|
|
|
waterLevel = BlockStateValues.getWaterLevel(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected float getGravity(GeyserSession session) {
|
|
|
|
if (metadata.getFlags().getFlag(EntityFlag.HAS_GRAVITY) && !onGround && !isInWater(session)) {
|
|
|
|
// 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
|
|
|
|
protected float getDrag(GeyserSession session) {
|
|
|
|
if (onGround) {
|
|
|
|
Vector3i groundBlockPos = position.toInt().down(1);
|
|
|
|
int blockState = session.getConnector().getWorldManager().getBlockAt(session, groundBlockPos);
|
|
|
|
return BlockStateValues.getSlipperiness(blockState) * 0.98f;
|
|
|
|
}
|
|
|
|
return 0.98f;
|
|
|
|
}
|
|
|
|
|
2019-11-28 01:30:30 +00:00
|
|
|
@Override
|
|
|
|
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
|
2021-05-20 02:24:11 +00:00
|
|
|
if (entityMetadata.getId() == 8) {
|
2021-06-16 23:23:01 +00:00
|
|
|
item = ItemTranslator.translateToBedrock(session, (ItemStack) entityMetadata.getValue());
|
|
|
|
despawnEntity(session);
|
|
|
|
spawnEntity(session);
|
2019-11-28 01:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
super.updateBedrockMetadata(entityMetadata, session);
|
|
|
|
}
|
2021-07-22 01:48:59 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean isInWater(GeyserSession session) {
|
|
|
|
return waterLevel != -1;
|
|
|
|
}
|
2019-11-28 01:30:30 +00:00
|
|
|
}
|