2019-07-29 22:20:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector.network.translators.item;
|
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
|
2019-08-04 22:23:19 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.message.Message;
|
2019-07-29 22:20:48 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.ByteArrayTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.DoubleTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.FloatTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.IntTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.ListTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.LongArrayTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.LongTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.ShortTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.StringTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.Tag;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.ItemData;
|
2019-08-09 21:09:47 +00:00
|
|
|
import org.geysermc.connector.console.GeyserLogger;
|
2019-08-10 03:16:34 +00:00
|
|
|
import org.geysermc.connector.utils.MessageUtils;
|
2019-10-10 00:11:50 +00:00
|
|
|
import org.geysermc.connector.utils.Toolbox;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public class ItemTranslator {
|
|
|
|
|
2019-08-10 03:16:34 +00:00
|
|
|
public ItemStack translateToJava(ItemData data) {
|
2019-10-10 00:11:50 +00:00
|
|
|
ItemEntry javaItem = getItem(data);
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2019-08-04 22:23:19 +00:00
|
|
|
if (data.getTag() == null) {
|
2019-10-10 00:11:50 +00:00
|
|
|
return new ItemStack(javaItem.getJavaId(), data.getCount());
|
2019-08-04 22:23:19 +00:00
|
|
|
}
|
2019-10-10 00:11:50 +00:00
|
|
|
return new ItemStack(javaItem.getJavaId(), data.getCount(), translateToJavaNBT(data.getTag()));
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 03:16:34 +00:00
|
|
|
public ItemData translateToBedrock(ItemStack stack) {
|
2019-09-14 02:53:16 +00:00
|
|
|
// Most likely dirt if null
|
2019-07-29 22:20:48 +00:00
|
|
|
if (stack == null) {
|
2019-09-14 02:53:16 +00:00
|
|
|
return ItemData.of(3, (short)0, 0);
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
ItemEntry bedrockItem = getItem(stack);
|
2019-08-04 22:23:19 +00:00
|
|
|
if (stack.getNBT() == null) {
|
2019-10-10 00:11:50 +00:00
|
|
|
return ItemData.of(bedrockItem.getBedrockId(), (short) bedrockItem.getBedrockData(), stack.getAmount());
|
2019-08-04 22:23:19 +00:00
|
|
|
}
|
2019-10-10 00:11:50 +00:00
|
|
|
return ItemData.of(bedrockItem.getBedrockId(), (short) bedrockItem.getBedrockData(), stack.getAmount(), translateToBedrockNBT(stack.getNBT()));
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
public ItemEntry getItem(ItemStack stack) {
|
|
|
|
ItemEntry item = Toolbox.ITEM_ENTRIES.get(stack.getId());
|
|
|
|
if (item == null) {
|
2019-08-09 21:09:47 +00:00
|
|
|
GeyserLogger.DEFAULT.debug("Missing mapping for java item " + stack.getId());
|
2019-10-10 00:11:50 +00:00
|
|
|
return ItemEntry.AIR;
|
2019-08-09 21:09:47 +00:00
|
|
|
}
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
return item;
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
public ItemEntry getItem(ItemData data) {
|
2019-10-11 23:38:34 +00:00
|
|
|
for (ItemEntry itemEntry : Toolbox.ITEM_ENTRIES.valueCollection()) {
|
2019-10-10 00:11:50 +00:00
|
|
|
if (itemEntry.getBedrockId() == data.getId() && itemEntry.getBedrockData() == data.getDamage()) {
|
|
|
|
return itemEntry;
|
|
|
|
}
|
2019-07-31 19:59:23 +00:00
|
|
|
}
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
GeyserLogger.DEFAULT.debug("Missing mapping for bedrock item " + data.getId() + ":" + data.getDamage());
|
|
|
|
return ItemEntry.AIR;
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 03:16:34 +00:00
|
|
|
private CompoundTag translateToJavaNBT(com.nukkitx.nbt.tag.CompoundTag tag) {
|
2019-07-29 22:20:48 +00:00
|
|
|
CompoundTag javaTag = new CompoundTag(tag.getName());
|
|
|
|
Map<String, Tag> javaValue = javaTag.getValue();
|
|
|
|
if (tag.getValue() != null && !tag.getValue().isEmpty()) {
|
|
|
|
for (String str : tag.getValue().keySet()) {
|
|
|
|
com.nukkitx.nbt.tag.Tag bedrockTag = tag.get(str);
|
|
|
|
Tag translatedTag = translateToJavaNBT(bedrockTag);
|
|
|
|
if (translatedTag == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
javaValue.put(str, translatedTag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return javaTag;
|
|
|
|
}
|
|
|
|
|
2019-08-10 03:16:34 +00:00
|
|
|
private Tag translateToJavaNBT(com.nukkitx.nbt.tag.Tag tag) {
|
2019-07-29 22:20:48 +00:00
|
|
|
if (tag instanceof com.nukkitx.nbt.tag.ByteArrayTag) {
|
|
|
|
com.nukkitx.nbt.tag.ByteArrayTag byteArrayTag = (com.nukkitx.nbt.tag.ByteArrayTag) tag;
|
|
|
|
return new ByteArrayTag(byteArrayTag.getName(), byteArrayTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof com.nukkitx.nbt.tag.ByteTag) {
|
|
|
|
com.nukkitx.nbt.tag.ByteTag byteTag = (com.nukkitx.nbt.tag.ByteTag) tag;
|
|
|
|
return new ByteTag(byteTag.getName(), byteTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof com.nukkitx.nbt.tag.DoubleTag) {
|
|
|
|
com.nukkitx.nbt.tag.DoubleTag doubleTag = (com.nukkitx.nbt.tag.DoubleTag) tag;
|
|
|
|
return new DoubleTag(doubleTag.getName(), doubleTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof com.nukkitx.nbt.tag.FloatTag) {
|
|
|
|
com.nukkitx.nbt.tag.FloatTag floatTag = (com.nukkitx.nbt.tag.FloatTag) tag;
|
|
|
|
return new FloatTag(floatTag.getName(), floatTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof com.nukkitx.nbt.tag.IntArrayTag) {
|
|
|
|
com.nukkitx.nbt.tag.IntArrayTag intArrayTag = (com.nukkitx.nbt.tag.IntArrayTag) tag;
|
|
|
|
return new IntArrayTag(intArrayTag.getName(), intArrayTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof com.nukkitx.nbt.tag.IntTag) {
|
|
|
|
com.nukkitx.nbt.tag.IntTag intTag = (com.nukkitx.nbt.tag.IntTag) tag;
|
|
|
|
return new IntTag(intTag.getName(), intTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof com.nukkitx.nbt.tag.LongArrayTag) {
|
|
|
|
com.nukkitx.nbt.tag.LongArrayTag longArrayTag = (com.nukkitx.nbt.tag.LongArrayTag) tag;
|
|
|
|
return new LongArrayTag(longArrayTag.getName(), longArrayTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof com.nukkitx.nbt.tag.LongTag) {
|
|
|
|
com.nukkitx.nbt.tag.LongTag longTag = (com.nukkitx.nbt.tag.LongTag) tag;
|
|
|
|
return new LongTag(longTag.getName(), longTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof com.nukkitx.nbt.tag.ShortTag) {
|
|
|
|
com.nukkitx.nbt.tag.ShortTag shortTag = (com.nukkitx.nbt.tag.ShortTag) tag;
|
|
|
|
return new ShortTag(shortTag.getName(), shortTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof com.nukkitx.nbt.tag.StringTag) {
|
|
|
|
com.nukkitx.nbt.tag.StringTag stringTag = (com.nukkitx.nbt.tag.StringTag) tag;
|
|
|
|
return new StringTag(stringTag.getName(), stringTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof com.nukkitx.nbt.tag.ListTag) {
|
|
|
|
com.nukkitx.nbt.tag.ListTag listTag = (com.nukkitx.nbt.tag.ListTag) tag;
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
List<Tag> tags = new ArrayList<>();
|
2019-07-29 22:20:48 +00:00
|
|
|
for (Object value : listTag.getValue()) {
|
|
|
|
if (!(value instanceof com.nukkitx.nbt.tag.Tag))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
com.nukkitx.nbt.tag.Tag tagValue = (com.nukkitx.nbt.tag.Tag) value;
|
|
|
|
Tag javaTag = translateToJavaNBT(tagValue);
|
|
|
|
if (javaTag != null)
|
|
|
|
tags.add(javaTag);
|
|
|
|
}
|
|
|
|
return new ListTag(listTag.getName(), tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof com.nukkitx.nbt.tag.CompoundTag) {
|
|
|
|
return translateToJavaNBT((com.nukkitx.nbt.tag.CompoundTag) tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-10 03:16:34 +00:00
|
|
|
private com.nukkitx.nbt.tag.CompoundTag translateToBedrockNBT(CompoundTag tag) {
|
2019-07-29 22:20:48 +00:00
|
|
|
Map<String, com.nukkitx.nbt.tag.Tag<?>> javaValue = new HashMap<String, com.nukkitx.nbt.tag.Tag<?>>();
|
|
|
|
if (tag.getValue() != null && !tag.getValue().isEmpty()) {
|
|
|
|
for (String str : tag.getValue().keySet()) {
|
|
|
|
Tag javaTag = tag.get(str);
|
|
|
|
com.nukkitx.nbt.tag.Tag translatedTag = translateToBedrockNBT(javaTag);
|
|
|
|
if (translatedTag == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
javaValue.put(str, translatedTag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
com.nukkitx.nbt.tag.CompoundTag bedrockTag = new com.nukkitx.nbt.tag.CompoundTag(tag.getName(), javaValue);
|
|
|
|
return bedrockTag;
|
|
|
|
}
|
|
|
|
|
2019-08-10 03:16:34 +00:00
|
|
|
private com.nukkitx.nbt.tag.Tag translateToBedrockNBT(Tag tag) {
|
2019-07-29 22:20:48 +00:00
|
|
|
if (tag instanceof ByteArrayTag) {
|
|
|
|
ByteArrayTag byteArrayTag = (ByteArrayTag) tag;
|
|
|
|
return new com.nukkitx.nbt.tag.ByteArrayTag(byteArrayTag.getName(), byteArrayTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof ByteTag) {
|
|
|
|
ByteTag byteTag = (ByteTag) tag;
|
|
|
|
return new com.nukkitx.nbt.tag.ByteTag(byteTag.getName(), byteTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof DoubleTag) {
|
|
|
|
DoubleTag doubleTag = (DoubleTag) tag;
|
|
|
|
return new com.nukkitx.nbt.tag.DoubleTag(doubleTag.getName(), doubleTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof FloatTag) {
|
|
|
|
FloatTag floatTag = (FloatTag) tag;
|
|
|
|
return new com.nukkitx.nbt.tag.FloatTag(floatTag.getName(), floatTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof IntArrayTag) {
|
|
|
|
IntArrayTag intArrayTag = (IntArrayTag) tag;
|
|
|
|
return new com.nukkitx.nbt.tag.IntArrayTag(intArrayTag.getName(), intArrayTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof IntTag) {
|
|
|
|
IntTag intTag = (IntTag) tag;
|
|
|
|
return new com.nukkitx.nbt.tag.IntTag(intTag.getName(), intTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof LongArrayTag) {
|
|
|
|
LongArrayTag longArrayTag = (LongArrayTag) tag;
|
|
|
|
return new com.nukkitx.nbt.tag.LongArrayTag(longArrayTag.getName(), longArrayTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof LongTag) {
|
|
|
|
LongTag longTag = (LongTag) tag;
|
|
|
|
return new com.nukkitx.nbt.tag.LongTag(longTag.getName(), longTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof ShortTag) {
|
|
|
|
ShortTag shortTag = (ShortTag) tag;
|
|
|
|
return new com.nukkitx.nbt.tag.ShortTag(shortTag.getName(), shortTag.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof StringTag) {
|
|
|
|
StringTag stringTag = (StringTag) tag;
|
2019-08-04 22:23:19 +00:00
|
|
|
return new com.nukkitx.nbt.tag.StringTag(stringTag.getName(), MessageUtils.getBedrockMessage(Message.fromString(stringTag.getValue())));
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof ListTag) {
|
|
|
|
ListTag listTag = (ListTag) tag;
|
2019-08-04 22:23:19 +00:00
|
|
|
if (listTag.getName().equalsIgnoreCase("Lore")) {
|
|
|
|
List<com.nukkitx.nbt.tag.StringTag> tags = new ArrayList<>();
|
|
|
|
for (Object value : listTag.getValue()) {
|
|
|
|
if (!(value instanceof Tag))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
com.nukkitx.nbt.tag.StringTag bedrockTag = (com.nukkitx.nbt.tag.StringTag) translateToBedrockNBT((Tag) value);
|
|
|
|
if (bedrockTag != null)
|
|
|
|
tags.add(bedrockTag);
|
|
|
|
}
|
|
|
|
return new com.nukkitx.nbt.tag.ListTag<>(listTag.getName(), com.nukkitx.nbt.tag.StringTag.class, tags);
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag instanceof CompoundTag) {
|
|
|
|
return translateToBedrockNBT((CompoundTag) tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|