Skip to content

Commit

Permalink
Map Java tags to Bedrock tags in recipes where possible
Browse files Browse the repository at this point in the history
Bedrock tags have been generated from Endstone's devtools.
  • Loading branch information
Camotoy committed Nov 2, 2024
1 parent 0eec65c commit 734e429
Show file tree
Hide file tree
Showing 9 changed files with 4,167 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder;
Expand All @@ -48,6 +49,7 @@
import org.geysermc.geyser.registry.loader.SoundTranslatorRegistryLoader;
import org.geysermc.geyser.registry.populator.ItemRegistryPopulator;
import org.geysermc.geyser.registry.populator.PacketRegistryPopulator;
import org.geysermc.geyser.registry.populator.TagRegistryPopulator;
import org.geysermc.geyser.registry.provider.ProviderSupplier;
import org.geysermc.geyser.registry.type.ItemMappings;
import org.geysermc.geyser.registry.type.ParticleMapping;
Expand Down Expand Up @@ -163,6 +165,11 @@ public final class Registries {
*/
public static final SimpleMappedDeferredRegistry<String, ResourcePack> RESOURCE_PACKS = SimpleMappedDeferredRegistry.create(GeyserImpl.getInstance().packDirectory(), RegistryLoaders.RESOURCE_PACKS);

/**
* A versioned registry holding most Bedrock tags, with the Java item list (sorted) being the key, and the tag name as the value.
*/
public static final VersionedRegistry<Object2ObjectMap<int[], String>> TAGS = VersionedRegistry.create(RegistryLoaders.empty(Int2ObjectOpenHashMap::new));

/**
* A mapped registry holding sound identifiers to their corresponding {@link SoundMapping}.
*/
Expand Down Expand Up @@ -202,6 +209,7 @@ public static void load() {
public static void populate() {
PacketRegistryPopulator.populate();
ItemRegistryPopulator.populate();
TagRegistryPopulator.populate();

// potion mixes depend on other registries
POTION_MIXES.load();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2024 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.geyser.registry.populator;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import it.unimi.dsi.fastutil.Hash;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.ObjectIntPair;
import org.cloudburstmc.protocol.bedrock.codec.v671.Bedrock_v671;
import org.cloudburstmc.protocol.bedrock.codec.v685.Bedrock_v685;
import org.cloudburstmc.protocol.bedrock.codec.v712.Bedrock_v712;
import org.cloudburstmc.protocol.bedrock.codec.v729.Bedrock_v729;
import org.cloudburstmc.protocol.bedrock.codec.v748.Bedrock_v748;
import org.geysermc.geyser.GeyserBootstrap;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.item.type.Item;
import org.geysermc.geyser.registry.Registries;
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.registry.type.ItemMappings;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public final class TagRegistryPopulator {
private static final Gson GSON = new GsonBuilder().create(); // temporary

public static void populate() {
List<ObjectIntPair<String>> paletteVersions = List.of(
ObjectIntPair.of("1_20_80", Bedrock_v671.CODEC.getProtocolVersion()),
ObjectIntPair.of("1_21_0", Bedrock_v685.CODEC.getProtocolVersion()),
ObjectIntPair.of("1_21_20", Bedrock_v712.CODEC.getProtocolVersion()),
ObjectIntPair.of("1_21_30", Bedrock_v729.CODEC.getProtocolVersion()),
ObjectIntPair.of("1_21_40", Bedrock_v748.CODEC.getProtocolVersion())
);
TypeToken<Map<String, List<String>>> type = new TypeToken<>() {};

GeyserBootstrap bootstrap = GeyserImpl.getInstance().getBootstrap();

for (var palette : paletteVersions) {
ItemMappings mappings = Registries.ITEMS.forVersion(palette.rightInt());

Map<String, List<String>> bedrockTags;
try (InputStream stream = bootstrap.getResourceOrThrow(String.format("bedrock/item_tags.%s.json", palette.left()))) {
bedrockTags = GSON.fromJson(new InputStreamReader(stream), type);
} catch (Exception e) {
throw new AssertionError("Unable to load Bedrock runtime item IDs", e);
}

Object2ObjectMap<int[], String> javaItemsToBedrockTag = new Object2ObjectOpenCustomHashMap<>(new Hash.Strategy<>() {
// Necessary so arrays can actually be compared
@Override
public int hashCode(int[] o) {
return Arrays.hashCode(o);
}

@Override
public boolean equals(int[] a, int[] b) {
return Arrays.equals(a, b);
}
});

for (var entry : bedrockTags.entrySet()) {
List<String> value = entry.getValue();
if (value.isEmpty() || value.size() == 1) {
// For our usecase, we don't need this. Empty values are worthless; one value can just be a reference
// to the item itself, instead of the tag.
continue;
}

// In some cases, the int list will need to be minimized
IntList javaNetworkIds = new IntArrayList(value.size());
for (int i = 0; i < value.size(); i++) {
String bedrockIdentifier = value.get(i);
Item javaItem = Registries.JAVA_ITEM_IDENTIFIERS.get(bedrockIdentifier);
if (javaItem == null) {
// Time to search the long way around.
for (ItemMapping mapping : mappings.getItems()) {
if (mapping.getBedrockIdentifier().equals(bedrockIdentifier)) {
javaItem = mapping.getJavaItem();
break;
}
}
}
if (javaItem == null) {
// Triggers for Bedrock-only spawn eggs. We don't care.
continue;
}

javaNetworkIds.add(javaItem.javaId());
}

int[] javaNetworkIdArray = javaNetworkIds.toIntArray();
// Sort IDs so equality checks just have to match if each is equal and not necessarily an order difference.
Arrays.sort(javaNetworkIdArray);

javaItemsToBedrockTag.put(javaNetworkIdArray, entry.getKey());
}

Registries.TAGS.register(palette.rightInt(), javaItemsToBedrockTag);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,18 @@ public void loadPacket(GeyserSession session, ClientboundUpdateTagsPacket packet
}
}

loadTags(registryTags, registry);
loadTags(registryTags, registry, registry == JavaRegistries.ITEM);
}
}

private void loadTags(Map<Key, int[]> packetTags, JavaRegistryKey<?> registry) {
private void loadTags(Map<Key, int[]> packetTags, JavaRegistryKey<?> registry, boolean sort) {
for (Map.Entry<Key, int[]> tag : packetTags.entrySet()) {
this.tags.put(new Tag<>(registry, tag.getKey()), tag.getValue());
int[] value = tag.getValue();
if (sort) {
// Used in RecipeBookAddTranslator
Arrays.sort(value);
}
this.tags.put(new Tag<>(registry, tag.getKey()), value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.cloudburstmc.protocol.bedrock.data.inventory.crafting.recipe.SmithingTransformRecipeData;
import org.cloudburstmc.protocol.bedrock.data.inventory.descriptor.DefaultDescriptor;
import org.cloudburstmc.protocol.bedrock.data.inventory.descriptor.ItemDescriptorWithCount;
import org.cloudburstmc.protocol.bedrock.data.inventory.descriptor.ItemTagDescriptor;
import org.cloudburstmc.protocol.bedrock.packet.CraftingDataPacket;
import org.cloudburstmc.protocol.bedrock.packet.UnlockedRecipesPacket;
import org.geysermc.geyser.inventory.recipe.GeyserRecipe;
Expand Down Expand Up @@ -190,6 +191,7 @@ public void translate(GeyserSession session, ClientboundRecipeBookAddPacket pack
TAG_TO_ITEM_DESCRIPTOR_CACHE.remove();
}

// Arrays are usually an issue in maps, but because it's referencing the tag array that is unchanged, it actually works out for us.
private static final ThreadLocal<Map<int[], List<ItemDescriptorWithCount>>> TAG_TO_ITEM_DESCRIPTOR_CACHE = ThreadLocal.withInitial(Object2ObjectOpenHashMap::new);

private List<ItemDescriptorWithCount> translateToInput(GeyserSession session, SlotDisplay slotDisplay) {
Expand Down Expand Up @@ -228,6 +230,18 @@ private List<ItemDescriptorWithCount> translateToInput(GeyserSession session, Sl
// Cache is implemented as, presumably, an item tag will be used multiple times in succession
// (E.G. a chest with planks tags)
return TAG_TO_ITEM_DESCRIPTOR_CACHE.get().computeIfAbsent(items, key -> {
var bedrockTags = Registries.TAGS.forVersion(session.getUpstream().getProtocolVersion());
String bedrockTag = bedrockTags.get(key);
if (bedrockTag != null) {
return Collections.singletonList(
new ItemDescriptorWithCount(new ItemTagDescriptor(bedrockTag), 1)
);
}

// In the future, we can probably search through and use subsets of tags as well.
// I.E. if a Bedrock tag contains [stone stone_brick] and the Java tag uses [stone stone_brick bricks]
// we can still use that Bedrock tag alongside plain item descriptors for "bricks".

Set<ItemDescriptorWithCount> itemDescriptors = new HashSet<>();
for (int item : key) {
itemDescriptors.add(fromItem(session, item));
Expand Down
Loading

0 comments on commit 734e429

Please sign in to comment.