2019-07-11 22:39:28 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.nukkitx.network.VarInts;
|
2019-07-17 01:05:10 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
|
2019-07-11 22:39:28 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.v361.BedrockUtils;
|
2019-10-11 23:38:34 +00:00
|
|
|
import gnu.trove.map.TIntObjectMap;
|
|
|
|
import gnu.trove.map.hash.TIntObjectHashMap;
|
2019-07-11 22:39:28 +00:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
2019-08-10 03:16:34 +00:00
|
|
|
import org.geysermc.connector.console.GeyserLogger;
|
2019-10-10 00:11:50 +00:00
|
|
|
import org.geysermc.connector.network.translators.block.BlockEntry;
|
|
|
|
import org.geysermc.connector.network.translators.item.ItemEntry;
|
2019-08-30 21:47:33 +00:00
|
|
|
import org.geysermc.connector.world.GlobalBlockPalette;
|
2019-07-11 22:39:28 +00:00
|
|
|
|
|
|
|
import java.io.InputStream;
|
2019-07-17 01:05:10 +00:00
|
|
|
import java.util.*;
|
2019-07-11 22:39:28 +00:00
|
|
|
|
|
|
|
public class Toolbox {
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
public static final Collection<StartGamePacket.ItemEntry> ITEMS;
|
|
|
|
public static final ByteBuf CACHED_PALLETE;
|
|
|
|
|
2019-10-11 23:38:34 +00:00
|
|
|
public static final TIntObjectMap<ItemEntry> ITEM_ENTRIES;
|
|
|
|
public static final TIntObjectMap<BlockEntry> BLOCK_ENTRIES;
|
2019-10-10 00:11:50 +00:00
|
|
|
|
2019-07-21 22:22:56 +00:00
|
|
|
static {
|
2019-08-06 01:59:54 +00:00
|
|
|
InputStream stream = Toolbox.class.getClassLoader().getResourceAsStream("bedrock/cached_palette.json");
|
2019-07-11 22:39:28 +00:00
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
2019-10-10 00:11:50 +00:00
|
|
|
List<LinkedHashMap<String, Object>> entries = new ArrayList<>();
|
2019-07-11 22:39:28 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
entries = mapper.readValue(stream, ArrayList.class);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
ByteBuf cachedPalette = Unpooled.buffer();
|
|
|
|
VarInts.writeUnsignedInt(cachedPalette, entries.size());
|
|
|
|
|
|
|
|
Map<String, Integer> blockIdToIdentifier = new HashMap<>();
|
2019-08-10 03:16:34 +00:00
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
for (Map<String, Object> entry : entries) {
|
|
|
|
blockIdToIdentifier.put((String) entry.get("name"), (int) entry.get("id"));
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
GlobalBlockPalette.registerMapping((int) entry.get("id") << 4 | (int) entry.get("data"));
|
|
|
|
BedrockUtils.writeString(cachedPalette, (String) entry.get("name"));
|
|
|
|
cachedPalette.writeShortLE((int) entry.get("data"));
|
|
|
|
cachedPalette.writeShortLE((int) entry.get("id"));
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
CACHED_PALLETE = cachedPalette;
|
2019-07-11 22:39:28 +00:00
|
|
|
|
2019-08-06 01:59:54 +00:00
|
|
|
InputStream stream2 = Toolbox.class.getClassLoader().getResourceAsStream("bedrock/items.json");
|
2019-07-17 01:05:10 +00:00
|
|
|
if (stream2 == null) {
|
|
|
|
throw new AssertionError("Items Table not found");
|
|
|
|
}
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
ObjectMapper startGameItemMapper = new ObjectMapper();
|
|
|
|
List<Map> startGameItems = new ArrayList<>();
|
2019-07-17 01:05:10 +00:00
|
|
|
try {
|
2019-10-10 00:11:50 +00:00
|
|
|
startGameItems = startGameItemMapper.readValue(stream2, ArrayList.class);
|
2019-07-17 01:05:10 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
List<StartGamePacket.ItemEntry> startGameEntries = new ArrayList<>();
|
|
|
|
for (Map entry : startGameItems) {
|
|
|
|
startGameEntries.add(new StartGamePacket.ItemEntry((String) entry.get("name"), (short) ((int) entry.get("id"))));
|
2019-07-17 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
ITEMS = startGameEntries;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
InputStream itemStream = Toolbox.class.getClassLoader().getResourceAsStream("items.json");
|
|
|
|
ObjectMapper itemMapper = new ObjectMapper();
|
|
|
|
Map<String, Map<String, Object>> items = new HashMap<>();
|
2019-07-29 22:20:48 +00:00
|
|
|
|
|
|
|
try {
|
2019-10-10 00:11:50 +00:00
|
|
|
items = itemMapper.readValue(itemStream, LinkedHashMap.class);
|
2019-07-29 22:20:48 +00:00
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2019-10-11 23:38:34 +00:00
|
|
|
TIntObjectMap<ItemEntry> itemEntries = new TIntObjectHashMap<>();
|
2019-10-10 00:11:50 +00:00
|
|
|
int itemIndex = 0;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
for (Map.Entry<String, Map<String, Object>> itemEntry : items.entrySet()) {
|
|
|
|
itemEntries.put(itemIndex, new ItemEntry(itemEntry.getKey(), itemIndex, (int) itemEntry.getValue().get("bedrock_id"), (int) itemEntry.getValue().get("bedrock_data")));
|
|
|
|
itemIndex++;
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
2019-07-31 19:59:23 +00:00
|
|
|
|
2019-10-11 23:38:34 +00:00
|
|
|
ITEM_ENTRIES = itemEntries;
|
2019-10-10 00:11:50 +00:00
|
|
|
|
|
|
|
InputStream blockStream = Toolbox.class.getClassLoader().getResourceAsStream("blocks.json");
|
|
|
|
ObjectMapper blockMapper = new ObjectMapper();
|
|
|
|
Map<String, Map<String, Object>> blocks = new HashMap<>();
|
2019-08-07 23:08:48 +00:00
|
|
|
|
|
|
|
try {
|
2019-10-10 00:11:50 +00:00
|
|
|
blocks = blockMapper.readValue(blockStream, LinkedHashMap.class);
|
2019-08-07 23:08:48 +00:00
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2019-10-11 23:38:34 +00:00
|
|
|
TIntObjectMap<BlockEntry> blockEntries = new TIntObjectHashMap<>();
|
2019-10-10 00:11:50 +00:00
|
|
|
int blockIndex = 0;
|
2019-08-07 23:08:48 +00:00
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
for (Map.Entry<String, Map<String, Object>> itemEntry : blocks.entrySet()) {
|
|
|
|
if (!blockIdToIdentifier.containsKey(itemEntry.getValue().get("bedrock_identifier"))) {
|
|
|
|
GeyserLogger.DEFAULT.debug("Mapping " + itemEntry.getValue().get("bedrock_identifier") + " does not exist on bedrock edition!");
|
|
|
|
blockEntries.put(blockIndex, new BlockEntry(itemEntry.getKey(), blockIndex, 248, 0)); // update block
|
|
|
|
} else {
|
|
|
|
blockEntries.put(blockIndex, new BlockEntry(itemEntry.getKey(), blockIndex, blockIdToIdentifier.get(itemEntry.getValue().get("bedrock_identifier")), (int) itemEntry.getValue().get("bedrock_data")));
|
|
|
|
}
|
2019-08-07 23:08:48 +00:00
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
blockIndex++;
|
|
|
|
}
|
2019-08-07 23:08:48 +00:00
|
|
|
|
2019-10-11 23:38:34 +00:00
|
|
|
BLOCK_ENTRIES = blockEntries;
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
|
|
|
}
|