2020-01-09 03:05:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* 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:
|
2020-01-09 03:05:42 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2020-01-09 03:05:42 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* 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.
|
2020-01-09 03:05:42 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
2020-01-09 03:05:42 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-25 01:07:05 +00:00
|
|
|
package org.geysermc.connector.network.translators.item;
|
2019-07-11 22:39:28 +00:00
|
|
|
|
2019-12-30 23:33:27 +00:00
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
2020-05-25 01:07:05 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
|
2020-07-05 20:58:43 +00:00
|
|
|
import com.nukkitx.nbt.NbtMap;
|
2019-10-31 02:14:23 +00:00
|
|
|
import com.nukkitx.nbt.NbtUtils;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
|
2019-07-17 01:05:10 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
|
2019-11-30 19:26:51 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
2019-10-31 02:14:23 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2020-05-25 01:07:05 +00:00
|
|
|
import org.geysermc.connector.utils.FileUtils;
|
2020-07-05 23:35:51 +00:00
|
|
|
import org.geysermc.connector.utils.LanguageUtils;
|
2020-05-25 01:07:05 +00:00
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2020-07-05 23:35:51 +00:00
|
|
|
import java.util.*;
|
2020-05-25 01:07:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registry for anything item related.
|
|
|
|
*/
|
|
|
|
public class ItemRegistry {
|
2019-07-11 22:39:28 +00:00
|
|
|
|
2020-05-25 01:07:05 +00:00
|
|
|
private static final Map<String, ItemEntry> JAVA_IDENTIFIER_MAP = new HashMap<>();
|
2019-07-11 22:39:28 +00:00
|
|
|
|
2020-02-13 06:58:09 +00:00
|
|
|
public static final ItemData[] CREATIVE_ITEMS;
|
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-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-07-31 02:10:55 +00:00
|
|
|
/**
|
|
|
|
* Boat item entry, used in BedrockInventoryTransactionTranslator.java
|
|
|
|
*/
|
2020-07-07 20:40:19 +00:00
|
|
|
public static ItemEntry BOAT;
|
2020-07-31 02:10:55 +00:00
|
|
|
/**
|
|
|
|
* Bucket item entry, used in BedrockInventoryTransactionTranslator.java
|
|
|
|
*/
|
2020-07-21 17:17:55 +00:00
|
|
|
public static ItemEntry BUCKET;
|
2020-07-31 02:10:55 +00:00
|
|
|
/**
|
|
|
|
* Gold item entry, used in PiglinEntity.java
|
|
|
|
*/
|
2020-07-07 20:40:19 +00:00
|
|
|
public static ItemEntry GOLD;
|
2020-07-31 02:10:55 +00:00
|
|
|
/**
|
|
|
|
* Shield item entry, used in Entity.java and LivingEntity.java
|
|
|
|
*/
|
2020-07-21 17:17:55 +00:00
|
|
|
public static ItemEntry SHIELD;
|
2020-04-26 04:55:06 +00:00
|
|
|
|
2020-04-25 22:33:52 +00:00
|
|
|
public static int BARRIER_INDEX = 0;
|
2020-04-05 01:58:23 +00:00
|
|
|
|
2020-05-25 01:07:05 +00:00
|
|
|
public static void init() {
|
|
|
|
// no-op
|
|
|
|
}
|
2019-07-11 22:39:28 +00:00
|
|
|
|
2020-05-25 01:07:05 +00:00
|
|
|
static {
|
2019-12-30 23:33:27 +00:00
|
|
|
/* Load item palette */
|
2020-05-25 01:07:05 +00:00
|
|
|
InputStream stream = FileUtils.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 {
|
2020-05-25 01:07:05 +00:00
|
|
|
itemEntries = GeyserConnector.JSON_MAPPER.readValue(stream, itemEntriesType);
|
2019-07-17 01:05:10 +00:00
|
|
|
} catch (Exception e) {
|
2020-07-05 23:35:51 +00:00
|
|
|
throw new AssertionError(LanguageUtils.getLocaleStringLog("geyser.toolbox.fail.runtime_bedrock"), 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
|
|
|
}
|
|
|
|
|
2020-05-25 01:07:05 +00:00
|
|
|
stream = FileUtils.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 {
|
2020-05-25 01:07:05 +00:00
|
|
|
items = GeyserConnector.JSON_MAPPER.readTree(stream);
|
2019-12-30 23:33:27 +00:00
|
|
|
} catch (Exception e) {
|
2020-07-05 23:35:51 +00:00
|
|
|
throw new AssertionError(LanguageUtils.getLocaleStringLog("geyser.toolbox.fail.runtime_java"), 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()) {
|
2020-06-26 15:15:21 +00:00
|
|
|
Map.Entry<String, JsonNode> entry = iterator.next();
|
|
|
|
if (entry.getValue().has("tool_type")) {
|
|
|
|
if (entry.getValue().has("tool_tier")) {
|
|
|
|
ITEM_ENTRIES.put(itemIndex, new ToolItemEntry(
|
2020-03-21 21:37:55 +00:00
|
|
|
entry.getKey(), itemIndex,
|
|
|
|
entry.getValue().get("bedrock_id").intValue(),
|
|
|
|
entry.getValue().get("bedrock_data").intValue(),
|
2020-06-26 15:15:21 +00:00
|
|
|
entry.getValue().get("tool_type").textValue(),
|
|
|
|
entry.getValue().get("tool_tier").textValue(),
|
2020-06-21 02:24:45 +00:00
|
|
|
entry.getValue().get("is_block") != null && entry.getValue().get("is_block").booleanValue()));
|
2020-06-26 15:15:21 +00:00
|
|
|
} else {
|
|
|
|
ITEM_ENTRIES.put(itemIndex, new ToolItemEntry(
|
|
|
|
entry.getKey(), itemIndex,
|
|
|
|
entry.getValue().get("bedrock_id").intValue(),
|
|
|
|
entry.getValue().get("bedrock_data").intValue(),
|
|
|
|
entry.getValue().get("tool_type").textValue(),
|
|
|
|
"",
|
|
|
|
entry.getValue().get("is_block").booleanValue()));
|
2020-06-21 02:24:45 +00:00
|
|
|
}
|
2020-06-26 15:15:21 +00:00
|
|
|
} else {
|
|
|
|
ITEM_ENTRIES.put(itemIndex, new ItemEntry(
|
|
|
|
entry.getKey(), itemIndex,
|
|
|
|
entry.getValue().get("bedrock_id").intValue(),
|
|
|
|
entry.getValue().get("bedrock_data").intValue(),
|
|
|
|
entry.getValue().get("is_block") != null && entry.getValue().get("is_block").booleanValue()));
|
2020-06-21 02:24:45 +00:00
|
|
|
}
|
2020-07-07 20:40:19 +00:00
|
|
|
switch (entry.getKey()) {
|
|
|
|
case "minecraft:barrier":
|
|
|
|
BARRIER_INDEX = itemIndex;
|
|
|
|
break;
|
|
|
|
case "minecraft:oak_boat":
|
|
|
|
BOAT = ITEM_ENTRIES.get(itemIndex);
|
|
|
|
break;
|
|
|
|
case "minecraft:gold_ingot":
|
|
|
|
GOLD = ITEM_ENTRIES.get(itemIndex);
|
|
|
|
break;
|
|
|
|
case "minecraft:shield":
|
|
|
|
SHIELD = ITEM_ENTRIES.get(itemIndex);
|
|
|
|
break;
|
2020-07-21 17:17:55 +00:00
|
|
|
case "minecraft:bucket":
|
|
|
|
BUCKET = ITEM_ENTRIES.get(itemIndex);
|
|
|
|
break;
|
2020-07-07 20:40:19 +00:00
|
|
|
default:
|
|
|
|
break;
|
2020-06-26 15:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
itemIndex++;
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
2020-04-23 04:40:49 +00:00
|
|
|
|
2020-06-29 23:52:32 +00:00
|
|
|
// Add the loadstonecompass since it doesn't exist on java but we need it for item conversion
|
|
|
|
ITEM_ENTRIES.put(itemIndex, new ItemEntry("minecraft:lodestonecompass", itemIndex, 741, 0, false));
|
|
|
|
|
2020-04-26 04:55:06 +00:00
|
|
|
/* Load creative items */
|
2020-05-25 01:07:05 +00:00
|
|
|
stream = FileUtils.getResource("bedrock/creative_items.json");
|
2019-08-07 23:08:48 +00:00
|
|
|
|
2020-01-31 01:05:57 +00:00
|
|
|
JsonNode creativeItemEntries;
|
2019-11-10 02:20:47 +00:00
|
|
|
try {
|
2020-05-25 01:07:05 +00:00
|
|
|
creativeItemEntries = GeyserConnector.JSON_MAPPER.readTree(stream).get("items");
|
2019-11-10 02:20:47 +00:00
|
|
|
} catch (Exception e) {
|
2020-07-05 23:35:51 +00:00
|
|
|
throw new AssertionError(LanguageUtils.getLocaleStringLog("geyser.toolbox.fail.creative"), e);
|
2019-11-10 02:20:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 19:42:15 +00:00
|
|
|
int netId = 1;
|
2019-11-10 02:20:47 +00:00
|
|
|
List<ItemData> creativeItems = new ArrayList<>();
|
2020-01-31 01:05:57 +00:00
|
|
|
for (JsonNode itemNode : creativeItemEntries) {
|
2020-08-25 02:14:44 +00:00
|
|
|
ItemData item = getBedrockItemFromJson(itemNode);
|
|
|
|
creativeItems.add(ItemData.fromNet(netId++, item.getId(), item.getDamage(), item.getCount(), item.getTag()));
|
2019-11-10 02:20:47 +00:00
|
|
|
}
|
|
|
|
CREATIVE_ITEMS = creativeItems.toArray(new ItemData[0]);
|
2020-05-25 01:07:05 +00:00
|
|
|
}
|
2020-04-26 04:55:06 +00:00
|
|
|
|
2020-05-25 01:07:05 +00:00
|
|
|
/**
|
|
|
|
* Gets an {@link ItemEntry} from the given {@link ItemStack}.
|
|
|
|
*
|
|
|
|
* @param stack the item stack
|
|
|
|
* @return an item entry from the given item stack
|
|
|
|
*/
|
|
|
|
public static ItemEntry getItem(ItemStack stack) {
|
|
|
|
return ITEM_ENTRIES.get(stack.getId());
|
2019-12-30 23:33:27 +00:00
|
|
|
}
|
2019-07-31 19:59:23 +00:00
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
/**
|
2020-05-25 01:07:05 +00:00
|
|
|
* Gets an {@link ItemEntry} from the given {@link ItemData}.
|
2020-04-21 05:28:44 +00:00
|
|
|
*
|
2020-05-25 01:07:05 +00:00
|
|
|
* @param data the item data
|
|
|
|
* @return an item entry from the given item data
|
2020-04-21 05:28:44 +00:00
|
|
|
*/
|
2020-05-25 01:07:05 +00:00
|
|
|
public static ItemEntry getItem(ItemData data) {
|
|
|
|
for (ItemEntry itemEntry : ITEM_ENTRIES.values()) {
|
|
|
|
if (itemEntry.getBedrockId() == data.getId() && (itemEntry.getBedrockData() == data.getDamage() || itemEntry.getJavaIdentifier().endsWith("potion"))) {
|
|
|
|
return itemEntry;
|
|
|
|
}
|
2019-08-07 23:08:48 +00:00
|
|
|
}
|
2020-05-25 01:07:05 +00:00
|
|
|
|
2020-06-30 12:20:03 +00:00
|
|
|
// This will hide the message when the player clicks with an empty hand
|
|
|
|
if (data.getId() != 0 && data.getDamage() != 0) {
|
|
|
|
GeyserConnector.getInstance().getLogger().debug("Missing mapping for bedrock item " + data.getId() + ":" + data.getDamage());
|
|
|
|
}
|
2020-05-25 01:07:05 +00:00
|
|
|
return ItemEntry.AIR;
|
2019-12-30 23:33:27 +00:00
|
|
|
}
|
2019-08-07 23:08:48 +00:00
|
|
|
|
2020-05-25 01:07:05 +00:00
|
|
|
/**
|
|
|
|
* Gets an {@link ItemEntry} from the given Minecraft: Java Edition
|
|
|
|
* block state identifier.
|
|
|
|
*
|
|
|
|
* @param javaIdentifier the block state identifier
|
|
|
|
* @return an item entry from the given java edition identifier
|
|
|
|
*/
|
|
|
|
public static ItemEntry getItemEntry(String javaIdentifier) {
|
|
|
|
return JAVA_IDENTIFIER_MAP.computeIfAbsent(javaIdentifier, key -> ITEM_ENTRIES.values()
|
|
|
|
.stream().filter(itemEntry -> itemEntry.getJavaIdentifier().equals(key)).findFirst().orElse(null));
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
2020-08-25 02:14:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a Bedrock {@link ItemData} from a {@link JsonNode}
|
|
|
|
* @param itemNode the JSON node that contains ProxyPass-compatible Bedrock item data
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static ItemData getBedrockItemFromJson(JsonNode itemNode) {
|
|
|
|
int count = 1;
|
|
|
|
short damage = 0;
|
|
|
|
NbtMap tag = null;
|
|
|
|
if (itemNode.has("damage")) {
|
|
|
|
damage = itemNode.get("damage").numberValue().shortValue();
|
|
|
|
}
|
|
|
|
if (itemNode.has("count")) {
|
|
|
|
count = itemNode.get("count").asInt();
|
|
|
|
}
|
|
|
|
if (itemNode.has("nbt_b64")) {
|
|
|
|
byte[] bytes = Base64.getDecoder().decode(itemNode.get("nbt_b64").asText());
|
|
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
|
|
|
|
try {
|
|
|
|
tag = (NbtMap) NbtUtils.createReaderLE(bais).readTag();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ItemData.of(itemNode.get("id").asInt(), damage, count, tag);
|
|
|
|
}
|
2020-05-25 01:07:05 +00:00
|
|
|
}
|