/* * Copyright (c) 2019-2020 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.connector.utils; import com.fasterxml.jackson.databind.ObjectMapper; import com.nukkitx.nbt.NbtUtils; import com.nukkitx.nbt.stream.NBTInputStream; import com.nukkitx.nbt.tag.CompoundTag; import com.nukkitx.nbt.tag.ListTag; import com.nukkitx.protocol.bedrock.data.ItemData; import com.nukkitx.nbt.tag.Tag; import com.nukkitx.protocol.bedrock.packet.StartGamePacket; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.console.GeyserLogger; import org.geysermc.connector.network.translators.block.BlockEntry; import org.geysermc.connector.network.translators.item.ItemEntry; import java.io.*; import java.util.*; public class Toolbox { public static final Collection ITEMS = new ArrayList<>(); public static ListTag BLOCKS; public static ItemData[] CREATIVE_ITEMS; public static final Int2ObjectMap ITEM_ENTRIES = new Int2ObjectOpenHashMap<>(); public static final Int2ObjectMap BLOCK_ENTRIES = new Int2ObjectOpenHashMap<>(); public static void init() { InputStream stream = GeyserConnector.class.getClassLoader().getResourceAsStream("bedrock/runtime_block_states.dat"); if (stream == null) { throw new AssertionError("Unable to find bedrock/runtime_block_states.dat"); } ListTag blocksTag; NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(stream); try { blocksTag = (ListTag) nbtInputStream.readTag(); nbtInputStream.close(); } catch (Exception ex) { GeyserLogger.DEFAULT.warning("Failed to get blocks from runtime block states, please report this error!"); throw new AssertionError(ex); } BLOCKS = blocksTag; InputStream stream2 = Toolbox.class.getClassLoader().getResourceAsStream("bedrock/items.json"); if (stream2 == null) { throw new AssertionError("Items Table not found"); } ObjectMapper startGameItemMapper = new ObjectMapper(); List startGameItems = new ArrayList<>(); try { startGameItems = startGameItemMapper.readValue(stream2, ArrayList.class); } catch (Exception e) { e.printStackTrace(); } for (Map entry : startGameItems) { ITEMS.add(new StartGamePacket.ItemEntry((String) entry.get("name"), (short) ((int) entry.get("id")))); } InputStream itemStream = Toolbox.class.getClassLoader().getResourceAsStream("mappings/items.json"); ObjectMapper itemMapper = new ObjectMapper(); Map> items = new HashMap<>(); try { items = itemMapper.readValue(itemStream, LinkedHashMap.class); } catch (Exception ex) { ex.printStackTrace(); } int itemIndex = 0; for (Map.Entry> itemEntry : items.entrySet()) { ITEM_ENTRIES.put(itemIndex, new ItemEntry(itemEntry.getKey(), itemIndex, (int) itemEntry.getValue().get("bedrock_id"), (int) itemEntry.getValue().get("bedrock_data"))); itemIndex++; } InputStream blockStream = Toolbox.class.getClassLoader().getResourceAsStream("mappings/blocks.json"); ObjectMapper blockMapper = new ObjectMapper(); Map> blocks = new HashMap<>(); try { blocks = blockMapper.readValue(blockStream, LinkedHashMap.class); } catch (Exception ex) { ex.printStackTrace(); } int javaIndex = -1; javaLoop: for (Map.Entry> javaEntry : blocks.entrySet()) { javaIndex++; String wantedIdentifier = (String) javaEntry.getValue().get("bedrock_identifier"); Map wantedStates = (Map) javaEntry.getValue().get("bedrock_states"); int bedrockIndex = -1; bedrockLoop: for (CompoundTag bedrockEntry : BLOCKS.getValue()) { bedrockIndex++; CompoundTag blockTag = bedrockEntry.getAsCompound("block"); if (blockTag.getAsString("name").equals(wantedIdentifier)) { if (wantedStates != null) { Map> bedrockStates = blockTag.getAsCompound("states").getValue(); for (Map.Entry stateEntry : wantedStates.entrySet()) { Tag bedrockStateTag = bedrockStates.get(stateEntry.getKey()); if (bedrockStateTag == null) continue bedrockLoop; Object bedrockStateValue = bedrockStateTag.getValue(); if (bedrockStateValue instanceof Byte) bedrockStateValue = ((Byte) bedrockStateValue) != 0; if (!stateEntry.getValue().equals(bedrockStateValue)) continue bedrockLoop; } } BlockEntry blockEntry = new BlockEntry(javaEntry.getKey(), javaIndex, bedrockIndex); BLOCK_ENTRIES.put(javaIndex, blockEntry); continue javaLoop; } } GeyserLogger.DEFAULT.debug("Mapping " + javaEntry.getKey() + " was not found for bedrock edition!"); } InputStream creativeItemStream = Toolbox.class.getClassLoader().getResourceAsStream("bedrock/creative_items.json"); ObjectMapper creativeItemMapper = new ObjectMapper(); List> creativeItemEntries = new ArrayList<>(); try { creativeItemEntries = (ArrayList>) creativeItemMapper.readValue(creativeItemStream, HashMap.class).get("items"); } catch (Exception e) { e.printStackTrace(); } List creativeItems = new ArrayList<>(); for (Map map : creativeItemEntries) { short damage = 0; if (map.containsKey("damage")) { damage = (short)(int) map.get("damage"); } if (map.containsKey("nbt_b64")) { byte[] bytes = Base64.getDecoder().decode((String) map.get("nbt_b64")); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); try { com.nukkitx.nbt.tag.CompoundTag tag = (com.nukkitx.nbt.tag.CompoundTag) NbtUtils.createReaderLE(bais).readTag(); creativeItems.add(ItemData.of((int) map.get("id"), damage, 1, tag)); } catch (IOException e) { e.printStackTrace(); } } else { creativeItems.add(ItemData.of((int) map.get("id"), damage, 1)); } } CREATIVE_ITEMS = creativeItems.toArray(new ItemData[0]); } }