2020-05-10 19:26:00 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-05-10 19:26:00 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +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:
|
2020-05-10 19:26:00 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2020-05-10 19:26:00 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* 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.
|
2020-05-10 19:26:00 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
2020-05-10 19:26:00 +00:00
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.translator.level.block.entity;
|
2020-05-10 19:26:00 +00:00
|
|
|
|
2021-11-14 17:06:07 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.level.block.BlockEntityType;
|
2020-05-10 19:26:00 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.math.vector.Vector3i;
|
2022-12-21 00:47:45 +00:00
|
|
|
import org.cloudburstmc.nbt.NbtMapBuilder;
|
2021-11-20 23:29:46 +00:00
|
|
|
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;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.util.BlockEntityUtils;
|
2020-05-10 19:26:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 })
|
2021-06-01 19:36:33 +00:00
|
|
|
public class DoubleChestBlockEntityTranslator extends BlockEntityTranslator implements BedrockOnlyBlockEntity {
|
2020-05-10 19:26:00 +00:00
|
|
|
@Override
|
2020-06-19 01:44:50 +00:00
|
|
|
public boolean isBlock(int blockState) {
|
|
|
|
return BlockStateValues.getDoubleChestValues().containsKey(blockState);
|
2020-05-10 19:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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());
|
2021-11-27 02:05:14 +00:00
|
|
|
translateTag(tagBuilder, null, blockState);
|
2020-07-05 20:58:43 +00:00
|
|
|
BlockEntityUtils.updateBlockEntity(session, tagBuilder.build(), position);
|
2020-05-10 19:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-11-05 21:36:22 +00:00
|
|
|
public void translateTag(NbtMapBuilder builder, CompoundTag tag, int blockState) {
|
2021-11-27 02:05:14 +00:00
|
|
|
DoubleChestValue chestValues = BlockStateValues.getDoubleChestValues().get(blockState);
|
2020-11-05 21:36:22 +00:00
|
|
|
if (chestValues != null) {
|
2021-11-27 02:05:14 +00:00
|
|
|
int x = (int) builder.get("x");
|
|
|
|
int z = (int) builder.get("z");
|
2021-02-12 19:39:41 +00:00
|
|
|
translateChestValue(builder, 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()) {
|
2021-02-12 19:39:41 +00:00
|
|
|
// East
|
2021-11-14 17:06:07 +00:00
|
|
|
z = z + (chestValues.isLeft() ? 1 : -1);
|
2020-11-05 21:36:22 +00:00
|
|
|
} else {
|
2021-02-12 19:39:41 +00:00
|
|
|
// West
|
2021-11-14 17:06:07 +00:00
|
|
|
z = z + (chestValues.isLeft() ? -1 : 1);
|
2020-05-10 19:26:00 +00:00
|
|
|
}
|
2021-02-12 19:39:41 +00:00
|
|
|
} else {
|
2021-11-14 17:06:07 +00:00
|
|
|
if (chestValues.isDirectionPositive()) {
|
2021-02-12 19:39:41 +00:00
|
|
|
// South
|
2021-11-14 17:06:07 +00:00
|
|
|
x = x + (chestValues.isLeft() ? -1 : 1);
|
2021-02-12 19:39:41 +00:00
|
|
|
} else {
|
|
|
|
// North
|
2021-11-14 17:06:07 +00:00
|
|
|
x = x + (chestValues.isLeft() ? 1 : -1);
|
2020-11-05 21:36:22 +00:00
|
|
|
}
|
2020-05-10 19:26:00 +00:00
|
|
|
}
|
2021-02-12 19:39:41 +00:00
|
|
|
builder.put("pairx", x);
|
|
|
|
builder.put("pairz", z);
|
2021-11-14 17:06:07 +00:00
|
|
|
if (!chestValues.isLeft()) {
|
2021-02-12 19:39:41 +00:00
|
|
|
builder.put("pairlead", (byte) 1);
|
|
|
|
}
|
2020-05-10 19:26:00 +00:00
|
|
|
}
|
|
|
|
}
|