3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-11-20 06:50:09 +01:00

Code cleanups and update ItemTranslator to work with new mappings

Dieser Commit ist enthalten in:
RednedEpic 2019-07-31 14:59:23 -05:00
Ursprung 8c2897dbec
Commit 3784ba7baa
8 geänderte Dateien mit 148 neuen und 209 gelöschten Zeilen

Datei anzeigen

@ -43,12 +43,6 @@
<version>1.0.0</version> <version>1.0.0</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats</artifactId>
<version>1.5</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>

Datei anzeigen

@ -4,7 +4,7 @@ import lombok.Getter;
@Getter @Getter
public class CommonItem { public class CommonItem {
JavaItem java;
BedrockItem bedrock; private JavaItem java;
private BedrockItem bedrock;
} }

Datei anzeigen

@ -1,26 +1,30 @@
package org.geysermc.connector.network.translators.item; package org.geysermc.connector.network.translators.item;
import lombok.Getter;
public enum DyeColor { public enum DyeColor {
white,
orange,
magenta,
light_blue,
yellow,
lime,
pink,
gray,
light_gray,
cyan,
purple,
blue,
brown,
green,
red,
black;
public final int id; WHITE,
ORANGE,
MAGENTA,
LIGHT_BLUE,
YELLOW,
LIME,
PINK,
GRAY,
LIGHT_GRAY,
CYAN,
PURPLE,
BLUE,
BROWN,
GREEN,
RED,
BLACK;
DyeColor() { @Getter
this.id = ordinal(); private final int id = ordinal();
public String getName() {
return name().toLowerCase();
} }
} }

Datei anzeigen

@ -40,7 +40,7 @@ import com.github.steveice10.opennbt.tag.builtin.ShortTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag; import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.nukkitx.protocol.bedrock.data.ItemData; import com.nukkitx.protocol.bedrock.data.ItemData;
import com.nukkitx.protocol.bedrock.packet.StartGamePacket; import org.geysermc.connector.utils.Remapper;
import org.geysermc.connector.utils.Toolbox; import org.geysermc.connector.utils.Toolbox;
import java.util.ArrayList; import java.util.ArrayList;
@ -50,68 +50,6 @@ import java.util.Map;
public class ItemTranslator { public class ItemTranslator {
private static Map<String, String> identifiers = new HashMap<String, String>();
static {
// Key: java translation
// Value: bedrock translation
identifiers.put("grass_block", "grass");
identifiers.put("granite", "stone:1");
identifiers.put("polished_granite", "stone:2");
identifiers.put("diorite", "stone:3");
identifiers.put("polished_diorite", "stone:4");
identifiers.put("andesite", "stone:5");
identifiers.put("polished_andesite", "stone:6");
identifiers.put("oak_log", "log");
identifiers.put("spruce_log", "log:1");
identifiers.put("birch_log", "log:2");
identifiers.put("jungle_log", "log:3");
identifiers.put("acacia_log", "log:4");
identifiers.put("dark_oak_log", "log:5");
identifiers.put("oak_planks", "planks");
identifiers.put("spruce_planks", "planks:1");
identifiers.put("birch_planks", "planks:2");
identifiers.put("jungle_planks", "planks:3");
identifiers.put("acacia_planks", "planks:4");
identifiers.put("dark_oak_planks", "planks:5");
identifiers.put("white_wool", "wool");
identifiers.put("orange_wool", "wool:1");
identifiers.put("magenta_wool", "wool:2");
identifiers.put("light_blue_wool", "wool:3");
identifiers.put("yellow_wool", "wool:4");
identifiers.put("lime_wool", "wool:5");
identifiers.put("pink_wool", "wool:6");
identifiers.put("gray_wool", "wool:7");
identifiers.put("light_gray_wool", "wool:8");
identifiers.put("cyan_wool", "wool:9");
identifiers.put("purple_wool", "wool:10");
identifiers.put("blue_wool", "wool:11");
identifiers.put("brown_wool", "wool:12");
identifiers.put("green_wool", "wool:13");
identifiers.put("red_wool", "wool:14");
identifiers.put("black_wool", "wool:15");
identifiers.put("white_carpet", "carpet");
identifiers.put("orange_carpet", "carpet:1");
identifiers.put("magenta_carpet", "carpet:2");
identifiers.put("light_blue_carpet", "carpet:3");
identifiers.put("yellow_carpet", "carpet:4");
identifiers.put("lime_carpet", "carpet:5");
identifiers.put("pink_carpet", "carpet:6");
identifiers.put("gray_carpet", "carpet:7");
identifiers.put("light_gray_carpet", "carpet:8");
identifiers.put("cyan_carpet", "carpet:9");
identifiers.put("purple_carpet", "carpet:10");
identifiers.put("blue_carpet", "carpet:11");
identifiers.put("brown_carpet", "carpet:12");
identifiers.put("green_carpet", "carpet:13");
identifiers.put("red_carpet", "carpet:14");
identifiers.put("black_carpet", "carpet:15");
}
public ItemStack translateToJava(ItemData data) { public ItemStack translateToJava(ItemData data) {
JavaItem javaItem = getJavaItem(data); JavaItem javaItem = getJavaItem(data);
@ -140,7 +78,7 @@ public class ItemTranslator {
continue; continue;
JavaItem javaItem = javaItems.getValue(); JavaItem javaItem = javaItems.getValue();
String identifier = getBedrockIdentifier(javaItem.getIdentifier().replace("minecraft:", "")); String identifier = getBedrockIdentifier(javaItem.getIdentifier());
if (!Toolbox.BEDROCK_ITEMS.containsKey(identifier)) if (!Toolbox.BEDROCK_ITEMS.containsKey(identifier))
continue; continue;
@ -155,7 +93,7 @@ public class ItemTranslator {
if (bedrockItems.getValue().getId() != data.getId()) if (bedrockItems.getValue().getId() != data.getId())
continue; continue;
String identifier = getJavaIdentifier(bedrockItems.getKey().replace("minecraft:", "")); String identifier = getJavaIdentifier(bedrockItems.getKey(), data.getDamage());
if (!Toolbox.JAVA_ITEMS.containsKey(identifier)) if (!Toolbox.JAVA_ITEMS.containsKey(identifier))
continue; continue;
@ -166,20 +104,23 @@ public class ItemTranslator {
} }
public String getBedrockIdentifier(String javaIdentifier) { public String getBedrockIdentifier(String javaIdentifier) {
if (!identifiers.containsKey(javaIdentifier)) if (!Remapper.JAVA_TO_BEDROCK.containsKey(javaIdentifier)) {
return "minecraft:" + javaIdentifier; return javaIdentifier;
return "minecraft:" + identifiers.get(javaIdentifier);
} }
public String getJavaIdentifier(String bedrockIdentifier) { if ((int) Remapper.JAVA_TO_BEDROCK.get(javaIdentifier).get("data") > 0) {
for (Map.Entry<String, String> entry : identifiers.entrySet()) { return Remapper.JAVA_TO_BEDROCK.get(javaIdentifier).get("name") + ":" + Remapper.JAVA_TO_BEDROCK.get(javaIdentifier).get("data");
if (entry.getValue().equals(bedrockIdentifier)) {
return "minecraft:" + entry.getKey();
}
} }
return "minecraft:" + bedrockIdentifier; return (String) Remapper.JAVA_TO_BEDROCK.get(javaIdentifier).get("name");
}
public String getJavaIdentifier(String bedrockIdentifier, int data) {
if (!Remapper.BEDROCK_TO_JAVA.containsKey(bedrockIdentifier)) {
return bedrockIdentifier;
}
return Remapper.BEDROCK_TO_JAVA.get(bedrockIdentifier).get(data);
} }
private CompoundTag translateToJavaNBT(com.nukkitx.nbt.tag.CompoundTag tag) { private CompoundTag translateToJavaNBT(com.nukkitx.nbt.tag.CompoundTag tag) {

Datei anzeigen

@ -1,24 +1,21 @@
package org.geysermc.connector.network.translators.item; package org.geysermc.connector.network.translators.item;
import lombok.Getter;
public enum StoneType { public enum StoneType {
stone,
granite,
polished_granite,
diorite,
polished_diorite,
andesite,
polished_andesite;
public final String name; STONE,
public final int id; GRANITE,
POLISHED_GRANITE,
DIORITE,
POLISHED_DIORITE,
ANDESITE,
POLISHED_ANDESITE;
StoneType(String name) { @Getter
this.id = ordinal(); private final int id = ordinal();
this.name = name;
}
StoneType() { public String getName() {
this.name = name(); return name().toLowerCase();
this.id = ordinal();
} }
} }

Datei anzeigen

@ -1,12 +1,20 @@
package org.geysermc.connector.network.translators.item; package org.geysermc.connector.network.translators.item;
public enum WoodType { import lombok.Getter;
oak,
spruce,
birch,
jungle,
acacia,
dark_oak;
public final int id = ordinal(); public enum WoodType {
OAK,
SPRUCE,
BIRCH,
JUNGLE,
ACACIA,
DARK_OAK;
@Getter
private final int id = ordinal();
public String getName() {
return name().toLowerCase();
}
} }

Datei anzeigen

@ -3,98 +3,89 @@ package org.geysermc.connector.utils;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.ObjectWriter;
import org.geysermc.api.events.Listener; import org.geysermc.connector.network.translators.item.BedrockItem;
import org.geysermc.connector.network.translators.item.*; import org.geysermc.connector.network.translators.item.DyeColor;
import org.geysermc.connector.network.translators.item.JavaItem;
import org.geysermc.connector.network.translators.item.StoneType;
import org.geysermc.connector.network.translators.item.WoodType;
import java.io.File; import java.io.File;
import java.util.*; import java.util.*;
public class Remapper { public class Remapper {
private static List<String> specials = new ArrayList<>(); private static List<String> specials = new ArrayList<>();
//Method to convert java to bedrock public static Map<String, Map<Integer, String>> BEDROCK_TO_JAVA = new HashMap<>();
static void convert(Map<String, BedrockItem> items1, Map<String, JavaItem> java) { public static Map<String, Map<String, Object>> JAVA_TO_BEDROCK = new HashMap<>();
for(StoneType type : StoneType.values()) { // Method to convert java to bedrock
specials.add(type.name); public static void addConversions(Map<String, BedrockItem> items1, Map<String, JavaItem> java) {
for (StoneType type : StoneType.values()) {
specials.add(type.getName());
} }
Map<JavaItem, List<BedrockItem>> convertions = new HashMap<>(); Map<JavaItem, List<BedrockItem>> convertions = new HashMap<>();
for(Map.Entry<String, JavaItem> entry2 : java.entrySet()) { for (Map.Entry<String, JavaItem> javaItem : java.entrySet()) {
for (Map.Entry<String, BedrockItem> entry1 : items1.entrySet()) { for (Map.Entry<String, BedrockItem> bedrockItem : items1.entrySet()) {
if (!tool(entry2.getValue().getIdentifier())) { if (tool(javaItem.getValue().getIdentifier()))
if (entry2.getKey().contains("white_")) { continue;
String stripped = entry2.getKey().replaceAll("white_", "").replaceAll("terracotta", "stained_hardened_clay");
if (stripped.equalsIgnoreCase(entry1.getKey()) && entry1.getValue().getData() == 0) { if (javaItem.getKey().contains("white_")) {
String stripped = javaItem.getKey().replaceAll("white_", "").replaceAll("terracotta", "stained_hardened_clay");
if (!stripped.equalsIgnoreCase(bedrockItem.getKey()) || bedrockItem.getValue().getData() != 0)
continue;
for (DyeColor dyeColor : DyeColor.values()) { for (DyeColor dyeColor : DyeColor.values()) {
JavaItem j = java.get(entry2.getValue().getIdentifier().replaceAll("white_", dyeColor.name() + "_")); JavaItem j = java.get(javaItem.getValue().getIdentifier().replaceAll("white_", dyeColor.getName() + "_"));
convertions.computeIfAbsent(j, (x) -> new ArrayList<>()); convertions.computeIfAbsent(j, (x) -> new ArrayList<>());
convertions.get(j).add(new BedrockItem(bedrockItem.getValue().getIdentifier(), bedrockItem.getValue().getId(), dyeColor.getId()));
}
} else if (javaItem.getKey().contains("oak_")) {
String stripped = javaItem.getKey().replaceAll("oak_", "").replaceAll("terracotta", "stained_hardened_clay");
if (!stripped.equalsIgnoreCase(bedrockItem.getKey()) || bedrockItem.getValue().getData() != 0)
continue;
convertions.get(j).add(new BedrockItem(entry1.getValue().getIdentifier(), entry1.getValue().getId(), dyeColor.id));
}
}
} else if (entry2.getKey().contains("oak_")) {
String stripped = entry2.getKey().replaceAll("oak_", "").replaceAll("terracotta", "stained_hardened_clay");
if (stripped.equalsIgnoreCase(entry1.getKey()) && entry1.getValue().getData() == 0) {
for (WoodType woodType : WoodType.values()) { for (WoodType woodType : WoodType.values()) {
JavaItem j = java.get(entry2.getValue().getIdentifier().replaceAll("oak_", woodType.name() + "_")); JavaItem j = java.get(javaItem.getValue().getIdentifier().replaceAll("oak_", woodType.getName() + "_"));
convertions.computeIfAbsent(j, (x) -> new ArrayList<>()); convertions.computeIfAbsent(j, (x) -> new ArrayList<>());
convertions.get(j).add(new BedrockItem(bedrockItem.getValue().getIdentifier(), bedrockItem.getValue().getId(), woodType.getId()));
}
} else if (!javaItem.getKey().contains("stairs")) {
if (!javaItem.getKey().startsWith("minecraft:stone_") || !bedrockItem.getValue().getIdentifier().equalsIgnoreCase(javaItem.getKey().replace("stone_", "")) || bedrockItem.getValue().getData() != 0)
continue;
convertions.get(j).add(new BedrockItem(entry1.getValue().getIdentifier(), entry1.getValue().getId(), woodType.id));
}
}
} else if (!entry2.getKey().contains("stairs")) {
if (entry2.getKey().startsWith("minecraft:stone_") && entry1.getValue().getIdentifier().equalsIgnoreCase(entry2.getKey().replace("stone_", "")) && entry1.getValue().getData() == 0) {
for (StoneType stoneType : StoneType.values()) { for (StoneType stoneType : StoneType.values()) {
JavaItem j = java.get(entry2.getValue().getIdentifier().replaceAll("stone_", stoneType.name() + "_")); JavaItem j = java.get(javaItem.getValue().getIdentifier().replaceAll("stone_", stoneType.getName() + "_"));
convertions.computeIfAbsent(j, (x) -> new ArrayList<>()); convertions.computeIfAbsent(j, (x) -> new ArrayList<>());
convertions.get(j).add(new BedrockItem(bedrockItem.getValue().getIdentifier(), bedrockItem.getValue().getId(), stoneType.getId()));
convertions.get(j).add(new BedrockItem(entry1.getValue().getIdentifier(), entry1.getValue().getId(), stoneType.id));
} }
} else if (entry2.getKey().equalsIgnoreCase("minecraft:stone") && entry1.getKey().equalsIgnoreCase("minecraft:stone")) { } else if (javaItem.getKey().equalsIgnoreCase("minecraft:stone") && bedrockItem.getKey().equalsIgnoreCase("minecraft:stone")) {
for (StoneType stoneType : StoneType.values()) { for (StoneType stoneType : StoneType.values()) {
JavaItem j = java.get(entry2.getValue().getIdentifier().replaceAll("stone", stoneType.name())); JavaItem j = java.get(javaItem.getValue().getIdentifier().replaceAll("stone", stoneType.getName()));
convertions.computeIfAbsent(j, (x) -> new ArrayList<>()); convertions.computeIfAbsent(j, (x) -> new ArrayList<>());
convertions.get(j).add(new BedrockItem(bedrockItem.getValue().getIdentifier(), bedrockItem.getValue().getId(), stoneType.getId()));
convertions.get(j).add(new BedrockItem(entry1.getValue().getIdentifier(), entry1.getValue().getId(), stoneType.id));
} }
} } else if (bedrockItem.getValue().getIdentifier().equalsIgnoreCase(javaItem.getKey()) && notSpecial(javaItem.getKey())) {
} else if (entry1.getValue().getIdentifier().equalsIgnoreCase(entry2.getKey()) && notSpecial(entry2.getKey())) { JavaItem j = javaItem.getValue();
JavaItem j = entry2.getValue();
convertions.computeIfAbsent(j, (x) -> new ArrayList<>()); convertions.computeIfAbsent(j, (x) -> new ArrayList<>());
convertions.get(j).add(bedrockItem.getValue());
convertions.get(j).add(entry1.getValue());
}
} }
} }
} }
//for(BedrockItem item : ) //for(BedrockItem item : )
for(DyeColor dyeColor : DyeColor.values()) { for (DyeColor dyeColor : DyeColor.values()) {
JavaItem j = java.get("minecraft:white_shulker_box".replaceAll("white_", dyeColor.name() + "_")); JavaItem j = java.get("minecraft:white_shulker_box".replaceAll("white_", dyeColor.getName() + "_"));
// System.out.println(j.getIdentifier() + " " + convertions.get(j).get(0).getIdentifier() + ":" + convertions.get(j).get(0).getData());
System.out.println(j.getIdentifier() + " " + convertions.get(j).get(0).getIdentifier() + ":" + convertions.get(j).get(0).getData());
} }
for (Map.Entry<JavaItem, List<BedrockItem>> entry : convertions.entrySet()) {
Map<String, Map<Integer, String>> BEDROCK_TO_JAVA = new HashMap<>(); for (BedrockItem item : entry.getValue()) {
Map<String, Map<String, Object>> JAVA_TO_BEDROCK = new HashMap<>();
for(Map.Entry<JavaItem, List<BedrockItem>> entry : convertions.entrySet()) {
for(BedrockItem item : entry.getValue()) {
if(entry.getKey().getIdentifier().contains("shul")) {
System.out.println(entry.getKey().getIdentifier());
}
Objects.requireNonNull(entry.getKey(), item.getIdentifier());
JAVA_TO_BEDROCK.computeIfAbsent(entry.getKey().getIdentifier(), (x) -> new HashMap<>()); JAVA_TO_BEDROCK.computeIfAbsent(entry.getKey().getIdentifier(), (x) -> new HashMap<>());
BEDROCK_TO_JAVA.computeIfAbsent(item.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<String, Object> map = JAVA_TO_BEDROCK.get(entry.getKey().getIdentifier());
@ -107,11 +98,15 @@ public class Remapper {
} }
} }
// Uncomment this for updated mappings
// writeMappings();
}
private static void writeMappings() {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
try { try {
writer.writeValue(new File("java_to_bedrock.json"), JAVA_TO_BEDROCK); writer.writeValue(new File("java_to_bedrock.json"), JAVA_TO_BEDROCK);
writer.writeValue(new File("bedrock_to_java.json"), BEDROCK_TO_JAVA); writer.writeValue(new File("bedrock_to_java.json"), BEDROCK_TO_JAVA);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -119,8 +114,8 @@ public class Remapper {
} }
private static boolean notSpecial(String key) { private static boolean notSpecial(String key) {
for(String spec : specials) { for (String spec : specials) {
if(key.contains(spec)) { if (key.contains(spec)) {
return false; return false;
} }
} }

Datei anzeigen

@ -93,8 +93,8 @@ public class Toolbox {
for (String str : javaItemList.keySet()) { for (String str : javaItemList.keySet()) {
javaItems.put(str, new JavaItem(str, (int) javaItemList.get(str).get("protocol_id"))); javaItems.put(str, new JavaItem(str, (int) javaItemList.get(str).get("protocol_id")));
} }
//Uncomment when you need new re-mappings!
//Remapper.convert(bedrockItems, javaItems); Remapper.addConversions(bedrockItems, javaItems);
JAVA_ITEMS = javaItems; JAVA_ITEMS = javaItems;
} }