2019-07-11 22:39:28 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
2019-07-29 22:20:48 +00:00
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
2019-07-30 02:25:13 +00:00
|
|
|
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
|
2019-07-11 22:39:28 +00:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2019-07-30 02:25:13 +00:00
|
|
|
import com.fasterxml.jackson.databind.ObjectWriter;
|
2019-07-11 22:39:28 +00:00
|
|
|
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;
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
2019-07-30 02:25:13 +00:00
|
|
|
import org.apache.logging.log4j.core.util.Patterns;
|
2019-07-29 22:20:48 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.BedrockItem;
|
2019-07-30 02:25:13 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.DyeColor;
|
2019-07-29 22:20:48 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.JavaItem;
|
2019-07-11 22:39:28 +00:00
|
|
|
|
2019-07-30 02:25:13 +00:00
|
|
|
import java.io.File;
|
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-30 02:25:13 +00:00
|
|
|
import java.util.regex.Pattern;
|
2019-07-11 22:39:28 +00:00
|
|
|
|
|
|
|
public class Toolbox {
|
|
|
|
|
2019-07-21 22:22:56 +00:00
|
|
|
static {
|
2019-07-11 22:39:28 +00:00
|
|
|
InputStream stream = Toolbox.class.getClassLoader().getResourceAsStream("cached_pallete.json");
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
ArrayList<LinkedHashMap<String, Object>> entries = new ArrayList<>();
|
|
|
|
|
|
|
|
try {
|
|
|
|
entries = mapper.readValue(stream, ArrayList.class);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2019-07-30 02:25:13 +00:00
|
|
|
Map<String, BedrockItem> bedrockItems = new HashMap<>();
|
2019-07-29 22:20:48 +00:00
|
|
|
for (Map<String, Object> e : entries) {
|
|
|
|
BedrockItem bedrockItem = new BedrockItem((String) e.get("name"), (int) e.get("id"), (int) e.get("data"));
|
|
|
|
if (bedrockItem.getData() != 0) {
|
|
|
|
bedrockItems.put(bedrockItem.getIdentifier() + ":" + bedrockItem.getData(), bedrockItem);
|
|
|
|
} else {
|
|
|
|
bedrockItems.put(bedrockItem.getIdentifier(), bedrockItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 02:25:13 +00:00
|
|
|
|
2019-07-11 22:39:28 +00:00
|
|
|
ByteBuf b = Unpooled.buffer();
|
2019-07-20 22:35:14 +00:00
|
|
|
VarInts.writeUnsignedInt(b, entries.size());
|
2019-07-11 22:39:28 +00:00
|
|
|
for (Map<String, Object> e : entries) {
|
|
|
|
BedrockUtils.writeString(b, (String) e.get("name"));
|
2019-07-20 22:35:14 +00:00
|
|
|
b.writeShortLE((int) e.get("data"));
|
|
|
|
b.writeShortLE((int) e.get("id"));
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CACHED_PALLETE = b;
|
|
|
|
|
2019-07-17 01:05:10 +00:00
|
|
|
InputStream stream2 = Toolbox.class.getClassLoader().getResourceAsStream("items.json");
|
|
|
|
if (stream2 == null) {
|
|
|
|
throw new AssertionError("Items Table not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectMapper mapper2 = new ObjectMapper();
|
|
|
|
ArrayList<HashMap> s = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
s = mapper2.readValue(stream2, ArrayList.class);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2019-07-29 22:20:48 +00:00
|
|
|
List<StartGamePacket.ItemEntry> l = new ArrayList<>();
|
2019-07-21 22:22:56 +00:00
|
|
|
for (HashMap e : s) {
|
2019-07-20 22:35:14 +00:00
|
|
|
l.add(new StartGamePacket.ItemEntry((String) e.get("name"), (short) ((int) e.get("id"))));
|
2019-07-29 22:20:48 +00:00
|
|
|
if (!bedrockItems.containsKey(e.get("name"))) {
|
|
|
|
BedrockItem bedrockItem = new BedrockItem((String) e.get("name"), ((int) e.get("id")), 0);
|
|
|
|
bedrockItems.put(bedrockItem.getIdentifier(), bedrockItem);
|
|
|
|
}
|
2019-07-17 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ITEMS = l;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
|
|
|
BEDROCK_ITEMS = bedrockItems;
|
|
|
|
|
|
|
|
InputStream javaItemStream = Toolbox.class.getClassLoader().getResourceAsStream("java_items.json");
|
|
|
|
ObjectMapper javaItemMapper = new ObjectMapper();
|
2019-07-30 02:25:13 +00:00
|
|
|
Map<String, HashMap> javaItemList = new HashMap<>();
|
2019-07-29 22:20:48 +00:00
|
|
|
try {
|
|
|
|
javaItemList = javaItemMapper.readValue(javaItemStream, new TypeReference<Map<String, HashMap>>(){});
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, JavaItem> javaItems = new HashMap<String, JavaItem>();
|
|
|
|
|
|
|
|
for (String str : javaItemList.keySet()) {
|
|
|
|
javaItems.put(str, new JavaItem(str, (int) javaItemList.get(str).get("protocol_id")));
|
|
|
|
}
|
2019-07-30 02:25:13 +00:00
|
|
|
//Uncomment when you need new re-mappings!
|
|
|
|
//convert(bedrockItems, javaItems);
|
2019-07-29 22:20:48 +00:00
|
|
|
|
|
|
|
JAVA_ITEMS = javaItems;
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 02:25:13 +00:00
|
|
|
//Method to convert java to bedrock
|
2019-07-30 12:38:37 +00:00
|
|
|
/*private static void convert(Map<String, BedrockItem> items1, Map<String, JavaItem> java) {
|
2019-07-30 02:25:13 +00:00
|
|
|
Map<JavaItem, List<BedrockItem>> convertions = new HashMap<>();
|
|
|
|
|
|
|
|
for(Map.Entry<String, JavaItem> entry2 : java.entrySet()) {
|
|
|
|
for (Map.Entry<String, BedrockItem> entry1 : items1.entrySet()) {
|
|
|
|
|
|
|
|
if (entry1.getValue().getIdentifier().equalsIgnoreCase(entry2.getKey())) {
|
|
|
|
JavaItem j = entry2.getValue();
|
|
|
|
|
|
|
|
convertions.computeIfAbsent(j, (x) -> new ArrayList<>());
|
|
|
|
|
|
|
|
convertions.get(j).add(entry1.getValue());
|
|
|
|
} else {
|
|
|
|
if (entry2.getKey().contains("white_")) {
|
|
|
|
String stripped = entry2.getKey().replaceAll("white_", "").replaceAll("terracotta", "stained_hardened_clay");
|
|
|
|
if (stripped.equalsIgnoreCase(entry1.getKey()) && entry1.getValue().getData() == 0) {
|
|
|
|
for(DyeColor dyeColor : DyeColor.values()) {
|
|
|
|
JavaItem j = java.get(entry2.getValue().getIdentifier().replaceAll("white_", dyeColor.name() + "_"));
|
|
|
|
|
|
|
|
convertions.computeIfAbsent(j, (x) -> new ArrayList<>());
|
|
|
|
|
|
|
|
convertions.get(j).add(new BedrockItem(entry1.getValue().getIdentifier(), entry1.getValue().getId(), dyeColor.id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(DyeColor dyeColor : DyeColor.values()) {
|
|
|
|
JavaItem j = java.get("minecraft:white_wool".replaceAll("white_", dyeColor.name() + "_"));
|
|
|
|
|
|
|
|
System.out.println(j.getIdentifier() + " " + convertions.get(j).get(0).getIdentifier() + ":" + convertions.get(j).get(0).getData());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, Map<Integer, String>> BEDROCK_TO_JAVA = new HashMap<>();
|
|
|
|
|
|
|
|
Map<String, Map<String, Object>> JAVA_TO_BEDROCK = new HashMap<>();
|
|
|
|
|
|
|
|
for(Map.Entry<JavaItem, List<BedrockItem>> entry : convertions.entrySet()) {
|
|
|
|
|
|
|
|
for(BedrockItem item : entry.getValue()) {
|
|
|
|
JAVA_TO_BEDROCK.computeIfAbsent(entry.getKey().getIdentifier(), (x) -> new HashMap<>());
|
|
|
|
BEDROCK_TO_JAVA.computeIfAbsent(item.getIdentifier(), (x) -> new HashMap<>());
|
|
|
|
Map<String, Object> map = JAVA_TO_BEDROCK.get(entry.getKey().getIdentifier());
|
|
|
|
|
|
|
|
map.put("name", item.getIdentifier());
|
|
|
|
map.put("id", item.getId());
|
|
|
|
map.put("data", item.getData());
|
|
|
|
|
|
|
|
BEDROCK_TO_JAVA.get(item.getIdentifier()).put(item.getData(), entry.getKey().getIdentifier());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
|
|
|
|
try {
|
|
|
|
writer.writeValue(new File("java_to_bedrock.json"), JAVA_TO_BEDROCK);
|
|
|
|
|
|
|
|
writer.writeValue(new File("bedrock_to_java.json"), BEDROCK_TO_JAVA);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-07-30 12:38:37 +00:00
|
|
|
}*/
|
2019-07-30 02:25:13 +00:00
|
|
|
|
2019-07-17 01:05:10 +00:00
|
|
|
public static final Collection<StartGamePacket.ItemEntry> ITEMS;
|
|
|
|
|
2019-07-11 22:39:28 +00:00
|
|
|
public static final ByteBuf CACHED_PALLETE;
|
|
|
|
|
2019-07-29 22:20:48 +00:00
|
|
|
public static final Map<String, BedrockItem> BEDROCK_ITEMS;
|
|
|
|
public static final Map<String, JavaItem> JAVA_ITEMS;
|
|
|
|
|
2019-07-19 00:11:58 +00:00
|
|
|
//public static final byte[] EMPTY_CHUNK;
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|