2020-01-09 03:05:42 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2019-07-11 22:39:28 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
2020-01-31 01:05:57 +00:00
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
2019-07-11 22:39:28 +00:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2019-11-10 02:20:47 +00:00
|
|
|
import com.nukkitx.nbt.NbtUtils;
|
2019-10-31 02:14:23 +00:00
|
|
|
import com.nukkitx.nbt.stream.NBTInputStream;
|
|
|
|
import com.nukkitx.nbt.tag.CompoundTag;
|
|
|
|
import com.nukkitx.nbt.tag.ListTag;
|
2019-11-10 02:20:47 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.ItemData;
|
2019-12-21 05:05:20 +00:00
|
|
|
import com.nukkitx.nbt.tag.Tag;
|
2019-07-17 01:05:10 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
|
2019-10-31 02:14:23 +00:00
|
|
|
|
2019-11-30 19:26:51 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
2019-10-31 02:14:23 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
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-07-11 22:39:28 +00:00
|
|
|
|
2019-11-10 02:20:47 +00:00
|
|
|
import java.io.*;
|
2019-07-17 01:05:10 +00:00
|
|
|
import java.util.*;
|
2019-07-11 22:39:28 +00:00
|
|
|
|
|
|
|
public class Toolbox {
|
|
|
|
|
2019-11-06 00:55:59 +00:00
|
|
|
public static final Collection<StartGamePacket.ItemEntry> ITEMS = new ArrayList<>();
|
2019-11-02 20:50:04 +00:00
|
|
|
public static ListTag<CompoundTag> BLOCKS;
|
2019-11-15 23:55:15 +00:00
|
|
|
public static ItemData[] CREATIVE_ITEMS;
|
2019-10-10 00:11:50 +00:00
|
|
|
|
2019-11-30 19:26:51 +00:00
|
|
|
public static final Int2ObjectMap<ItemEntry> ITEM_ENTRIES = new Int2ObjectOpenHashMap<>();
|
|
|
|
public static final Int2ObjectMap<BlockEntry> BLOCK_ENTRIES = new Int2ObjectOpenHashMap<>();
|
2019-07-11 22:39:28 +00:00
|
|
|
|
2019-11-06 00:55:59 +00:00
|
|
|
public static void init() {
|
2019-11-02 20:50:04 +00:00
|
|
|
InputStream stream = GeyserConnector.class.getClassLoader().getResourceAsStream("bedrock/runtime_block_states.dat");
|
2019-10-31 02:14:23 +00:00
|
|
|
if (stream == null) {
|
2019-11-02 20:50:04 +00:00
|
|
|
throw new AssertionError("Unable to find bedrock/runtime_block_states.dat");
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 20:50:04 +00:00
|
|
|
ListTag<CompoundTag> blocksTag;
|
2019-08-10 03:16:34 +00:00
|
|
|
|
2019-11-09 17:14:31 +00:00
|
|
|
NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(stream);
|
2019-10-31 02:14:23 +00:00
|
|
|
try {
|
2019-11-02 20:50:04 +00:00
|
|
|
blocksTag = (ListTag<CompoundTag>) nbtInputStream.readTag();
|
2019-10-31 02:14:23 +00:00
|
|
|
nbtInputStream.close();
|
|
|
|
} catch (Exception ex) {
|
2019-11-02 20:50:04 +00:00
|
|
|
GeyserLogger.DEFAULT.warning("Failed to get blocks from runtime block states, please report this error!");
|
2019-10-31 02:14:23 +00:00
|
|
|
throw new AssertionError(ex);
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 20:50:04 +00:00
|
|
|
BLOCKS = blocksTag;
|
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
|
|
|
for (Map entry : startGameItems) {
|
2019-11-06 00:55:59 +00:00
|
|
|
ITEMS.add(new StartGamePacket.ItemEntry((String) entry.get("name"), (short) ((int) entry.get("id"))));
|
2019-07-17 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 02:34:51 +00:00
|
|
|
InputStream itemStream = Toolbox.class.getClassLoader().getResourceAsStream("mappings/items.json");
|
2019-10-10 00:11:50 +00:00
|
|
|
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-10 00:11:50 +00:00
|
|
|
int itemIndex = 0;
|
|
|
|
for (Map.Entry<String, Map<String, Object>> itemEntry : items.entrySet()) {
|
2019-11-06 00:55:59 +00:00
|
|
|
ITEM_ENTRIES.put(itemIndex, new ItemEntry(itemEntry.getKey(), itemIndex, (int) itemEntry.getValue().get("bedrock_id"), (int) itemEntry.getValue().get("bedrock_data")));
|
2019-10-10 00:11:50 +00:00
|
|
|
itemIndex++;
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
2019-07-31 19:59:23 +00:00
|
|
|
|
2019-11-30 02:34:51 +00:00
|
|
|
InputStream blockStream = Toolbox.class.getClassLoader().getResourceAsStream("mappings/blocks.json");
|
2019-10-10 00:11:50 +00:00
|
|
|
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-12-21 05:05:20 +00:00
|
|
|
int javaIndex = -1;
|
|
|
|
javaLoop:
|
|
|
|
for (Map.Entry<String, Map<String, Object>> javaEntry : blocks.entrySet()) {
|
|
|
|
javaIndex++;
|
|
|
|
String wantedIdentifier = (String) javaEntry.getValue().get("bedrock_identifier");
|
|
|
|
Map<String, Object> wantedStates = (Map<String, Object>) 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<String, Tag<?>> bedrockStates = blockTag.getAsCompound("states").getValue();
|
|
|
|
for (Map.Entry<String, Object> stateEntry : wantedStates.entrySet()) {
|
|
|
|
Tag<?> bedrockStateTag = bedrockStates.get(stateEntry.getKey());
|
|
|
|
if (bedrockStateTag == null)
|
|
|
|
continue bedrockLoop;
|
|
|
|
Object bedrockStateValue = bedrockStateTag.getValue();
|
|
|
|
if (bedrockStateValue instanceof Byte)
|
2020-01-09 04:48:03 +00:00
|
|
|
bedrockStateValue = ((Byte) bedrockStateValue) != 0;
|
2019-12-21 05:05:20 +00:00
|
|
|
if (!stateEntry.getValue().equals(bedrockStateValue))
|
|
|
|
continue bedrockLoop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BlockEntry blockEntry = new BlockEntry(javaEntry.getKey(), javaIndex, bedrockIndex);
|
|
|
|
BLOCK_ENTRIES.put(javaIndex, blockEntry);
|
|
|
|
continue javaLoop;
|
|
|
|
}
|
2019-10-10 00:11:50 +00:00
|
|
|
}
|
2019-12-21 05:05:20 +00:00
|
|
|
GeyserLogger.DEFAULT.debug("Mapping " + javaEntry.getKey() + " was not found for bedrock edition!");
|
2019-10-10 00:11:50 +00:00
|
|
|
}
|
2019-08-07 23:08:48 +00:00
|
|
|
|
2019-11-10 02:20:47 +00:00
|
|
|
InputStream creativeItemStream = Toolbox.class.getClassLoader().getResourceAsStream("bedrock/creative_items.json");
|
|
|
|
ObjectMapper creativeItemMapper = new ObjectMapper();
|
2020-01-31 01:05:57 +00:00
|
|
|
JsonNode creativeItemEntries;
|
2019-11-10 02:20:47 +00:00
|
|
|
|
|
|
|
try {
|
2020-01-31 01:05:57 +00:00
|
|
|
creativeItemEntries = creativeItemMapper.readTree(creativeItemStream).get("items");
|
2019-11-10 02:20:47 +00:00
|
|
|
} catch (Exception e) {
|
2020-01-31 01:05:57 +00:00
|
|
|
throw new AssertionError("Unable to load creative items", e);
|
2019-11-10 02:20:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
List<ItemData> creativeItems = new ArrayList<>();
|
2020-01-31 01:05:57 +00:00
|
|
|
for (JsonNode itemNode : creativeItemEntries) {
|
2019-11-10 02:20:47 +00:00
|
|
|
short damage = 0;
|
2020-01-31 01:05:57 +00:00
|
|
|
if (itemNode.has("damage")) {
|
|
|
|
damage = itemNode.get("damage").numberValue().shortValue();
|
2019-11-10 02:20:47 +00:00
|
|
|
}
|
2020-01-31 01:05:57 +00:00
|
|
|
if (itemNode.has("nbt_b64")) {
|
|
|
|
byte[] bytes = Base64.getDecoder().decode(itemNode.get("nbt_b64").asText());
|
2019-11-10 02:20:47 +00:00
|
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
|
|
|
|
try {
|
|
|
|
com.nukkitx.nbt.tag.CompoundTag tag = (com.nukkitx.nbt.tag.CompoundTag) NbtUtils.createReaderLE(bais).readTag();
|
2020-01-31 01:05:57 +00:00
|
|
|
creativeItems.add(ItemData.of(itemNode.get("id").asInt(), damage, 1, tag));
|
2019-11-10 02:20:47 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-31 01:05:57 +00:00
|
|
|
creativeItems.add(ItemData.of(itemNode.get("id").asInt(), damage, 1));
|
2019-11-10 02:20:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CREATIVE_ITEMS = creativeItems.toArray(new ItemData[0]);
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
|
|
|
}
|