2020-01-31 01:05:57 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-01-31 01:05:57 +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.holder;
|
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
|
2021-02-17 00:09:57 +00:00
|
|
|
import com.google.common.collect.ImmutableSet;
|
2020-01-31 01:05:57 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3i;
|
2020-07-05 20:58:43 +00:00
|
|
|
import com.nukkitx.nbt.NbtMap;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ContainerType;
|
2020-01-31 01:05:57 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket;
|
2021-02-21 04:52:49 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.ContainerClosePacket;
|
2020-01-31 01:05:57 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.ContainerOpenPacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
|
2021-02-12 19:39:41 +00:00
|
|
|
import org.geysermc.connector.inventory.Container;
|
2020-01-31 01:05:57 +00:00
|
|
|
import org.geysermc.connector.inventory.Inventory;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
|
2021-07-13 01:19:40 +00:00
|
|
|
import org.geysermc.connector.registry.BlockRegistries;
|
2020-01-31 01:05:57 +00:00
|
|
|
|
2021-02-12 19:39:41 +00:00
|
|
|
import java.util.Collections;
|
2021-02-17 00:09:57 +00:00
|
|
|
import java.util.HashSet;
|
2021-02-12 19:39:41 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
2020-12-16 04:01:27 +00:00
|
|
|
/**
|
2021-02-12 19:39:41 +00:00
|
|
|
* Manages the fake block we implement for each inventory, should we need to.
|
|
|
|
* This class will attempt to use a real block first, if possible.
|
2020-12-16 04:01:27 +00:00
|
|
|
*/
|
2020-01-31 01:05:57 +00:00
|
|
|
public class BlockInventoryHolder extends InventoryHolder {
|
2021-02-12 19:39:41 +00:00
|
|
|
/**
|
2021-02-13 20:29:54 +00:00
|
|
|
* The default Java block ID to translate as a fake block
|
2021-02-12 19:39:41 +00:00
|
|
|
*/
|
2021-02-13 20:29:54 +00:00
|
|
|
private final int defaultJavaBlockState;
|
2020-01-31 01:05:57 +00:00
|
|
|
private final ContainerType containerType;
|
2021-02-12 19:39:41 +00:00
|
|
|
private final Set<String> validBlocks;
|
|
|
|
|
|
|
|
public BlockInventoryHolder(String javaBlockIdentifier, ContainerType containerType, String... validBlocks) {
|
2021-07-13 01:19:40 +00:00
|
|
|
this.defaultJavaBlockState = BlockRegistries.JAVA_IDENTIFIERS.get(javaBlockIdentifier);
|
2021-02-12 19:39:41 +00:00
|
|
|
this.containerType = containerType;
|
|
|
|
if (validBlocks != null) {
|
2021-02-17 00:09:57 +00:00
|
|
|
Set<String> validBlocksTemp = new HashSet<>(validBlocks.length + 1);
|
|
|
|
Collections.addAll(validBlocksTemp, validBlocks);
|
|
|
|
validBlocksTemp.add(javaBlockIdentifier.split("\\[")[0]);
|
|
|
|
this.validBlocks = ImmutableSet.copyOf(validBlocksTemp);
|
2021-02-12 19:39:41 +00:00
|
|
|
} else {
|
|
|
|
this.validBlocks = Collections.singleton(javaBlockIdentifier.split("\\[")[0]);
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 01:05:57 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void prepareInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
|
2021-02-12 19:39:41 +00:00
|
|
|
// Check to see if there is an existing block we can use that the player just selected.
|
|
|
|
// First, verify that the player's position has not changed, so we don't try to select a block wildly out of range.
|
|
|
|
// (This could be a virtual inventory that the player is opening)
|
2021-03-12 22:07:29 +00:00
|
|
|
if (checkInteractionPosition(session)) {
|
2021-02-12 19:39:41 +00:00
|
|
|
// Then, check to see if the interacted block is valid for this inventory by ensuring the block state identifier is valid
|
|
|
|
int javaBlockId = session.getConnector().getWorldManager().getBlockAt(session, session.getLastInteractionBlockPosition());
|
2021-07-13 01:19:40 +00:00
|
|
|
String[] javaBlockString = BlockRegistries.JAVA_IDENTIFIERS.get().inverse().getOrDefault(javaBlockId, "minecraft:air").split("\\[");
|
2021-02-21 04:52:49 +00:00
|
|
|
if (isValidBlock(javaBlockString)) {
|
2021-02-12 19:39:41 +00:00
|
|
|
// We can safely use this block
|
|
|
|
inventory.setHolderPosition(session.getLastInteractionBlockPosition());
|
2021-02-21 04:52:49 +00:00
|
|
|
((Container) inventory).setUsingRealBlock(true, javaBlockString[0]);
|
2021-02-26 02:51:50 +00:00
|
|
|
setCustomName(session, session.getLastInteractionBlockPosition(), inventory, javaBlockId);
|
2021-02-12 19:39:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-12-23 17:30:36 +00:00
|
|
|
}
|
2021-02-12 19:39:41 +00:00
|
|
|
|
|
|
|
// Otherwise, time to conjure up a fake block!
|
2020-01-31 01:05:57 +00:00
|
|
|
Vector3i position = session.getPlayerEntity().getPosition().toInt();
|
|
|
|
position = position.add(Vector3i.UP);
|
|
|
|
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
|
|
|
|
blockPacket.setDataLayer(0);
|
|
|
|
blockPacket.setBlockPosition(position);
|
2021-07-13 01:19:40 +00:00
|
|
|
blockPacket.setRuntimeId(session.getBlockMappings().getBedrockBlockId(defaultJavaBlockState));
|
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);
|
2020-01-31 01:05:57 +00:00
|
|
|
inventory.setHolderPosition(position);
|
|
|
|
|
2021-02-26 02:51:50 +00:00
|
|
|
setCustomName(session, position, inventory, defaultJavaBlockState);
|
2021-02-12 19:39:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 22:07:29 +00:00
|
|
|
/**
|
|
|
|
* Will be overwritten in the beacon inventory translator to remove the check, since virtual inventories can't exist.
|
|
|
|
*
|
|
|
|
* @return if the player's last interaction position and current position match. Used to ensure that we don't select
|
|
|
|
* a block to hold the inventory that's wildly out of range.
|
|
|
|
*/
|
|
|
|
protected boolean checkInteractionPosition(GeyserSession session) {
|
|
|
|
return session.getLastInteractionPlayerPosition().equals(session.getPlayerEntity().getPosition());
|
|
|
|
}
|
|
|
|
|
2021-02-21 04:52:49 +00:00
|
|
|
/**
|
|
|
|
* @return true if this Java block ID can be used for player inventory.
|
|
|
|
*/
|
|
|
|
protected boolean isValidBlock(String[] javaBlockString) {
|
|
|
|
return this.validBlocks.contains(javaBlockString[0]);
|
|
|
|
}
|
|
|
|
|
2021-02-26 02:51:50 +00:00
|
|
|
protected void setCustomName(GeyserSession session, Vector3i position, Inventory inventory, int javaBlockState) {
|
2020-07-05 20:58:43 +00:00
|
|
|
NbtMap tag = NbtMap.builder()
|
|
|
|
.putInt("x", position.getX())
|
|
|
|
.putInt("y", position.getY())
|
|
|
|
.putInt("z", position.getZ())
|
2020-09-29 17:19:37 +00:00
|
|
|
.putString("CustomName", inventory.getTitle()).build();
|
2020-01-31 01:05:57 +00:00
|
|
|
BlockEntityDataPacket dataPacket = new BlockEntityDataPacket();
|
|
|
|
dataPacket.setData(tag);
|
|
|
|
dataPacket.setBlockPosition(position);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(dataPacket);
|
2020-01-31 01:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void openInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
|
|
|
|
ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket();
|
2020-06-23 00:11:09 +00:00
|
|
|
containerOpenPacket.setId((byte) inventory.getId());
|
|
|
|
containerOpenPacket.setType(containerType);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void closeInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
|
2021-02-12 19:39:41 +00:00
|
|
|
if (((Container) inventory).isUsingRealBlock()) {
|
|
|
|
// No need to reset a block since we didn't change any blocks
|
2021-02-21 04:52:49 +00:00
|
|
|
// But send a container close packet because we aren't destroying the original.
|
|
|
|
ContainerClosePacket packet = new ContainerClosePacket();
|
|
|
|
packet.setId((byte) inventory.getId());
|
|
|
|
packet.setUnknownBool0(true); //TODO needs to be changed in Protocol to "server-side" or something
|
|
|
|
session.sendUpstreamPacket(packet);
|
2020-12-23 17:30:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-02-12 19:39:41 +00:00
|
|
|
|
|
|
|
Vector3i holderPos = inventory.getHolderPosition();
|
2020-01-31 01:05:57 +00:00
|
|
|
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());
|
2020-01-31 01:05:57 +00:00
|
|
|
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
|
|
|
|
blockPacket.setDataLayer(0);
|
|
|
|
blockPacket.setBlockPosition(holderPos);
|
2021-07-13 01:19:40 +00:00
|
|
|
blockPacket.setRuntimeId(session.getBlockMappings().getBedrockBlockId(realBlock));
|
2020-12-28 05:47:10 +00:00
|
|
|
blockPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(blockPacket);
|
2020-01-31 01:05:57 +00:00
|
|
|
}
|
|
|
|
}
|