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;
|
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
2019-07-11 22:39:28 +00:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2019-10-31 02:14:23 +00:00
|
|
|
import com.nukkitx.nbt.NbtUtils;
|
|
|
|
import com.nukkitx.nbt.stream.NBTInputStream;
|
|
|
|
import com.nukkitx.nbt.tag.CompoundTag;
|
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-12-21 17:35:48 +00:00
|
|
|
|
2019-10-31 02:14:23 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2019-10-10 00:11:50 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.ItemEntry;
|
2019-07-11 22:39:28 +00:00
|
|
|
|
2020-04-05 01:37:39 +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-12-30 23:33:27 +00:00
|
|
|
public static final ObjectMapper JSON_MAPPER = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
|
2020-02-09 22:55:07 +00:00
|
|
|
public static final CompoundTag BIOMES;
|
2019-12-30 23:33:27 +00:00
|
|
|
|
2020-03-18 21:10:28 +00:00
|
|
|
public static final List<StartGamePacket.ItemEntry> ITEMS = new ArrayList<>();
|
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<>();
|
2019-10-10 00:11:50 +00:00
|
|
|
|
2020-04-05 09:13:47 +00:00
|
|
|
public static final Map<String, Map<String, String>> LOCALE_MAPPINGS = new HashMap<>();
|
2020-04-05 01:58:23 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
static {
|
2020-02-09 22:53:57 +00:00
|
|
|
/* Load biomes */
|
2020-02-06 03:02:23 +00:00
|
|
|
InputStream biomestream = GeyserConnector.class.getClassLoader().getResourceAsStream("bedrock/biome_definitions.dat");
|
|
|
|
if (biomestream == null) {
|
|
|
|
throw new AssertionError("Unable to find bedrock/biome_definitions.dat");
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 03:02:23 +00:00
|
|
|
CompoundTag biomesTag;
|
2019-08-10 03:16:34 +00:00
|
|
|
|
2020-02-06 03:02:23 +00:00
|
|
|
try (NBTInputStream biomenbtInputStream = NbtUtils.createNetworkReader(biomestream)){
|
|
|
|
biomesTag = (CompoundTag) biomenbtInputStream.readTag();
|
|
|
|
BIOMES = biomesTag;
|
2019-10-31 02:14:23 +00:00
|
|
|
} catch (Exception ex) {
|
2020-02-16 19:25:37 +00:00
|
|
|
GeyserConnector.getInstance().getLogger().warning("Failed to get biomes from biome definitions, is there something wrong with the file?");
|
2019-10-31 02:14:23 +00:00
|
|
|
throw new AssertionError(ex);
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
/* Load item palette */
|
2019-12-31 00:14:38 +00:00
|
|
|
InputStream stream = getResource("bedrock/items.json");
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
TypeReference<List<JsonNode>> itemEntriesType = new TypeReference<List<JsonNode>>() {
|
|
|
|
};
|
2019-07-17 01:05:10 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
List<JsonNode> itemEntries;
|
2019-07-17 01:05:10 +00:00
|
|
|
try {
|
2019-12-30 23:33:27 +00:00
|
|
|
itemEntries = JSON_MAPPER.readValue(stream, itemEntriesType);
|
2019-07-17 01:05:10 +00:00
|
|
|
} catch (Exception e) {
|
2019-12-30 23:33:27 +00:00
|
|
|
throw new AssertionError("Unable to load Bedrock runtime item IDs", e);
|
2019-07-17 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
for (JsonNode entry : itemEntries) {
|
|
|
|
ITEMS.add(new StartGamePacket.ItemEntry(entry.get("name").textValue(), (short) entry.get("id").intValue()));
|
2019-07-17 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
stream = getResource("mappings/items.json");
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
JsonNode items;
|
2019-07-29 22:20:48 +00:00
|
|
|
try {
|
2019-12-30 23:33:27 +00:00
|
|
|
items = JSON_MAPPER.readTree(stream);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new AssertionError("Unable to load Java runtime item IDs", e);
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
int itemIndex = 0;
|
2019-12-30 23:33:27 +00:00
|
|
|
Iterator<Map.Entry<String, JsonNode>> iterator = items.fields();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
Map.Entry<String, JsonNode> entry = iterator.next();
|
|
|
|
ITEM_ENTRIES.put(itemIndex, new ItemEntry(entry.getKey(), itemIndex,
|
|
|
|
entry.getValue().get("bedrock_id").intValue(), entry.getValue().get("bedrock_data").intValue()));
|
2019-10-10 00:11:50 +00:00
|
|
|
itemIndex++;
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
2020-04-05 01:58:23 +00:00
|
|
|
|
|
|
|
/* Load the language mappings */
|
|
|
|
stream = Toolbox.getResource("mappings/locales.json");
|
|
|
|
JsonNode locales;
|
|
|
|
try {
|
|
|
|
locales = Toolbox.JSON_MAPPER.readTree(stream);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new AssertionError("Unable to load Java locale list", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (JsonNode localeNode : locales.get("locales")) {
|
|
|
|
String currentLocale = localeNode.asText();
|
|
|
|
|
|
|
|
InputStream localeStream = Toolbox.getResource("mappings/lang/" + currentLocale + ".json");
|
|
|
|
JsonNode locale;
|
|
|
|
try {
|
|
|
|
locale = Toolbox.JSON_MAPPER.readTree(localeStream);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new AssertionError("Unable to load Java lang map for " + currentLocale, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator<Map.Entry<String, JsonNode>> localeIterator = locale.fields();
|
2020-04-05 09:13:47 +00:00
|
|
|
Map<String, String> langMap = new HashMap<>();
|
2020-04-05 01:58:23 +00:00
|
|
|
while (localeIterator.hasNext()) {
|
|
|
|
Map.Entry<String, JsonNode> entry = localeIterator.next();
|
|
|
|
langMap.put(entry.getKey(), entry.getValue().asText());
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCALE_MAPPINGS.put(currentLocale.toLowerCase(), langMap);
|
|
|
|
}
|
2019-12-30 23:33:27 +00:00
|
|
|
}
|
2019-07-31 19:59:23 +00:00
|
|
|
|
2019-12-31 00:14:38 +00:00
|
|
|
public static InputStream getResource(String resource) {
|
2019-12-30 23:33:27 +00:00
|
|
|
InputStream stream = Toolbox.class.getClassLoader().getResourceAsStream(resource);
|
|
|
|
if (stream == null) {
|
|
|
|
throw new AssertionError("Unable to find resource: " + resource);
|
2019-08-07 23:08:48 +00:00
|
|
|
}
|
2019-12-30 23:33:27 +00:00
|
|
|
return stream;
|
|
|
|
}
|
2019-08-07 23:08:48 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
public static void init() {
|
|
|
|
// no-op
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
|
|
|
}
|