2020-05-05 02:32:02 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-05-05 02:32:02 +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-05 02:32:02 +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-05 02:32:02 +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-05 02:32:02 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
2020-05-05 02:32:02 +00:00
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.translator.level.block.entity;
|
2020-05-05 02:32:02 +00:00
|
|
|
|
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.NbtMap;
|
|
|
|
import org.cloudburstmc.nbt.NbtMapBuilder;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.UpdateBlockPacket;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.level.block.BlockStateValues;
|
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-05 02:32:02 +00:00
|
|
|
|
2021-06-01 19:36:33 +00:00
|
|
|
public class FlowerPotBlockEntityTranslator implements BedrockOnlyBlockEntity {
|
2020-11-05 21:36:22 +00:00
|
|
|
/**
|
|
|
|
* @param blockState the Java block state of a potential flower pot block
|
|
|
|
* @return true if the block is a flower pot
|
|
|
|
*/
|
|
|
|
public static boolean isFlowerBlock(int blockState) {
|
|
|
|
return BlockStateValues.getFlowerPotValues().containsKey(blockState);
|
2020-05-05 02:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Nukkit CompoundTag of the flower pot.
|
2020-11-05 21:36:22 +00:00
|
|
|
*
|
2020-06-19 01:44:50 +00:00
|
|
|
* @param blockState Java block state of flower pot.
|
2020-11-05 21:36:22 +00:00
|
|
|
* @param position Bedrock position of flower pot.
|
2020-05-05 02:32:02 +00:00
|
|
|
* @return Bedrock tag of flower pot.
|
|
|
|
*/
|
2021-11-22 19:52:26 +00:00
|
|
|
public static NbtMap getTag(GeyserSession session, int blockState, Vector3i position) {
|
2020-07-05 20:58:43 +00:00
|
|
|
NbtMapBuilder tagBuilder = NbtMap.builder()
|
|
|
|
.putInt("x", position.getX())
|
|
|
|
.putInt("y", position.getY())
|
|
|
|
.putInt("z", position.getZ())
|
|
|
|
.putByte("isMovable", (byte) 1)
|
|
|
|
.putString("id", "FlowerPot");
|
2020-05-05 02:32:02 +00:00
|
|
|
// Get the Java name of the plant inside. e.g. minecraft:oak_sapling
|
2020-06-19 01:44:50 +00:00
|
|
|
String name = BlockStateValues.getFlowerPotValues().get(blockState);
|
2020-05-05 02:32:02 +00:00
|
|
|
if (name != null) {
|
|
|
|
// Get the Bedrock CompoundTag of the block.
|
|
|
|
// This is where we need to store the *Java* name because Bedrock has six minecraft:sapling blocks with different block states.
|
2021-07-13 01:19:40 +00:00
|
|
|
NbtMap plant = session.getBlockMappings().getFlowerPotBlocks().get(name);
|
2020-05-05 02:32:02 +00:00
|
|
|
if (plant != null) {
|
2020-07-05 20:58:43 +00:00
|
|
|
tagBuilder.put("PlantBlock", plant.toBuilder().build());
|
2020-05-05 02:32:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-05 20:58:43 +00:00
|
|
|
return tagBuilder.build();
|
2020-05-05 02:32:02 +00:00
|
|
|
}
|
2020-11-05 21:36:22 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isBlock(int blockState) {
|
|
|
|
return isFlowerBlock(blockState);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void updateBlock(GeyserSession session, int blockState, Vector3i position) {
|
2021-03-09 17:51:48 +00:00
|
|
|
NbtMap tag = getTag(session, blockState, position);
|
|
|
|
BlockEntityUtils.updateBlockEntity(session, tag, position);
|
2020-11-05 21:36:22 +00:00
|
|
|
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
|
|
|
|
updateBlockPacket.setDataLayer(0);
|
2022-10-30 16:34:08 +00:00
|
|
|
updateBlockPacket.setDefinition(session.getBlockMappings().getBedrockBlock(blockState));
|
2020-11-05 21:36:22 +00:00
|
|
|
updateBlockPacket.setBlockPosition(position);
|
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
|
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NETWORK);
|
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
|
|
|
|
session.sendUpstreamPacket(updateBlockPacket);
|
2021-03-09 17:51:48 +00:00
|
|
|
BlockEntityUtils.updateBlockEntity(session, tag, position);
|
2020-11-05 21:36:22 +00:00
|
|
|
}
|
2020-05-05 02:32:02 +00:00
|
|
|
}
|