Skip to content
This repository has been archived by the owner on Apr 7, 2021. It is now read-only.

Commit

Permalink
Begin cleaning up the item translator
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeeey committed Apr 25, 2020
1 parent 9da0afb commit 49628e5
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 118 deletions.
29 changes: 0 additions & 29 deletions .github/workflows/maven.yml

This file was deleted.

27 changes: 0 additions & 27 deletions .github/workflows/pullrequest.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.nukkitx.protocol.bedrock.data.ItemData;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import org.dragonet.proxy.DragonProxy;
Expand All @@ -17,7 +18,9 @@
import org.dragonet.proxy.network.translator.misc.item.ItemEntry;
import org.dragonet.proxy.network.translator.misc.item.nbt.DisplayNbtTranslator;
import org.dragonet.proxy.network.translator.misc.item.nbt.EnchantmentNbtTranslator;
import org.dragonet.proxy.network.translator.misc.item.nbt.ItemNbtTranslator;
import org.dragonet.proxy.util.registry.ItemRegisterInfo;
import org.dragonet.proxy.util.registry.MappingEntry;
import org.dragonet.proxy.util.registry.Registry;

import java.io.IOException;
Expand All @@ -29,6 +32,7 @@

@Log4j2
public class ItemTranslatorRegistry extends Registry {
private static List<ItemNbtTranslator> nbtTranslators = new ArrayList<>();
private static Int2ObjectMap<IItemTranslator> customTranslators = new Int2ObjectOpenHashMap<>(); // bedrock id

private static final Int2ObjectMap<ItemEntry> javaToBedrockMap = new Int2ObjectOpenHashMap<>();
Expand All @@ -38,39 +42,32 @@ public class ItemTranslatorRegistry extends Registry {


static {
registerType(ItemRegisterInfo.class);
registerPath("org.dragonet.proxy.network.translator.misc.item", (info, clazz) -> {
// Register custom item translators
registerPath("org.dragonet.proxy.network.translator.misc.item", ItemRegisterInfo.class, (info, clazz) -> {
try {
customTranslators.put(((ItemRegisterInfo) info).bedrockId(), (IItemTranslator) clazz.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
});
}

static {
InputStream stream = DragonProxy.class.getClassLoader().getResourceAsStream("mappings/1.15/item_mappings.json");
if (stream == null) {
throw new AssertionError("Item mapping table not found");
}

Map<String, ItemTranslatorRegistry.ItemMappingEntry> itemEntries;
try {
itemEntries = DragonProxy.JSON_MAPPER.readValue(stream, new TypeReference<Map<String, ItemTranslatorRegistry.ItemMappingEntry>>(){});
} catch (IOException e) {
throw new RuntimeException("Failed to parse item mappings", e);
}

itemEntries.forEach((javaIdentifier, itemMappingEntry) -> {
int javaProtocolId = javaIdAllocator.getAndIncrement(); // Entries are loaded sequentially in the mapping file
// Load item mappings from disk
registerMapping("mappings/1.15/item_mappings.json", ItemMappingEntry.class, itemEntries -> {
itemEntries.forEach((key, value) -> {
ItemMappingEntry itemMappingEntry = (ItemMappingEntry) value;
int javaProtocolId = javaIdAllocator.getAndIncrement(); // Entries are loaded sequentially in the mapping file

javaToBedrockMap.put(javaProtocolId, new ItemEntry(javaIdentifier, javaProtocolId, itemMappingEntry.getBedrockId(), itemMappingEntry.getBedrockData()));
bedrockToJavaMap.put(itemMappingEntry.getBedrockId(), new ItemEntry(javaIdentifier, javaProtocolId, itemMappingEntry.getBedrockId(), itemMappingEntry.getBedrockData()));
javaToBedrockMap.put(javaProtocolId, new ItemEntry(key, javaProtocolId, itemMappingEntry.getBedrockId(), itemMappingEntry.getBedrockData()));
bedrockToJavaMap.put(itemMappingEntry.getBedrockId(), new ItemEntry(key, javaProtocolId, itemMappingEntry.getBedrockId(), itemMappingEntry.getBedrockData()));
});
});

nbtTranslators.add(new DisplayNbtTranslator());
nbtTranslators.add(new EnchantmentNbtTranslator());
}

public static ItemData translateToBedrock(ItemStack item) {
if(item == null || !javaToBedrockMap.containsKey(item.getId())) {
if(item == null || item.getId() == 0 || !javaToBedrockMap.containsKey(item.getId())) {
return ItemData.AIR;
}
ItemEntry itemEntry = javaToBedrockMap.get(item.getId());
Expand All @@ -87,35 +84,24 @@ public static ItemData translateToBedrock(ItemStack item) {
return itemEntry.toItemData(item.getAmount(), translateItemNBT(item.getNbt()));
}

public static ItemData translateSlotToBedrock(ItemStack item) {
if(item == null || item.getId() == 0) {
return ItemData.AIR;
}

ItemData data = translateToBedrock(item);
return data;
}

public static ItemStack translateToJava(ItemData item) {
if(item == null || !bedrockToJavaMap.containsKey(item.getId())) {
if(item == null || item.getId() == 0 || !bedrockToJavaMap.containsKey(item.getId())) {
return new ItemStack(0);
}

ItemEntry javaItem = bedrockToJavaMap.get(item.getId());
// log.warn("ITEM NAME: " + javaItem.getJavaIdentifier());
return new ItemStack(javaItem.getJavaProtocolId(), item.getCount());
}

public static com.nukkitx.nbt.tag.CompoundTag translateItemNBT(CompoundTag tag) {
CompoundTagBuilder root = CompoundTagBuilder.builder();

// First handle NBT that applies to all items
// TODO: register these with annotations
root.tag(new DisplayNbtTranslator().translateToBedrock(tag));
com.nukkitx.nbt.tag.CompoundTag etag = new EnchantmentNbtTranslator().translateToBedrock(tag);
if(etag != null) {
root.tag(etag);
}
nbtTranslators.forEach(translator -> {
com.nukkitx.nbt.tag.CompoundTag bedrockTag = translator.translateToBedrock(tag);
if(bedrockTag != null) {
root.tag(bedrockTag);
}
});

if(tag.getValue() != null && !tag.getValue().isEmpty()) {
for(String tagName : tag.getValue().keySet()) {
Expand Down Expand Up @@ -189,7 +175,7 @@ public static com.nukkitx.nbt.tag.Tag translateRawNBT(Tag tag) {
}

@Getter
private static class ItemMappingEntry {
private static class ItemMappingEntry implements MappingEntry {
@JsonProperty("bedrock_id")
private int bedrockId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ public class PacketTranslatorRegistry<P> extends Registry {

static {
// TODO: make this shit shorter
registerType(PacketRegisterInfo.class);
registerPath("org.dragonet.proxy.network.translator.java", (info, clazz) -> {
registerPath("org.dragonet.proxy.network.translator.java", PacketRegisterInfo.class, (info, clazz) -> {
try {
JAVA_TO_BEDROCK.addTranslator(((PacketRegisterInfo) info).packet(), (PacketTranslator<Packet>) clazz.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
});
registerPath("org.dragonet.proxy.network.translator.bedrock", (info, clazz) -> {
registerPath("org.dragonet.proxy.network.translator.bedrock", PacketRegisterInfo.class, (info, clazz) -> {
try {
BEDROCK_TO_JAVA.addTranslator(((PacketRegisterInfo) info).packet(), (PacketTranslator<BedrockPacket>) clazz.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,27 @@ public void translate(ProxySession session, ServerEntityEquipmentPacket packet)

switch(packet.getSlot()) {
case HELMET:
cachedEntity.setHelmet(ItemTranslatorRegistry.translateSlotToBedrock(packet.getItem()));
cachedEntity.setHelmet(ItemTranslatorRegistry.translateToBedrock(packet.getItem()));
cachedEntity.sendArmor(session);
return;
case CHESTPLATE:
cachedEntity.setChestplate(ItemTranslatorRegistry.translateSlotToBedrock(packet.getItem()));
cachedEntity.setChestplate(ItemTranslatorRegistry.translateToBedrock(packet.getItem()));
cachedEntity.sendArmor(session);
return;
case LEGGINGS:
cachedEntity.setLeggings(ItemTranslatorRegistry.translateSlotToBedrock(packet.getItem()));
cachedEntity.setLeggings(ItemTranslatorRegistry.translateToBedrock(packet.getItem()));
cachedEntity.sendArmor(session);
return;
case BOOTS:
cachedEntity.setBoots(ItemTranslatorRegistry.translateSlotToBedrock(packet.getItem()));
cachedEntity.setBoots(ItemTranslatorRegistry.translateToBedrock(packet.getItem()));
cachedEntity.sendArmor(session);
return;
case MAIN_HAND:
cachedEntity.setMainHand(ItemTranslatorRegistry.translateSlotToBedrock(packet.getItem()));
cachedEntity.setMainHand(ItemTranslatorRegistry.translateToBedrock(packet.getItem()));
break;
case OFF_HAND:
// TODO
cachedEntity.setOffHand(ItemTranslatorRegistry.translateSlotToBedrock(packet.getItem()));
cachedEntity.setOffHand(ItemTranslatorRegistry.translateToBedrock(packet.getItem()));
return;
default:
log.warn("Unknown slot: " + packet.getSlot().name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void translate(ProxySession session, ServerPlayerPositionRotationPacket p
movePlayerPacket.setRuntimeEntityId(entity.getProxyEid());
movePlayerPacket.setPosition(Vector3f.from(packet.getX(), packet.getY() + BedrockEntityType.PLAYER.getOffset() + 0.1f, packet.getZ()));
movePlayerPacket.setRotation(Vector3f.from(packet.getPitch(), packet.getYaw(), 0));
movePlayerPacket.setMode(MovePlayerPacket.Mode.RESET);
movePlayerPacket.setMode(MovePlayerPacket.Mode.NORMAL);
movePlayerPacket.setOnGround(true);

session.sendPacket(movePlayerPacket);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ public static void sendPlayerInventory(ProxySession session) {

// Hotbar
for(int i = 36; i < 45; i++) {
contents[i - 36] = ItemTranslatorRegistry.translateSlotToBedrock(cachedWindow.getItems()[i]);
contents[i - 36] = ItemTranslatorRegistry.translateToBedrock(cachedWindow.getItems()[i]);
}

// Inventory
for(int i = 9; i < 36; i++) {
contents[i] = ItemTranslatorRegistry.translateSlotToBedrock(cachedWindow.getItems()[i]);
contents[i] = ItemTranslatorRegistry.translateToBedrock(cachedWindow.getItems()[i]);
}

// Armour
for(int i = 5; i < 9; i++) {
contents[i + 31] = ItemTranslatorRegistry.translateSlotToBedrock(cachedWindow.getItems()[i]);
contents[i + 31] = ItemTranslatorRegistry.translateToBedrock(cachedWindow.getItems()[i]);
}

inventoryContentPacket.setContents(contents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ItemEntityMetaTranslator extends IMetaTranslator {
public void translateToBedrock(ProxySession session, EntityDataMap dictionary, EntityMetadata metadata) {
if(metadata.getId() == 7) { // Item
CachedItemEntity entity = (CachedItemEntity) this.entity;
entity.setItem(ItemTranslatorRegistry.translateSlotToBedrock((ItemStack) metadata.getValue()));
entity.setItem(ItemTranslatorRegistry.translateToBedrock((ItemStack) metadata.getValue()));
entity.spawn(session);
}
}
Expand Down
12 changes: 12 additions & 0 deletions proxy/src/main/java/org/dragonet/proxy/util/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.dragonet.proxy.util;

import org.dragonet.proxy.DragonProxy;

import java.io.InputStream;

public class FileUtils {

public static InputStream getResource(String path) {
return DragonProxy.class.getClassLoader().getResourceAsStream(path);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.dragonet.proxy.util.registry;

public interface MappingEntry {
}
34 changes: 26 additions & 8 deletions proxy/src/main/java/org/dragonet/proxy/util/registry/Registry.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
package org.dragonet.proxy.util.registry;

import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.dragonet.proxy.DragonProxy;
import org.dragonet.proxy.util.FileUtils;
import org.reflections.Reflections;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.util.LinkedHashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public abstract class Registry {
private static Class<? extends Annotation> annotationClass;

// TODO: make this not static
public static void registerType(Class<? extends Annotation> annotation) {
annotationClass = annotation;
}

// TODO: make this not static
public static void registerPath(String path, BiConsumer<Annotation, Class> callback) {
public static void registerPath(String path, Class<? extends Annotation> annotationClass, BiConsumer<Annotation, Class> consumer) {
for(Class clazz : new Reflections(path).getTypesAnnotatedWith(annotationClass)) {
Annotation annotation = clazz.getAnnotation(annotationClass);

callback.accept(annotation, clazz);
consumer.accept(annotation, clazz);
}
}

public static void registerMapping(String path, Class<? extends MappingEntry> type, Consumer<LinkedHashMap<String, MappingEntry>> consumer) {
InputStream stream = FileUtils.getResource(path);
if(stream == null) {
throw new RuntimeException("Failed to load mappings: " + path);
}

LinkedHashMap<String, MappingEntry> entries;
try {
entries = DragonProxy.JSON_MAPPER.readValue(stream, DragonProxy.JSON_MAPPER.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, type));
} catch (IOException e) {
throw new RuntimeException("Failed to parse mappings: " + path, e);
}

consumer.accept(entries);
}
}

0 comments on commit 49628e5

Please sign in to comment.