2020-05-02 20:44:05 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-05-02 20:44:05 +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;
|
2020-05-23 20:56:49 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.object.HangingDirection;
|
2020-05-02 20:44:05 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3f;
|
|
|
|
import com.nukkitx.math.vector.Vector3i;
|
2020-07-05 20:58:43 +00:00
|
|
|
import com.nukkitx.nbt.NbtMap;
|
|
|
|
import com.nukkitx.nbt.NbtMapBuilder;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
|
2020-05-02 20:44:05 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
|
2021-10-18 00:24:27 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.v465.Bedrock_v465;
|
2021-05-09 19:44:41 +00:00
|
|
|
import lombok.Getter;
|
2020-05-02 20:44:05 +00:00
|
|
|
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-13 01:19:40 +00:00
|
|
|
import org.geysermc.connector.registry.type.ItemMapping;
|
2020-05-02 20:44:05 +00:00
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Item frames are an entity in Java but a block entity in Bedrock.
|
|
|
|
*/
|
|
|
|
public class ItemFrameEntity extends Entity {
|
|
|
|
|
2021-03-09 17:51:48 +00:00
|
|
|
/**
|
|
|
|
* Used to construct the block entity tag on spawning.
|
|
|
|
*/
|
|
|
|
private final HangingDirection direction;
|
2020-05-02 20:44:05 +00:00
|
|
|
/**
|
|
|
|
* Used for getting the Bedrock block position.
|
|
|
|
* Blocks deal with integers whereas entities deal with floats.
|
|
|
|
*/
|
2021-03-09 17:51:48 +00:00
|
|
|
private Vector3i bedrockPosition;
|
2020-05-02 20:44:05 +00:00
|
|
|
/**
|
|
|
|
* Specific block 'state' we are emulating in Bedrock.
|
|
|
|
*/
|
2021-03-09 17:51:48 +00:00
|
|
|
private int bedrockRuntimeId;
|
2020-05-02 20:44:05 +00:00
|
|
|
/**
|
|
|
|
* Rotation of item in frame.
|
|
|
|
*/
|
|
|
|
private float rotation = 0.0f;
|
|
|
|
/**
|
|
|
|
* Cached item frame's Bedrock compound tag.
|
|
|
|
*/
|
2020-07-05 20:58:43 +00:00
|
|
|
private NbtMap cachedTag;
|
2021-05-09 19:44:41 +00:00
|
|
|
/**
|
|
|
|
* The item currently in the item frame. Used for block picking.
|
|
|
|
*/
|
|
|
|
@Getter
|
|
|
|
private ItemStack heldItem = null;
|
2020-05-02 20:44:05 +00:00
|
|
|
|
|
|
|
public ItemFrameEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation, HangingDirection direction) {
|
|
|
|
super(entityId, geyserId, entityType, position, motion, rotation);
|
2021-03-09 17:51:48 +00:00
|
|
|
this.direction = direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void spawnEntity(GeyserSession session) {
|
2020-07-05 20:58:43 +00:00
|
|
|
NbtMapBuilder blockBuilder = NbtMap.builder()
|
2021-06-03 23:04:28 +00:00
|
|
|
.putString("name", this.entityType == EntityType.GLOW_ITEM_FRAME ? "minecraft:glow_frame" : "minecraft:frame")
|
2021-07-13 01:19:40 +00:00
|
|
|
.putInt("version", session.getBlockMappings().getBlockStateVersion());
|
2021-09-21 20:50:06 +00:00
|
|
|
NbtMapBuilder statesBuilder = NbtMap.builder()
|
2020-07-05 20:58:43 +00:00
|
|
|
.putInt("facing_direction", direction.ordinal())
|
2021-09-21 20:50:06 +00:00
|
|
|
.putByte("item_frame_map_bit", (byte) 0);
|
|
|
|
if (session.getUpstream().getProtocolVersion() >= Bedrock_v465.V465_CODEC.getProtocolVersion()) {
|
|
|
|
statesBuilder.putByte("item_frame_photo_bit", (byte) 0);
|
|
|
|
}
|
|
|
|
blockBuilder.put("states", statesBuilder.build());
|
|
|
|
|
2021-07-13 01:19:40 +00:00
|
|
|
bedrockRuntimeId = session.getBlockMappings().getItemFrame(blockBuilder.build());
|
2020-05-02 20:44:05 +00:00
|
|
|
bedrockPosition = Vector3i.from(position.getFloorX(), position.getFloorY(), position.getFloorZ());
|
|
|
|
|
2021-05-09 19:44:41 +00:00
|
|
|
session.getItemFrameCache().put(bedrockPosition, this);
|
|
|
|
|
2020-05-24 04:14:29 +00:00
|
|
|
// Delay is required, or else loading in frames on chunk load is sketchy at best
|
|
|
|
session.getConnector().getGeneralThreadPool().schedule(() -> {
|
|
|
|
updateBlock(session);
|
|
|
|
session.getConnector().getLogger().debug("Spawned item frame at location " + bedrockPosition + " with java id " + entityId);
|
|
|
|
}, 500, TimeUnit.MILLISECONDS);
|
2020-05-02 20:44:05 +00:00
|
|
|
valid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
|
2021-05-20 02:24:11 +00:00
|
|
|
if (entityMetadata.getId() == 8 && entityMetadata.getValue() != null) {
|
2021-05-09 19:44:41 +00:00
|
|
|
this.heldItem = (ItemStack) entityMetadata.getValue();
|
|
|
|
ItemData itemData = ItemTranslator.translateToBedrock(session, heldItem);
|
2021-07-13 01:19:40 +00:00
|
|
|
ItemMapping mapping = session.getItemMappings().getMapping((ItemStack) entityMetadata.getValue());
|
2020-07-05 20:58:43 +00:00
|
|
|
NbtMapBuilder builder = NbtMap.builder();
|
2020-05-02 20:44:05 +00:00
|
|
|
|
2020-07-05 20:58:43 +00:00
|
|
|
builder.putByte("Count", (byte) itemData.getCount());
|
2020-05-02 20:44:05 +00:00
|
|
|
if (itemData.getTag() != null) {
|
2021-05-09 19:44:41 +00:00
|
|
|
builder.put("tag", itemData.getTag());
|
2020-05-02 20:44:05 +00:00
|
|
|
}
|
2021-04-06 04:14:06 +00:00
|
|
|
builder.putShort("Damage", (short) itemData.getDamage());
|
2021-07-13 01:19:40 +00:00
|
|
|
builder.putString("Name", mapping.getBedrockIdentifier());
|
2020-07-05 20:58:43 +00:00
|
|
|
NbtMapBuilder tag = getDefaultTag().toBuilder();
|
|
|
|
tag.put("Item", builder.build());
|
|
|
|
tag.putFloat("ItemDropChance", 1.0f);
|
|
|
|
tag.putFloat("ItemRotation", rotation);
|
|
|
|
cachedTag = tag.build();
|
2020-05-02 20:44:05 +00:00
|
|
|
updateBlock(session);
|
|
|
|
}
|
2021-05-20 02:24:11 +00:00
|
|
|
else if (entityMetadata.getId() == 8 && entityMetadata.getValue() == null && cachedTag != null) {
|
2020-05-02 20:44:05 +00:00
|
|
|
cachedTag = getDefaultTag();
|
|
|
|
updateBlock(session);
|
|
|
|
}
|
2021-05-20 02:24:11 +00:00
|
|
|
else if (entityMetadata.getId() == 9) {
|
2020-05-02 20:44:05 +00:00
|
|
|
rotation = ((int) entityMetadata.getValue()) * 45;
|
|
|
|
if (cachedTag == null) {
|
|
|
|
updateBlock(session);
|
|
|
|
return;
|
|
|
|
}
|
2020-07-05 20:58:43 +00:00
|
|
|
NbtMapBuilder builder = cachedTag.toBuilder();
|
|
|
|
builder.putFloat("ItemRotation", rotation);
|
|
|
|
cachedTag = builder.build();
|
2020-05-02 20:44:05 +00:00
|
|
|
updateBlock(session);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
updateBlock(session);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean despawnEntity(GeyserSession session) {
|
|
|
|
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
|
|
|
|
updateBlockPacket.setDataLayer(0);
|
|
|
|
updateBlockPacket.setBlockPosition(bedrockPosition);
|
2021-07-13 01:19:40 +00:00
|
|
|
updateBlockPacket.setRuntimeId(session.getBlockMappings().getBedrockAirId());
|
2020-05-02 20:44:05 +00:00
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
|
2020-07-05 04:03:51 +00:00
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NETWORK);
|
2020-05-02 20:44:05 +00:00
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(updateBlockPacket);
|
2021-05-09 19:44:41 +00:00
|
|
|
|
|
|
|
session.getItemFrameCache().remove(bedrockPosition, this);
|
|
|
|
|
2020-05-02 20:44:05 +00:00
|
|
|
valid = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-05 20:58:43 +00:00
|
|
|
private NbtMap getDefaultTag() {
|
|
|
|
NbtMapBuilder builder = NbtMap.builder();
|
|
|
|
builder.putInt("x", bedrockPosition.getX());
|
|
|
|
builder.putInt("y", bedrockPosition.getY());
|
|
|
|
builder.putInt("z", bedrockPosition.getZ());
|
|
|
|
builder.putByte("isMovable", (byte) 1);
|
2021-06-03 23:04:28 +00:00
|
|
|
builder.putString("id", this.entityType == EntityType.GLOW_ITEM_FRAME ? "GlowItemFrame" : "ItemFrame");
|
2020-07-05 20:58:43 +00:00
|
|
|
return builder.build();
|
2020-05-02 20:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the item frame as a block
|
|
|
|
* @param session GeyserSession.
|
|
|
|
*/
|
|
|
|
public void updateBlock(GeyserSession session) {
|
2020-05-24 04:14:29 +00:00
|
|
|
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
|
|
|
|
updateBlockPacket.setDataLayer(0);
|
|
|
|
updateBlockPacket.setBlockPosition(bedrockPosition);
|
|
|
|
updateBlockPacket.setRuntimeId(bedrockRuntimeId);
|
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
|
2020-07-05 04:03:51 +00:00
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NETWORK);
|
2020-05-24 04:14:29 +00:00
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
|
|
|
|
session.sendUpstreamPacket(updateBlockPacket);
|
2020-05-02 20:44:05 +00:00
|
|
|
|
2020-05-24 04:14:29 +00:00
|
|
|
BlockEntityDataPacket blockEntityDataPacket = new BlockEntityDataPacket();
|
|
|
|
blockEntityDataPacket.setBlockPosition(bedrockPosition);
|
|
|
|
if (cachedTag != null) {
|
|
|
|
blockEntityDataPacket.setData(cachedTag);
|
|
|
|
} else {
|
|
|
|
blockEntityDataPacket.setData(getDefaultTag());
|
|
|
|
}
|
|
|
|
|
|
|
|
session.sendUpstreamPacket(blockEntityDataPacket);
|
2020-05-02 20:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the Java entity ID of an item frame from its Bedrock position.
|
|
|
|
* @param position position of item frame in Bedrock.
|
|
|
|
* @param session GeyserSession.
|
|
|
|
* @return Java entity ID or -1 if not found.
|
|
|
|
*/
|
2021-05-09 19:44:41 +00:00
|
|
|
public static ItemFrameEntity getItemFrameEntity(GeyserSession session, Vector3i position) {
|
|
|
|
return session.getItemFrameCache().get(position);
|
2020-05-02 20:44:05 +00:00
|
|
|
}
|
|
|
|
}
|