2020-01-09 03:05:42 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-01-09 03:05:42 +00:00
|
|
|
*
|
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;
|
2021-03-31 18:06:05 +00:00
|
|
|
import com.google.common.collect.ImmutableSet;
|
2020-07-05 20:58:43 +00:00
|
|
|
import com.nukkitx.nbt.NbtMap;
|
2021-03-31 18:06:05 +00:00
|
|
|
import com.nukkitx.nbt.NbtMapBuilder;
|
|
|
|
import com.nukkitx.nbt.NbtType;
|
2019-10-31 02:14:23 +00:00
|
|
|
import com.nukkitx.nbt.NbtUtils;
|
2021-03-31 18:06:05 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ComponentItemData;
|
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;
|
2021-04-08 23:52:54 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.IntArraySet;
|
|
|
|
import it.unimi.dsi.fastutil.ints.IntSet;
|
2021-04-08 23:32:06 +00:00
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
2021-03-31 18:06:05 +00:00
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
2019-10-31 02:14:23 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2021-04-10 04:31:24 +00:00
|
|
|
import org.geysermc.connector.network.translators.world.block.BlockTranslator;
|
2021-04-06 04:14:06 +00:00
|
|
|
import org.geysermc.connector.network.translators.world.block.BlockTranslator1_16_210;
|
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-11-30 15:55:35 +00:00
|
|
|
/**
|
|
|
|
* A list of all identifiers that only exist on Java. Used to prevent creative items from becoming these unintentionally.
|
|
|
|
*/
|
2021-03-31 18:06:05 +00:00
|
|
|
private static final Set<String> JAVA_ONLY_ITEMS;
|
2020-11-30 15:55:35 +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
|
|
|
|
2021-02-17 23:00:00 +00:00
|
|
|
/**
|
|
|
|
* A list of all Java item names.
|
|
|
|
*/
|
|
|
|
public static final String[] ITEM_NAMES;
|
|
|
|
|
2020-11-12 00:28:45 +00:00
|
|
|
/**
|
|
|
|
* Bamboo item entry, used in PandaEntity.java
|
|
|
|
*/
|
|
|
|
public static ItemEntry BAMBOO;
|
2021-04-08 23:52:54 +00:00
|
|
|
/**
|
|
|
|
* Banner item entry, used in LivingEntity.java
|
|
|
|
*/
|
|
|
|
public static ItemEntry BANNER;
|
2020-07-31 02:10:55 +00:00
|
|
|
/**
|
2020-11-17 17:03:12 +00:00
|
|
|
* Boat item entries, used in BedrockInventoryTransactionTranslator.java
|
2020-07-31 02:10:55 +00:00
|
|
|
*/
|
2021-04-08 23:52:54 +00:00
|
|
|
public static final IntSet BOATS = new IntArraySet();
|
2020-07-31 02:10:55 +00:00
|
|
|
/**
|
2020-11-17 17:03:12 +00:00
|
|
|
* Bucket item entries (excluding the milk bucket), used in BedrockInventoryTransactionTranslator.java
|
2020-07-31 02:10:55 +00:00
|
|
|
*/
|
2021-04-08 23:52:54 +00:00
|
|
|
public static final IntSet BUCKETS = new IntArraySet();
|
|
|
|
/**
|
|
|
|
* Crossbow item entry, used in PillagerEntity.java
|
|
|
|
*/
|
|
|
|
public static ItemEntry CROSSBOW;
|
2020-11-17 17:03:12 +00:00
|
|
|
/**
|
|
|
|
* Empty item bucket, used in BedrockInventoryTransactionTranslator.java
|
|
|
|
*/
|
|
|
|
public static ItemEntry MILK_BUCKET;
|
2020-10-26 15:54:37 +00:00
|
|
|
/**
|
|
|
|
* Egg item entry, used in JavaEntityStatusTranslator.java
|
|
|
|
*/
|
|
|
|
public static ItemEntry EGG;
|
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-11-11 18:13:13 +00:00
|
|
|
/**
|
|
|
|
* Wheat item entry, used in AbstractHorseEntity.java
|
|
|
|
*/
|
|
|
|
public static ItemEntry WHEAT;
|
2021-01-08 00:40:34 +00:00
|
|
|
/**
|
|
|
|
* Writable book item entry, used in BedrockBookEditTranslator.java
|
|
|
|
*/
|
|
|
|
public static ItemEntry WRITABLE_BOOK;
|
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
|
|
|
|
2021-03-31 18:06:05 +00:00
|
|
|
/**
|
|
|
|
* Stores the properties and data of the "custom" furnace minecart item.
|
|
|
|
*/
|
|
|
|
public static final ComponentItemData FURNACE_MINECART_DATA;
|
|
|
|
|
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-11-17 17:03:12 +00:00
|
|
|
InputStream stream = FileUtils.getResource("bedrock/runtime_item_states.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
|
|
|
|
2020-11-18 06:10:49 +00:00
|
|
|
// Used to get the Bedrock namespaced ID (in instances where there are small differences)
|
|
|
|
Int2ObjectMap<String> bedrockIdToIdentifier = new Int2ObjectOpenHashMap<>();
|
|
|
|
|
2021-02-17 23:00:00 +00:00
|
|
|
List<String> itemNames = new ArrayList<>();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-17 17:03:12 +00:00
|
|
|
int lodestoneCompassId = 0;
|
|
|
|
|
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()));
|
2020-11-18 06:10:49 +00:00
|
|
|
bedrockIdToIdentifier.put(entry.get("id").intValue(), entry.get("name").textValue());
|
2020-11-17 17:03:12 +00:00
|
|
|
if (entry.get("name").textValue().equals("minecraft:lodestone_compass")) {
|
|
|
|
lodestoneCompassId = entry.get("id").intValue();
|
|
|
|
}
|
2019-07-17 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 23:32:06 +00:00
|
|
|
Object2IntMap<String> bedrockBlockIdOverrides = new Object2IntOpenHashMap<>();
|
2021-04-10 04:31:24 +00:00
|
|
|
Object2IntMap<String> blacklistedIdentifiers = new Object2IntOpenHashMap<>();
|
2021-04-08 23:32:06 +00:00
|
|
|
|
|
|
|
// Load creative items
|
|
|
|
// We load this before item mappings to get overridden block runtime ID mappings
|
|
|
|
stream = FileUtils.getResource("bedrock/creative_items.json");
|
|
|
|
|
|
|
|
JsonNode creativeItemEntries;
|
|
|
|
try {
|
|
|
|
creativeItemEntries = GeyserConnector.JSON_MAPPER.readTree(stream).get("items");
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new AssertionError(LanguageUtils.getLocaleStringLog("geyser.toolbox.fail.creative"), e);
|
|
|
|
}
|
|
|
|
|
|
|
|
int netId = 1;
|
|
|
|
List<ItemData> creativeItems = new ArrayList<>();
|
|
|
|
for (JsonNode itemNode : creativeItemEntries) {
|
|
|
|
int count = 1;
|
|
|
|
int damage = 0;
|
|
|
|
int blockRuntimeId = 0;
|
|
|
|
NbtMap tag = null;
|
|
|
|
JsonNode damageNode = itemNode.get("damage");
|
|
|
|
if (damageNode != null) {
|
|
|
|
damage = damageNode.asInt();
|
|
|
|
}
|
|
|
|
JsonNode countNode = itemNode.get("count");
|
|
|
|
if (countNode != null) {
|
|
|
|
count = countNode.asInt();
|
|
|
|
}
|
|
|
|
JsonNode blockRuntimeIdNode = itemNode.get("blockRuntimeId");
|
|
|
|
if (blockRuntimeIdNode != null) {
|
|
|
|
blockRuntimeId = blockRuntimeIdNode.asInt();
|
|
|
|
}
|
|
|
|
JsonNode nbtNode = itemNode.get("nbt_b64");
|
|
|
|
if (nbtNode != null) {
|
|
|
|
byte[] bytes = Base64.getDecoder().decode(nbtNode.asText());
|
|
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
|
|
|
|
try {
|
|
|
|
tag = (NbtMap) NbtUtils.createReaderLE(bais).readTag();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String identifier = itemNode.get("id").textValue();
|
|
|
|
int id = -1;
|
|
|
|
for (StartGamePacket.ItemEntry itemEntry : ITEMS) {
|
|
|
|
if (itemEntry.getIdentifier().equals(identifier)) {
|
|
|
|
id = itemEntry.getId();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (id == -1) {
|
|
|
|
throw new RuntimeException("Unable to find matching Bedrock item for " + identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
creativeItems.add(ItemData.builder()
|
|
|
|
.id(id)
|
|
|
|
.damage(damage)
|
|
|
|
.count(count)
|
|
|
|
.blockRuntimeId(blockRuntimeId)
|
|
|
|
.tag(tag)
|
|
|
|
.netId(netId++).build());
|
|
|
|
|
|
|
|
if (blockRuntimeId != 0) {
|
|
|
|
// Add override for item mapping, unless it already exists... then we know multiple states can exist
|
2021-04-10 04:31:24 +00:00
|
|
|
if (!blacklistedIdentifiers.containsKey(identifier)) {
|
2021-04-08 23:32:06 +00:00
|
|
|
if (bedrockBlockIdOverrides.containsKey(identifier)) {
|
|
|
|
bedrockBlockIdOverrides.remove(identifier);
|
2021-04-10 04:31:24 +00:00
|
|
|
// Save this as a blacklist, but also as knowledge of what the block state name should be
|
|
|
|
blacklistedIdentifiers.put(identifier, blockRuntimeId);
|
2021-04-08 23:32:06 +00:00
|
|
|
} else {
|
|
|
|
// Unless there's multiple possibilities for this one state, let this be
|
|
|
|
bedrockBlockIdOverrides.put(identifier, blockRuntimeId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load item mappings from Java Edition to Bedrock Edition
|
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
|
|
|
}
|
|
|
|
|
2021-04-10 04:31:24 +00:00
|
|
|
BlockTranslator blockTranslator = BlockTranslator1_16_210.INSTANCE;
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
int itemIndex = 0;
|
2021-03-31 18:06:05 +00:00
|
|
|
int javaFurnaceMinecartId = 0;
|
|
|
|
boolean usingFurnaceMinecart = GeyserConnector.getInstance().getConfig().isAddNonBedrockItems();
|
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();
|
2021-03-31 18:06:05 +00:00
|
|
|
if (usingFurnaceMinecart && entry.getKey().equals("minecraft:furnace_minecart")) {
|
|
|
|
javaFurnaceMinecartId = itemIndex;
|
|
|
|
itemIndex++;
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-18 06:10:49 +00:00
|
|
|
int bedrockId = entry.getValue().get("bedrock_id").intValue();
|
|
|
|
String bedrockIdentifier = bedrockIdToIdentifier.get(bedrockId);
|
|
|
|
if (bedrockIdentifier == null) {
|
|
|
|
throw new RuntimeException("Missing Bedrock ID in mappings!: " + bedrockId);
|
|
|
|
}
|
2021-01-05 00:12:26 +00:00
|
|
|
JsonNode stackSizeNode = entry.getValue().get("stack_size");
|
|
|
|
int stackSize = stackSizeNode == null ? 64 : stackSizeNode.intValue();
|
2021-04-06 04:14:06 +00:00
|
|
|
|
|
|
|
int bedrockBlockId = -1;
|
|
|
|
JsonNode blockRuntimeIdNode = entry.getValue().get("blockRuntimeId");
|
|
|
|
if (blockRuntimeIdNode != null) {
|
2021-04-08 23:32:06 +00:00
|
|
|
int blockIdOverride = bedrockBlockIdOverrides.getOrDefault(bedrockIdentifier, -1);
|
|
|
|
if (blockIdOverride != -1) {
|
|
|
|
// Straight from BDS is our best chance of getting an item that doesn't run into issues
|
|
|
|
bedrockBlockId = blockIdOverride;
|
|
|
|
} else {
|
2021-04-10 04:31:24 +00:00
|
|
|
// Try to get an example block runtime ID from the creative contents packet, for Bedrock identifier obtaining
|
|
|
|
int aValidBedrockBlockId = blacklistedIdentifiers.getOrDefault(bedrockIdentifier, -1);
|
|
|
|
if (aValidBedrockBlockId == -1) {
|
|
|
|
// Fallback
|
|
|
|
bedrockBlockId = blockTranslator.getBedrockBlockId(blockRuntimeIdNode.intValue());
|
|
|
|
} else {
|
|
|
|
// As of 1.16.220, every item requires a block runtime ID attached to it.
|
|
|
|
// This is mostly for identifying different blocks with the same item ID - wool, slabs, some walls.
|
|
|
|
// However, in order for some visuals and crafting to work, we need to send the first matching block state
|
|
|
|
// as indexed by Bedrock's block palette
|
|
|
|
// There are exceptions! But, ideally, the block ID override should take care of those.
|
|
|
|
String javaBlockIdentifier = BlockTranslator.getJavaIdBlockMap().inverse().get(blockRuntimeIdNode.intValue()).split("\\[")[0];
|
|
|
|
NbtMapBuilder requiredBlockStatesBuilder = NbtMap.builder();
|
|
|
|
String correctBedrockIdentifier = blockTranslator.getAllBedrockBlockStates().get(aValidBedrockBlockId).getString("name");
|
|
|
|
boolean firstPass = true;
|
|
|
|
for (Map.Entry<String, Integer> blockEntry : BlockTranslator.getJavaIdBlockMap().entrySet()) {
|
|
|
|
if (blockEntry.getKey().split("\\[")[0].equals(javaBlockIdentifier)) {
|
|
|
|
int bedrockBlockRuntimeId = blockTranslator.getBedrockBlockId(blockEntry.getValue());
|
|
|
|
NbtMap blockTag = blockTranslator.getAllBedrockBlockStates().get(bedrockBlockRuntimeId);
|
|
|
|
String bedrockName = blockTag.getString("name");
|
|
|
|
if (!bedrockName.equals(correctBedrockIdentifier)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
NbtMap states = blockTag.getCompound("states");
|
|
|
|
|
|
|
|
if (firstPass) {
|
|
|
|
firstPass = false;
|
|
|
|
if (states.size() == 0) {
|
|
|
|
// No need to iterate and find all block states - this is the one, as there can't be any others
|
|
|
|
bedrockBlockId = bedrockBlockRuntimeId;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
requiredBlockStatesBuilder.putAll(states);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (Map.Entry<String, Object> nbtEntry : states.entrySet()) {
|
|
|
|
Object value = requiredBlockStatesBuilder.get(nbtEntry.getKey());
|
|
|
|
if (value != null && !nbtEntry.getValue().equals(value)) { // Null means this value has already been removed/deemed as unneeded
|
|
|
|
// This state can change between different block states, and therefore is not required
|
|
|
|
// to build a successful block state of this
|
|
|
|
requiredBlockStatesBuilder.remove(nbtEntry.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (requiredBlockStatesBuilder.size() == 0) {
|
|
|
|
// There are no required block states
|
|
|
|
// E.G. there was only a direction property that is no longer in play
|
|
|
|
// (States that are important include color for glass)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NbtMap requiredBlockStates = requiredBlockStatesBuilder.build();
|
|
|
|
if (bedrockBlockId == -1) {
|
|
|
|
int i = -1;
|
|
|
|
// We need to loop around again (we can't cache the block tags above) because Bedrock can include states that we don't have a pairing for
|
|
|
|
// in it's "preferred" block state - I.E. the first matching block state in the list
|
|
|
|
for (NbtMap blockTag : blockTranslator.getAllBedrockBlockStates()) {
|
|
|
|
i++;
|
|
|
|
if (blockTag.getString("name").equals(correctBedrockIdentifier)) {
|
|
|
|
NbtMap states = blockTag.getCompound("states");
|
|
|
|
boolean valid = true;
|
|
|
|
for (Map.Entry<String, Object> nbtEntry : requiredBlockStates.entrySet()) {
|
|
|
|
if (!states.get(nbtEntry.getKey()).equals(nbtEntry.getValue())) {
|
|
|
|
// A required block state doesn't match - this one is not valid
|
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (valid) {
|
|
|
|
bedrockBlockId = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bedrockBlockId == -1) {
|
|
|
|
throw new RuntimeException("Could not find a block match for " + entry.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Because we have replaced the Bedrock block ID, we also need to replace the creative contents block runtime ID
|
|
|
|
// That way, creative items work correctly for these blocks
|
|
|
|
for (int j = 0; j < creativeItems.size(); j++) {
|
|
|
|
ItemData itemData = creativeItems.get(j);
|
|
|
|
if (itemData.getId() == bedrockId) {
|
|
|
|
if (itemData.getDamage() != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
NbtMap states = blockTranslator.getAllBedrockBlockStates().get(itemData.getBlockRuntimeId()).getCompound("states");
|
|
|
|
boolean valid = true;
|
|
|
|
for (Map.Entry<String, Object> nbtEntry : requiredBlockStates.entrySet()) {
|
|
|
|
if (!states.get(nbtEntry.getKey()).equals(nbtEntry.getValue())) {
|
|
|
|
// A required block state doesn't match - this one is not valid
|
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (valid) {
|
|
|
|
creativeItems.set(j, itemData.toBuilder().blockRuntimeId(bedrockBlockId).build());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-08 23:32:06 +00:00
|
|
|
}
|
2021-04-06 04:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ItemEntry itemEntry;
|
2020-06-26 15:15:21 +00:00
|
|
|
if (entry.getValue().has("tool_type")) {
|
|
|
|
if (entry.getValue().has("tool_tier")) {
|
2021-04-06 04:14:06 +00:00
|
|
|
itemEntry = new ToolItemEntry(
|
2020-11-18 06:10:49 +00:00
|
|
|
entry.getKey(), bedrockIdentifier, itemIndex, bedrockId,
|
2020-03-21 21:37:55 +00:00
|
|
|
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(),
|
2021-04-06 04:14:06 +00:00
|
|
|
bedrockBlockId,
|
|
|
|
stackSize);
|
2020-06-26 15:15:21 +00:00
|
|
|
} else {
|
2021-04-06 04:14:06 +00:00
|
|
|
itemEntry = new ToolItemEntry(
|
2020-11-18 06:10:49 +00:00
|
|
|
entry.getKey(), bedrockIdentifier, itemIndex, bedrockId,
|
2020-06-26 15:15:21 +00:00
|
|
|
entry.getValue().get("bedrock_data").intValue(),
|
|
|
|
entry.getValue().get("tool_type").textValue(),
|
2021-04-06 04:14:06 +00:00
|
|
|
"", bedrockBlockId,
|
|
|
|
stackSize);
|
2020-06-21 02:24:45 +00:00
|
|
|
}
|
2020-06-26 15:15:21 +00:00
|
|
|
} else {
|
2021-04-06 04:14:06 +00:00
|
|
|
itemEntry = new ItemEntry(
|
2020-11-18 06:10:49 +00:00
|
|
|
entry.getKey(), bedrockIdentifier, itemIndex, bedrockId,
|
2020-06-26 15:15:21 +00:00
|
|
|
entry.getValue().get("bedrock_data").intValue(),
|
2021-04-06 04:14:06 +00:00
|
|
|
bedrockBlockId,
|
|
|
|
stackSize);
|
2020-06-21 02:24:45 +00:00
|
|
|
}
|
2021-04-06 04:14:06 +00:00
|
|
|
ITEM_ENTRIES.put(itemIndex, itemEntry);
|
|
|
|
|
2020-07-07 20:40:19 +00:00
|
|
|
switch (entry.getKey()) {
|
|
|
|
case "minecraft:barrier":
|
|
|
|
BARRIER_INDEX = itemIndex;
|
|
|
|
break;
|
2020-11-12 00:28:45 +00:00
|
|
|
case "minecraft:bamboo":
|
2021-04-06 04:14:06 +00:00
|
|
|
BAMBOO = itemEntry;
|
2020-11-12 00:28:45 +00:00
|
|
|
break;
|
2021-04-08 23:52:54 +00:00
|
|
|
case "minecraft:crossbow":
|
|
|
|
CROSSBOW = itemEntry;
|
|
|
|
break;
|
2020-10-26 15:54:37 +00:00
|
|
|
case "minecraft:egg":
|
2021-04-06 04:14:06 +00:00
|
|
|
EGG = itemEntry;
|
2020-10-26 15:54:37 +00:00
|
|
|
break;
|
2020-07-07 20:40:19 +00:00
|
|
|
case "minecraft:gold_ingot":
|
2021-04-06 04:14:06 +00:00
|
|
|
GOLD = itemEntry;
|
2020-07-07 20:40:19 +00:00
|
|
|
break;
|
|
|
|
case "minecraft:shield":
|
2021-04-06 04:14:06 +00:00
|
|
|
SHIELD = itemEntry;
|
2020-07-07 20:40:19 +00:00
|
|
|
break;
|
2020-11-17 17:03:12 +00:00
|
|
|
case "minecraft:milk_bucket":
|
2021-04-06 04:14:06 +00:00
|
|
|
MILK_BUCKET = itemEntry;
|
2020-07-21 17:17:55 +00:00
|
|
|
break;
|
2020-11-11 18:13:13 +00:00
|
|
|
case "minecraft:wheat":
|
2021-04-06 04:14:06 +00:00
|
|
|
WHEAT = itemEntry;
|
2020-11-11 18:13:13 +00:00
|
|
|
break;
|
2021-04-08 23:52:54 +00:00
|
|
|
case "minecraft:white_banner": // As of 1.16.220, all banners share the same Bedrock ID and differ their colors through their damage value
|
|
|
|
BANNER = itemEntry;
|
|
|
|
break;
|
2021-01-08 00:40:34 +00:00
|
|
|
case "minecraft:writable_book":
|
2021-04-06 04:14:06 +00:00
|
|
|
WRITABLE_BOOK = itemEntry;
|
2021-01-08 00:40:34 +00:00
|
|
|
break;
|
2020-07-07 20:40:19 +00:00
|
|
|
default:
|
|
|
|
break;
|
2020-06-26 15:15:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 17:03:12 +00:00
|
|
|
if (entry.getKey().contains("boat")) {
|
|
|
|
BOATS.add(entry.getValue().get("bedrock_id").intValue());
|
|
|
|
} else if (entry.getKey().contains("bucket") && !entry.getKey().contains("milk")) {
|
|
|
|
BUCKETS.add(entry.getValue().get("bedrock_id").intValue());
|
|
|
|
}
|
|
|
|
|
2021-02-17 23:00:00 +00:00
|
|
|
itemNames.add(entry.getKey());
|
|
|
|
|
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
|
|
|
|
2021-03-31 18:06:05 +00:00
|
|
|
itemNames.add("minecraft:furnace_minecart");
|
|
|
|
itemNames.add("minecraft:spectral_arrow");
|
|
|
|
|
2020-11-17 17:03:12 +00:00
|
|
|
if (lodestoneCompassId == 0) {
|
|
|
|
throw new RuntimeException("Lodestone compass not found in item palette!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the loadstone compass since it doesn't exist on java but we need it for item conversion
|
2020-11-18 06:10:49 +00:00
|
|
|
ITEM_ENTRIES.put(itemIndex, new ItemEntry("minecraft:lodestone_compass", "minecraft:lodestone_compass", itemIndex,
|
2021-04-06 04:14:06 +00:00
|
|
|
lodestoneCompassId, 0, -1, 1));
|
2020-06-29 23:52:32 +00:00
|
|
|
|
2021-03-31 18:06:05 +00:00
|
|
|
if (usingFurnaceMinecart) {
|
2021-04-08 23:32:06 +00:00
|
|
|
// Add the furnace minecart as a custom item
|
2021-03-31 18:06:05 +00:00
|
|
|
int furnaceMinecartId = ITEMS.size() + 1;
|
|
|
|
|
|
|
|
ITEMS.add(new StartGamePacket.ItemEntry("geysermc:furnace_minecart", (short) furnaceMinecartId, true));
|
|
|
|
ITEM_ENTRIES.put(javaFurnaceMinecartId, new ItemEntry("minecraft:furnace_minecart", "geysermc:furnace_minecart", javaFurnaceMinecartId,
|
2021-04-06 04:14:06 +00:00
|
|
|
furnaceMinecartId, 0, -1, 1));
|
|
|
|
creativeItems.add(ItemData.builder()
|
|
|
|
.netId(netId)
|
|
|
|
.id(furnaceMinecartId)
|
|
|
|
.count(1).build());
|
2021-03-31 18:06:05 +00:00
|
|
|
|
|
|
|
NbtMapBuilder builder = NbtMap.builder();
|
|
|
|
builder.putString("name", "geysermc:furnace_minecart")
|
|
|
|
.putInt("id", furnaceMinecartId);
|
|
|
|
|
|
|
|
NbtMapBuilder componentBuilder = NbtMap.builder();
|
|
|
|
// Conveniently, as of 1.16.200, the furnace minecart has a texture AND translation string already.
|
|
|
|
componentBuilder.putCompound("minecraft:icon", NbtMap.builder().putString("texture", "minecart_furnace").build());
|
|
|
|
componentBuilder.putCompound("minecraft:display_name", NbtMap.builder().putString("value", "item.minecartFurnace.name").build());
|
|
|
|
|
|
|
|
// Indicate that the arm animation should play on rails
|
|
|
|
List<NbtMap> useOnTag = Collections.singletonList(NbtMap.builder().putString("tags", "q.any_tag('rail')").build());
|
|
|
|
componentBuilder.putCompound("minecraft:entity_placer", NbtMap.builder()
|
|
|
|
.putList("dispense_on", NbtType.COMPOUND, useOnTag)
|
|
|
|
.putString("entity", "minecraft:minecart")
|
|
|
|
.putList("use_on", NbtType.COMPOUND, useOnTag)
|
|
|
|
.build());
|
|
|
|
|
|
|
|
NbtMapBuilder itemProperties = NbtMap.builder();
|
|
|
|
// We always want to allow offhand usage when we can - matches Java Edition
|
|
|
|
itemProperties.putBoolean("allow_off_hand", true);
|
|
|
|
itemProperties.putBoolean("hand_equipped", false);
|
|
|
|
itemProperties.putInt("max_stack_size", 1);
|
|
|
|
itemProperties.putString("creative_group", "itemGroup.name.minecart");
|
|
|
|
itemProperties.putInt("creative_category", 4); // 4 - "Items"
|
|
|
|
|
|
|
|
componentBuilder.putCompound("item_properties", itemProperties.build());
|
|
|
|
builder.putCompound("components", componentBuilder.build());
|
|
|
|
FURNACE_MINECART_DATA = new ComponentItemData("geysermc:furnace_minecart", builder.build());
|
|
|
|
} else {
|
|
|
|
FURNACE_MINECART_DATA = null;
|
|
|
|
}
|
|
|
|
|
2019-11-10 02:20:47 +00:00
|
|
|
CREATIVE_ITEMS = creativeItems.toArray(new ItemData[0]);
|
2021-02-17 23:00:00 +00:00
|
|
|
|
|
|
|
ITEM_NAMES = itemNames.toArray(new String[0]);
|
2021-04-08 23:32:06 +00:00
|
|
|
|
|
|
|
Set<String> javaOnlyItems = new ObjectOpenHashSet<>();
|
|
|
|
Collections.addAll(javaOnlyItems, "minecraft:spectral_arrow", "minecraft:debug_stick",
|
|
|
|
"minecraft:knowledge_book", "minecraft:tipped_arrow");
|
|
|
|
if (!usingFurnaceMinecart) {
|
|
|
|
javaOnlyItems.add("minecraft:furnace_minecart");
|
|
|
|
}
|
|
|
|
JAVA_ONLY_ITEMS = ImmutableSet.copyOf(javaOnlyItems);
|
2020-05-25 01:07:05 +00:00
|
|
|
}
|
2020-04-26 04:55:06 +00:00
|
|
|
|
2021-04-10 04:31:24 +00:00
|
|
|
/* pre-1.16.220 support start */
|
|
|
|
|
|
|
|
private static ItemData[] LEGACY_CREATIVE_CONTENTS = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Built on the fly so extra memory isn't used if there are no <=1.16.210 clients joining.
|
|
|
|
*
|
|
|
|
* @return a list of creative items built for versions before 1.16.220.
|
|
|
|
*/
|
|
|
|
public static ItemData[] getPre1_16_220CreativeContents() {
|
|
|
|
if (LEGACY_CREATIVE_CONTENTS != null) {
|
|
|
|
return LEGACY_CREATIVE_CONTENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pre-1.16.220 relies on item damage values that the creative content packet drops
|
|
|
|
ItemData[] creativeContents = new ItemData[CREATIVE_ITEMS.length];
|
|
|
|
for (int i = 0; i < CREATIVE_ITEMS.length; i++) {
|
|
|
|
ItemData item = CREATIVE_ITEMS[i];
|
|
|
|
if (item.getBlockRuntimeId() != 0) {
|
|
|
|
creativeContents[i] = item.toBuilder().damage(getItem(item).getBedrockData()).build();
|
|
|
|
} else {
|
|
|
|
// No block runtime ID means that this item is backwards-compatible
|
|
|
|
creativeContents[i] = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LEGACY_CREATIVE_CONTENTS = creativeContents;
|
|
|
|
return creativeContents;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pre-1.16.220 support end */
|
|
|
|
|
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) {
|
2021-04-10 04:31:24 +00:00
|
|
|
boolean isBlock = data.getBlockRuntimeId() != 0;
|
|
|
|
boolean hasDamage = data.getDamage() != 0;
|
|
|
|
|
2020-05-25 01:07:05 +00:00
|
|
|
for (ItemEntry itemEntry : ITEM_ENTRIES.values()) {
|
2021-04-10 04:31:24 +00:00
|
|
|
if (itemEntry.getBedrockId() == data.getId()) {
|
|
|
|
if (isBlock && !hasDamage) { // Pre-1.16.220 will not use block runtime IDs at all, so we shouldn't check either
|
|
|
|
if (data.getBlockRuntimeId() != itemEntry.getBedrockBlockId()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!(itemEntry.getBedrockData() == data.getDamage() ||
|
|
|
|
// Make exceptions for potions and tipped arrows, whose damage values can vary
|
|
|
|
(itemEntry.getJavaIdentifier().endsWith("potion") || itemEntry.getJavaIdentifier().equals("minecraft:arrow")))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2020-11-30 15:55:35 +00:00
|
|
|
if (!JAVA_ONLY_ITEMS.contains(itemEntry.getJavaIdentifier())) {
|
|
|
|
// From a Bedrock item data, we aren't getting one of these items
|
|
|
|
return itemEntry;
|
|
|
|
}
|
2020-05-25 01:07:05 +00:00
|
|
|
}
|
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) {
|
2020-11-05 21:36:22 +00:00
|
|
|
return JAVA_IDENTIFIER_MAP.computeIfAbsent(javaIdentifier, key -> {
|
|
|
|
for (ItemEntry entry : ITEM_ENTRIES.values()) {
|
|
|
|
if (entry.getJavaIdentifier().equals(key)) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
2019-07-11 22:39:28 +00:00
|
|
|
}
|
2020-05-25 01:07:05 +00:00
|
|
|
}
|