2019-10-20 21:25:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 GeyserMC. http://geysermc.org
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.ContainerType;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.ItemData;
|
2019-10-21 03:41:46 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket;
|
|
|
|
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;
|
|
|
|
import org.geysermc.connector.network.translators.TranslatorsInit;
|
|
|
|
import org.geysermc.connector.network.translators.block.BlockEntry;
|
|
|
|
import org.geysermc.connector.world.GlobalBlockPalette;
|
|
|
|
|
2019-11-05 23:17:22 +00:00
|
|
|
public class DoubleChestInventoryTranslator extends BlockInventoryTranslator {
|
2019-10-21 03:41:46 +00:00
|
|
|
public DoubleChestInventoryTranslator(int size) {
|
2019-11-05 23:17:22 +00:00
|
|
|
super(size, 54 << 4, ContainerType.CONTAINER);
|
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);
|
2019-10-20 21:25:41 +00:00
|
|
|
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
|
|
|
|
blockPacket.setDataLayer(0);
|
|
|
|
blockPacket.setBlockPosition(position);
|
2019-11-05 23:17:22 +00:00
|
|
|
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(blockId));
|
2019-10-20 21:25:41 +00:00
|
|
|
blockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
|
|
|
|
session.getUpstream().sendPacket(blockPacket);
|
|
|
|
|
2019-10-21 03:41:46 +00:00
|
|
|
CompoundTag tag = CompoundTag.EMPTY.toBuilder()
|
|
|
|
.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);
|
|
|
|
session.getUpstream().sendPacket(dataPacket);
|
|
|
|
|
|
|
|
blockPacket = new UpdateBlockPacket();
|
|
|
|
blockPacket.setDataLayer(0);
|
|
|
|
blockPacket.setBlockPosition(pairPosition);
|
2019-11-05 23:17:22 +00:00
|
|
|
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(blockId));
|
2019-10-21 03:41:46 +00:00
|
|
|
blockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
|
|
|
|
session.getUpstream().sendPacket(blockPacket);
|
|
|
|
|
|
|
|
tag = CompoundTag.EMPTY.toBuilder()
|
|
|
|
.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);
|
|
|
|
session.getUpstream().sendPacket(dataPacket);
|
2019-10-20 21:25:41 +00:00
|
|
|
|
|
|
|
inventory.setHolderPosition(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void closeInventory(GeyserSession session, Inventory inventory) {
|
|
|
|
Vector3i holderPos = inventory.getHolderPosition();
|
|
|
|
Position pos = new Position(holderPos.getX(), holderPos.getY(), holderPos.getZ());
|
|
|
|
BlockEntry realBlock = session.getChunkCache().getBlockAt(pos);
|
|
|
|
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
|
|
|
|
blockPacket.setDataLayer(0);
|
|
|
|
blockPacket.setBlockPosition(holderPos);
|
|
|
|
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(realBlock.getBedrockId() << 4 | realBlock.getBedrockData()));
|
|
|
|
session.getUpstream().sendPacket(blockPacket);
|
|
|
|
|
2019-10-21 03:41:46 +00:00
|
|
|
holderPos = holderPos.add(Vector3i.UNIT_X);
|
|
|
|
pos = new Position(holderPos.getX(), holderPos.getY(), holderPos.getZ());
|
|
|
|
realBlock = session.getChunkCache().getBlockAt(pos);
|
|
|
|
blockPacket = new UpdateBlockPacket();
|
|
|
|
blockPacket.setDataLayer(0);
|
|
|
|
blockPacket.setBlockPosition(holderPos);
|
|
|
|
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(realBlock.getBedrockId() << 4 | realBlock.getBedrockData()));
|
|
|
|
session.getUpstream().sendPacket(blockPacket);
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateInventory(GeyserSession session, Inventory inventory) {
|
2019-11-05 23:17:22 +00:00
|
|
|
//need to pad empty slots for 4x9 and 5x9
|
|
|
|
ItemData[] bedrockItems = new ItemData[54];
|
2019-10-20 21:25:41 +00:00
|
|
|
for (int i = 0; i < bedrockItems.length; i++) {
|
|
|
|
if (i <= this.size) {
|
2019-11-28 03:55:58 +00:00
|
|
|
bedrockItems[i] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItem(i));
|
2019-10-20 21:25:41 +00:00
|
|
|
} else {
|
|
|
|
bedrockItems[i] = ItemData.AIR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InventoryContentPacket contentPacket = new InventoryContentPacket();
|
|
|
|
contentPacket.setContainerId(inventory.getId());
|
|
|
|
contentPacket.setContents(bedrockItems);
|
|
|
|
session.getUpstream().sendPacket(contentPacket);
|
|
|
|
|
|
|
|
Inventory playerInventory = session.getInventory();
|
|
|
|
for (int i = 0; i < 36; i++) {
|
2019-11-28 03:55:58 +00:00
|
|
|
playerInventory.setItem(i + 9, inventory.getItem(i + this.size));
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
|
|
|
TranslatorsInit.getInventoryTranslators().get(playerInventory.getWindowType()).updateInventory(session, playerInventory);
|
|
|
|
}
|
|
|
|
}
|