2019-11-28 00:14:38 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
2019-12-31 03:55:17 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
|
|
|
|
import com.nukkitx.math.vector.Vector3i;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2020-04-29 20:01:53 +00:00
|
|
|
import org.geysermc.connector.network.translators.world.block.entity.BlockEntityTranslator;
|
2019-11-28 00:14:38 +00:00
|
|
|
|
|
|
|
public class BlockEntityUtils {
|
|
|
|
|
2020-05-25 01:07:05 +00:00
|
|
|
private static final BlockEntityTranslator EMPTY_TRANSLATOR = BlockEntityTranslator.BLOCK_ENTITY_TRANSLATORS.get("Empty");
|
2020-01-26 05:51:29 +00:00
|
|
|
|
2019-11-28 00:14:38 +00:00
|
|
|
public static String getBedrockBlockEntityId(String id) {
|
2020-05-08 02:49:44 +00:00
|
|
|
// These are the only exceptions when it comes to block entity ids
|
2019-11-28 00:14:38 +00:00
|
|
|
if (id.contains("piston_head"))
|
|
|
|
return "PistonArm";
|
|
|
|
|
2020-05-05 00:39:44 +00:00
|
|
|
if (id.contains("trapped_chest"))
|
|
|
|
return "Chest";
|
|
|
|
|
2020-05-08 02:49:44 +00:00
|
|
|
if (id.contains("EnderChest"))
|
|
|
|
return "EnderChest";
|
|
|
|
|
2020-05-17 04:59:03 +00:00
|
|
|
if (id.contains("enchanting_table")) {
|
|
|
|
return "EnchantTable";
|
|
|
|
}
|
|
|
|
|
2019-12-31 03:55:17 +00:00
|
|
|
id = id.toLowerCase()
|
|
|
|
.replace("minecraft:", "")
|
|
|
|
.replace("_", " ");
|
2019-11-28 00:14:38 +00:00
|
|
|
String[] words = id.split(" ");
|
|
|
|
for (int i = 0; i < words.length; i++) {
|
|
|
|
words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1).toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
id = String.join(" ", words);
|
|
|
|
return id.replace(" ", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static BlockEntityTranslator getBlockEntityTranslator(String name) {
|
2020-05-25 01:07:05 +00:00
|
|
|
BlockEntityTranslator blockEntityTranslator = BlockEntityTranslator.BLOCK_ENTITY_TRANSLATORS.get(name);
|
2019-11-28 00:14:38 +00:00
|
|
|
if (blockEntityTranslator == null) {
|
2020-01-26 05:51:29 +00:00
|
|
|
return EMPTY_TRANSLATOR;
|
2019-11-28 00:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return blockEntityTranslator;
|
|
|
|
}
|
2019-12-31 03:55:17 +00:00
|
|
|
|
|
|
|
public static void updateBlockEntity(GeyserSession session, com.nukkitx.nbt.tag.CompoundTag blockEntity, Position position) {
|
2020-05-05 02:32:02 +00:00
|
|
|
updateBlockEntity(session, blockEntity, Vector3i.from(position.getX(), position.getY(), position.getZ()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateBlockEntity(GeyserSession session, com.nukkitx.nbt.tag.CompoundTag blockEntity, Vector3i position) {
|
2019-12-31 03:55:17 +00:00
|
|
|
BlockEntityDataPacket blockEntityPacket = new BlockEntityDataPacket();
|
2020-05-05 02:32:02 +00:00
|
|
|
blockEntityPacket.setBlockPosition(position);
|
2019-12-31 03:55:17 +00:00
|
|
|
blockEntityPacket.setData(blockEntity);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(blockEntityPacket);
|
2019-12-31 03:55:17 +00:00
|
|
|
}
|
2019-11-28 00:14:38 +00:00
|
|
|
}
|