2019-10-20 21:25:41 +00:00
|
|
|
/*
|
2020-01-31 01:05:57 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-10-20 21:25:41 +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.network.translators.inventory;
|
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
|
|
|
|
import com.nukkitx.math.vector.Vector3i;
|
|
|
|
import com.nukkitx.nbt.tag.CompoundTag;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ContainerType;
|
2019-10-21 03:41:46 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket;
|
2020-01-31 01:05:57 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.ContainerOpenPacket;
|
2019-10-21 03:41:46 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
|
2019-10-20 21:25:41 +00:00
|
|
|
import org.geysermc.connector.inventory.Inventory;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2020-04-29 20:01:53 +00:00
|
|
|
import org.geysermc.connector.network.translators.world.block.BlockTranslator;
|
2020-01-31 01:05:57 +00:00
|
|
|
|
2020-06-02 16:48:26 +00:00
|
|
|
public class DoubleChestInventoryTranslator extends ChestInventoryTranslator {
|
2020-01-31 01:05:57 +00:00
|
|
|
private final int blockId;
|
2019-10-20 21:25:41 +00:00
|
|
|
|
2019-10-21 03:41:46 +00:00
|
|
|
public DoubleChestInventoryTranslator(int size) {
|
2020-06-02 16:48:26 +00:00
|
|
|
super(size, 54);
|
2020-06-19 01:44:50 +00:00
|
|
|
int javaBlockState = BlockTranslator.getJavaBlockState("minecraft:chest[facing=north,type=single,waterlogged=false]");
|
2020-02-13 07:29:44 +00:00
|
|
|
this.blockId = BlockTranslator.getBedrockBlockId(javaBlockState);
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void prepareInventory(GeyserSession session, Inventory inventory) {
|
2019-10-21 03:41:46 +00:00
|
|
|
Vector3i position = session.getPlayerEntity().getPosition().toInt().add(Vector3i.UP);
|
|
|
|
Vector3i pairPosition = position.add(Vector3i.UNIT_X);
|
2020-01-31 01:05:57 +00:00
|
|
|
|
2019-10-20 21:25:41 +00:00
|
|
|
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
|
|
|
|
blockPacket.setDataLayer(0);
|
|
|
|
blockPacket.setBlockPosition(position);
|
2019-12-24 02:00:31 +00:00
|
|
|
blockPacket.setRuntimeId(blockId);
|
2020-01-31 01:05:57 +00:00
|
|
|
blockPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(blockPacket);
|
2019-10-20 21:25:41 +00:00
|
|
|
|
2020-01-31 01:05:57 +00:00
|
|
|
CompoundTag tag = CompoundTag.builder()
|
2019-10-21 03:41:46 +00:00
|
|
|
.stringTag("id", "Chest")
|
|
|
|
.intTag("x", position.getX())
|
|
|
|
.intTag("y", position.getY())
|
|
|
|
.intTag("z", position.getZ())
|
|
|
|
.intTag("pairx", pairPosition.getX())
|
2019-11-01 09:07:13 +00:00
|
|
|
.intTag("pairz", pairPosition.getZ())
|
|
|
|
.stringTag("CustomName", inventory.getTitle()).buildRootTag();
|
2019-10-21 03:41:46 +00:00
|
|
|
BlockEntityDataPacket dataPacket = new BlockEntityDataPacket();
|
|
|
|
dataPacket.setData(tag);
|
|
|
|
dataPacket.setBlockPosition(position);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(dataPacket);
|
2019-10-21 03:41:46 +00:00
|
|
|
|
|
|
|
blockPacket = new UpdateBlockPacket();
|
|
|
|
blockPacket.setDataLayer(0);
|
|
|
|
blockPacket.setBlockPosition(pairPosition);
|
2019-12-24 02:00:31 +00:00
|
|
|
blockPacket.setRuntimeId(blockId);
|
2020-01-31 01:05:57 +00:00
|
|
|
blockPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(blockPacket);
|
2019-10-21 03:41:46 +00:00
|
|
|
|
2020-01-31 01:05:57 +00:00
|
|
|
tag = CompoundTag.builder()
|
2019-10-21 03:41:46 +00:00
|
|
|
.stringTag("id", "Chest")
|
|
|
|
.intTag("x", pairPosition.getX())
|
|
|
|
.intTag("y", pairPosition.getY())
|
|
|
|
.intTag("z", pairPosition.getZ())
|
|
|
|
.intTag("pairx", position.getX())
|
2019-11-01 09:07:13 +00:00
|
|
|
.intTag("pairz", position.getZ())
|
|
|
|
.stringTag("CustomName", inventory.getTitle()).buildRootTag();
|
2019-10-21 03:41:46 +00:00
|
|
|
dataPacket = new BlockEntityDataPacket();
|
|
|
|
dataPacket.setData(tag);
|
|
|
|
dataPacket.setBlockPosition(pairPosition);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(dataPacket);
|
2019-10-20 21:25:41 +00:00
|
|
|
|
|
|
|
inventory.setHolderPosition(position);
|
|
|
|
}
|
|
|
|
|
2020-01-31 01:05:57 +00:00
|
|
|
@Override
|
|
|
|
public void openInventory(GeyserSession session, Inventory inventory) {
|
|
|
|
ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket();
|
2020-06-23 00:11:09 +00:00
|
|
|
containerOpenPacket.setId((byte) inventory.getId());
|
|
|
|
containerOpenPacket.setType(ContainerType.CONTAINER);
|
2020-01-31 01:05:57 +00:00
|
|
|
containerOpenPacket.setBlockPosition(inventory.getHolderPosition());
|
|
|
|
containerOpenPacket.setUniqueEntityId(inventory.getHolderId());
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(containerOpenPacket);
|
2020-01-31 01:05:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 21:25:41 +00:00
|
|
|
@Override
|
|
|
|
public void closeInventory(GeyserSession session, Inventory inventory) {
|
|
|
|
Vector3i holderPos = inventory.getHolderPosition();
|
|
|
|
Position pos = new Position(holderPos.getX(), holderPos.getY(), holderPos.getZ());
|
2020-06-19 01:44:50 +00:00
|
|
|
int realBlock = session.getConnector().getWorldManager().getBlockAt(session, pos.getX(), pos.getY(), pos.getZ());
|
2019-10-20 21:25:41 +00:00
|
|
|
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
|
|
|
|
blockPacket.setDataLayer(0);
|
|
|
|
blockPacket.setBlockPosition(holderPos);
|
2020-02-13 07:29:44 +00:00
|
|
|
blockPacket.setRuntimeId(BlockTranslator.getBedrockBlockId(realBlock));
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(blockPacket);
|
2019-10-20 21:25:41 +00:00
|
|
|
|
2019-10-21 03:41:46 +00:00
|
|
|
holderPos = holderPos.add(Vector3i.UNIT_X);
|
|
|
|
pos = new Position(holderPos.getX(), holderPos.getY(), holderPos.getZ());
|
2020-04-29 20:56:41 +00:00
|
|
|
realBlock = session.getConnector().getWorldManager().getBlockAt(session, pos.getX(), pos.getY(), pos.getZ());
|
2019-10-21 03:41:46 +00:00
|
|
|
blockPacket = new UpdateBlockPacket();
|
|
|
|
blockPacket.setDataLayer(0);
|
|
|
|
blockPacket.setBlockPosition(holderPos);
|
2020-02-13 07:29:44 +00:00
|
|
|
blockPacket.setRuntimeId(BlockTranslator.getBedrockBlockId(realBlock));
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(blockPacket);
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
|
|
|
}
|