Geyser/core/src/main/java/org/geysermc/geyser/translator/level/block/entity/DoubleChestBlockEntityTrans...

98 lines
4.2 KiB
Java
Raw Normal View History

/*
2022-01-01 19:03:05 +00:00
* Copyright (c) 2019-2022 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.geyser.translator.level.block.entity;
2022-10-30 00:23:21 +00:00
import org.cloudburstmc.math.vector.Vector3i;
2024-04-27 01:44:59 +00:00
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder;
import org.geysermc.geyser.level.block.BlockStateValues;
import org.geysermc.geyser.level.block.DoubleChestValue;
2022-06-08 12:09:14 +00:00
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.util.BlockEntityUtils;
import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockEntityType;
/**
* Chests have more block entity properties in Bedrock, which is solved by implementing the BedrockOnlyBlockEntity
*/
2021-11-14 17:06:07 +00:00
@BlockEntity(type = { BlockEntityType.CHEST, BlockEntityType.TRAPPED_CHEST })
public class DoubleChestBlockEntityTranslator extends BlockEntityTranslator implements BedrockOnlyBlockEntity {
@Override
2020-06-19 01:44:50 +00:00
public boolean isBlock(int blockState) {
return BlockStateValues.getDoubleChestValues().containsKey(blockState);
}
@Override
2021-11-22 19:52:26 +00:00
public void updateBlock(GeyserSession session, int blockState, Vector3i position) {
2021-11-14 17:06:07 +00:00
NbtMapBuilder tagBuilder = getConstantBedrockTag(BlockEntityUtils.getBedrockBlockEntityId(BlockEntityType.CHEST), position.getX(), position.getY(), position.getZ());
2024-04-27 01:44:59 +00:00
translateTag(session, tagBuilder, null, blockState);
2020-07-05 20:58:43 +00:00
BlockEntityUtils.updateBlockEntity(session, tagBuilder.build(), position);
}
@Override
2024-04-27 01:44:59 +00:00
public void translateTag(GeyserSession session, NbtMapBuilder bedrockNbt, NbtMap javaNbt, int blockState) {
DoubleChestValue chestValues = BlockStateValues.getDoubleChestValues().get(blockState);
if (chestValues != null) {
2024-04-27 01:44:59 +00:00
int x = (int) bedrockNbt.get("x");
int z = (int) bedrockNbt.get("z");
translateChestValue(bedrockNbt, chestValues, x, z);
}
}
/**
* Add Bedrock block entity tags to a NbtMap based on Java properties
*
* @param builder the NbtMapBuilder to apply properties to
* @param chestValues the position properties of this double chest
* @param x the x position of this chest pair
* @param z the z position of this chest pair
*/
public static void translateChestValue(NbtMapBuilder builder, DoubleChestValue chestValues, int x, int z) {
// Calculate the position of the other chest based on the Java block state
2021-11-14 17:06:07 +00:00
if (chestValues.isFacingEast()) {
if (chestValues.isDirectionPositive()) {
// East
2021-11-14 17:06:07 +00:00
z = z + (chestValues.isLeft() ? 1 : -1);
} else {
// West
2021-11-14 17:06:07 +00:00
z = z + (chestValues.isLeft() ? -1 : 1);
}
} else {
2021-11-14 17:06:07 +00:00
if (chestValues.isDirectionPositive()) {
// South
2021-11-14 17:06:07 +00:00
x = x + (chestValues.isLeft() ? -1 : 1);
} else {
// North
2021-11-14 17:06:07 +00:00
x = x + (chestValues.isLeft() ? 1 : -1);
}
}
2024-04-27 01:44:59 +00:00
builder.putInt("pairx", x);
builder.putInt("pairz", z);
2021-11-14 17:06:07 +00:00
if (!chestValues.isLeft()) {
2024-04-27 01:44:59 +00:00
builder.putInt("pairlead", (byte) 1);
}
}
}