mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
Mostly fix custom enchantments
This commit is contained in:
parent
0a928c41b0
commit
d074f50f1b
4 changed files with 84 additions and 41 deletions
|
|
@ -27,6 +27,7 @@ package org.geysermc.geyser.inventory.updater;
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
||||||
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import org.cloudburstmc.nbt.NbtMap;
|
import org.cloudburstmc.nbt.NbtMap;
|
||||||
import org.cloudburstmc.nbt.NbtMapBuilder;
|
import org.cloudburstmc.nbt.NbtMapBuilder;
|
||||||
|
|
@ -41,11 +42,14 @@ import org.geysermc.geyser.inventory.item.BedrockEnchantment;
|
||||||
import org.geysermc.geyser.item.enchantment.Enchantment;
|
import org.geysermc.geyser.item.enchantment.Enchantment;
|
||||||
import org.geysermc.geyser.item.Items;
|
import org.geysermc.geyser.item.Items;
|
||||||
import org.geysermc.geyser.session.GeyserSession;
|
import org.geysermc.geyser.session.GeyserSession;
|
||||||
|
import org.geysermc.geyser.session.cache.tags.EnchantmentTag;
|
||||||
|
import org.geysermc.geyser.session.cache.tags.ItemTag;
|
||||||
import org.geysermc.geyser.translator.inventory.InventoryTranslator;
|
import org.geysermc.geyser.translator.inventory.InventoryTranslator;
|
||||||
import org.geysermc.geyser.translator.text.MessageTranslator;
|
import org.geysermc.geyser.translator.text.MessageTranslator;
|
||||||
import org.geysermc.geyser.util.ItemUtils;
|
import org.geysermc.geyser.util.ItemUtils;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
|
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentType;
|
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentType;
|
||||||
|
import org.geysermc.mcprotocollib.protocol.data.game.item.component.HolderSet;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.item.component.ItemEnchantments;
|
import org.geysermc.mcprotocollib.protocol.data.game.item.component.ItemEnchantments;
|
||||||
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.inventory.ServerboundRenameItemPacket;
|
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.inventory.ServerboundRenameItemPacket;
|
||||||
|
|
||||||
|
|
@ -310,10 +314,12 @@ public class AnvilInventoryUpdater extends InventoryUpdater {
|
||||||
for (Object2IntMap.Entry<Enchantment> entry : getEnchantments(session, material).object2IntEntrySet()) {
|
for (Object2IntMap.Entry<Enchantment> entry : getEnchantments(session, material).object2IntEntrySet()) {
|
||||||
Enchantment enchantment = entry.getKey();
|
Enchantment enchantment = entry.getKey();
|
||||||
|
|
||||||
boolean canApply = isEnchantedBook(input) || session.getTagCache().is(enchantment.supportedItems(), input);
|
HolderSet supportedItems = enchantment.supportedItems();
|
||||||
var exclusiveSet = enchantment.exclusiveSet();
|
int[] supportedItemIds = supportedItems.resolve(key -> session.getTagCache().get(ItemTag.ALL_ITEM_TAGS.get(key)));
|
||||||
if (exclusiveSet != null) {
|
boolean canApply = isEnchantedBook(input) || IntStream.of(supportedItemIds).anyMatch(id -> id == input.getJavaId());
|
||||||
int[] incompatibleEnchantments = session.getTagCache().get(exclusiveSet);
|
|
||||||
|
HolderSet exclusiveSet = enchantment.exclusiveSet();
|
||||||
|
int[] incompatibleEnchantments = exclusiveSet.resolve(key -> session.getTagCache().get(EnchantmentTag.ALL_ENCHANTMENT_TAGS.get(key)));
|
||||||
for (int i : incompatibleEnchantments) {
|
for (int i : incompatibleEnchantments) {
|
||||||
Enchantment incompatible = session.getRegistryCache().enchantments().byId(i);
|
Enchantment incompatible = session.getRegistryCache().enchantments().byId(i);
|
||||||
if (combinedEnchantments.containsKey(incompatible)) {
|
if (combinedEnchantments.containsKey(incompatible)) {
|
||||||
|
|
@ -323,7 +329,6 @@ public class AnvilInventoryUpdater extends InventoryUpdater {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (canApply || (!bedrock && session.getGameMode() == GameMode.CREATIVE)) {
|
if (canApply || (!bedrock && session.getGameMode() == GameMode.CREATIVE)) {
|
||||||
int currentLevel = combinedEnchantments.getOrDefault(enchantment, 0);
|
int currentLevel = combinedEnchantments.getOrDefault(enchantment, 0);
|
||||||
|
|
|
||||||
|
|
@ -25,18 +25,19 @@
|
||||||
|
|
||||||
package org.geysermc.geyser.item.enchantment;
|
package org.geysermc.geyser.item.enchantment;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import net.kyori.adventure.key.Key;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
import org.cloudburstmc.nbt.NbtMap;
|
import org.cloudburstmc.nbt.NbtMap;
|
||||||
import org.geysermc.geyser.inventory.item.BedrockEnchantment;
|
import org.geysermc.geyser.inventory.item.BedrockEnchantment;
|
||||||
import org.geysermc.geyser.session.cache.tags.EnchantmentTag;
|
|
||||||
import org.geysermc.geyser.session.cache.tags.ItemTag;
|
|
||||||
import org.geysermc.geyser.translator.text.MessageTranslator;
|
import org.geysermc.geyser.translator.text.MessageTranslator;
|
||||||
import org.geysermc.geyser.util.MinecraftKey;
|
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
|
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import org.geysermc.mcprotocollib.protocol.data.game.item.component.HolderSet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param description only populated if {@link #bedrockEnchantment()} is not null.
|
* @param description only populated if {@link #bedrockEnchantment()} is not null.
|
||||||
|
|
@ -44,28 +45,32 @@ import java.util.Set;
|
||||||
*/
|
*/
|
||||||
public record Enchantment(String identifier,
|
public record Enchantment(String identifier,
|
||||||
Set<EnchantmentComponent> effects,
|
Set<EnchantmentComponent> effects,
|
||||||
ItemTag supportedItems,
|
HolderSet supportedItems,
|
||||||
int maxLevel,
|
int maxLevel,
|
||||||
String description,
|
String description,
|
||||||
int anvilCost,
|
int anvilCost,
|
||||||
@Nullable EnchantmentTag exclusiveSet,
|
HolderSet exclusiveSet,
|
||||||
@Nullable BedrockEnchantment bedrockEnchantment) {
|
@Nullable BedrockEnchantment bedrockEnchantment) {
|
||||||
|
|
||||||
// Implementation note: I have a feeling the tags can be a list of items, because in vanilla they're HolderSet classes.
|
public static Enchantment read(Function<Key, Integer> keyIdMapping, RegistryEntry entry) {
|
||||||
// I'm not sure how that's wired over the network, so we'll put it off.
|
|
||||||
public static Enchantment read(RegistryEntry entry) {
|
|
||||||
NbtMap data = entry.getData();
|
NbtMap data = entry.getData();
|
||||||
Set<EnchantmentComponent> effects = readEnchantmentComponents(data.getCompound("effects"));
|
Set<EnchantmentComponent> effects = readEnchantmentComponents(data.getCompound("effects"));
|
||||||
String supportedItems = data.getString("supported_items").substring(1); // Remove '#' at beginning that indicates tag
|
|
||||||
|
HolderSet supportedItems = readHolderSet(data.get("supported_items"), keyIdMapping);
|
||||||
|
|
||||||
int maxLevel = data.getInt("max_level");
|
int maxLevel = data.getInt("max_level");
|
||||||
int anvilCost = data.getInt("anvil_cost");
|
int anvilCost = data.getInt("anvil_cost");
|
||||||
String exclusiveSet = data.getString("exclusive_set", null);
|
|
||||||
EnchantmentTag exclusiveSetTag = exclusiveSet == null ? null : EnchantmentTag.ALL_ENCHANTMENT_TAGS.get(MinecraftKey.key(exclusiveSet.substring(1)));
|
HolderSet exclusiveSet = readHolderSet(data.getOrDefault("exclusive_set", null), keyIdMapping);
|
||||||
|
|
||||||
BedrockEnchantment bedrockEnchantment = BedrockEnchantment.getByJavaIdentifier(entry.getId().asString());
|
BedrockEnchantment bedrockEnchantment = BedrockEnchantment.getByJavaIdentifier(entry.getId().asString());
|
||||||
|
|
||||||
|
// TODO - description is a component. So if a hardcoded literal string is given, this will display normally on Java,
|
||||||
|
// but Geyser will attempt to lookup the literal string as translation - and will fail, displaying an empty string as enchantment name.
|
||||||
String description = bedrockEnchantment == null ? MessageTranslator.deserializeDescription(data) : null;
|
String description = bedrockEnchantment == null ? MessageTranslator.deserializeDescription(data) : null;
|
||||||
|
|
||||||
return new Enchantment(entry.getId().asString(), effects, ItemTag.ALL_ITEM_TAGS.get(MinecraftKey.key(supportedItems)), maxLevel,
|
return new Enchantment(entry.getId().asString(), effects, supportedItems, maxLevel,
|
||||||
description, anvilCost, exclusiveSetTag, bedrockEnchantment);
|
description, anvilCost, exclusiveSet, bedrockEnchantment);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Set<EnchantmentComponent> readEnchantmentComponents(NbtMap effects) {
|
private static Set<EnchantmentComponent> readEnchantmentComponents(NbtMap effects) {
|
||||||
|
|
@ -77,4 +82,24 @@ public record Enchantment(String identifier,
|
||||||
}
|
}
|
||||||
return Set.copyOf(components); // Also ensures any empty sets are consolidated
|
return Set.copyOf(components); // Also ensures any empty sets are consolidated
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO holder set util?
|
||||||
|
private static HolderSet readHolderSet(@Nullable Object holderSet, Function<Key, Integer> keyIdMapping) {
|
||||||
|
if (holderSet == null) {
|
||||||
|
return new HolderSet(new int[]{});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (holderSet instanceof String stringTag) {
|
||||||
|
// Tag
|
||||||
|
if (stringTag.startsWith("#")) {
|
||||||
|
return new HolderSet(Key.key(stringTag.substring(1))); // Remove '#' at beginning that indicates tag
|
||||||
|
} else {
|
||||||
|
return new HolderSet(new int[]{keyIdMapping.apply(Key.key(stringTag))});
|
||||||
|
}
|
||||||
|
} else if (holderSet instanceof List<?> list) {
|
||||||
|
// Assume the list is a list of strings
|
||||||
|
return new HolderSet(list.stream().map(o -> (String) o).map(Key::key).map(keyIdMapping).mapToInt(Integer::intValue).toArray());
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("Holder set must either be a tag, a string ID or a list of string IDs");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ import org.cloudburstmc.nbt.NbtMap;
|
||||||
import org.cloudburstmc.nbt.NbtType;
|
import org.cloudburstmc.nbt.NbtType;
|
||||||
import org.cloudburstmc.protocol.bedrock.data.TrimMaterial;
|
import org.cloudburstmc.protocol.bedrock.data.TrimMaterial;
|
||||||
import org.cloudburstmc.protocol.bedrock.data.TrimPattern;
|
import org.cloudburstmc.protocol.bedrock.data.TrimPattern;
|
||||||
|
import org.cloudburstmc.protocol.common.util.TriFunction;
|
||||||
import org.geysermc.geyser.GeyserImpl;
|
import org.geysermc.geyser.GeyserImpl;
|
||||||
import org.geysermc.geyser.entity.type.living.animal.tameable.WolfEntity;
|
import org.geysermc.geyser.entity.type.living.animal.tameable.WolfEntity;
|
||||||
import org.geysermc.geyser.inventory.item.BannerPattern;
|
import org.geysermc.geyser.inventory.item.BannerPattern;
|
||||||
|
|
@ -59,7 +60,6 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.BiFunction;
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.ToIntFunction;
|
import java.util.function.ToIntFunction;
|
||||||
|
|
||||||
|
|
@ -76,16 +76,16 @@ public final class RegistryCache {
|
||||||
private static final Map<Key, BiConsumer<RegistryCache, List<RegistryEntry>>> REGISTRIES = new HashMap<>();
|
private static final Map<Key, BiConsumer<RegistryCache, List<RegistryEntry>>> REGISTRIES = new HashMap<>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
register("chat_type", cache -> cache.chatTypes, ($, entry) -> TextDecoration.readChatType(entry));
|
register("chat_type", cache -> cache.chatTypes, ($, n, entry) -> TextDecoration.readChatType(entry));
|
||||||
register("dimension_type", cache -> cache.dimensions, ($, entry) -> JavaDimension.read(entry));
|
register("dimension_type", cache -> cache.dimensions, ($, n, entry) -> JavaDimension.read(entry));
|
||||||
register("enchantment", cache -> cache.enchantments, ($, entry) -> Enchantment.read(entry));
|
register("enchantment", cache -> cache.enchantments, ($, n, entry) -> Enchantment.read(n, entry));
|
||||||
register("jukebox_song", cache -> cache.jukeboxSongs, ($, entry) -> JukeboxSong.read(entry));
|
register("jukebox_song", cache -> cache.jukeboxSongs, ($, n, entry) -> JukeboxSong.read(entry));
|
||||||
register("painting_variant", cache -> cache.paintings, ($, entry) -> PaintingType.getByName(entry.getId()));
|
register("painting_variant", cache -> cache.paintings, ($, n, entry) -> PaintingType.getByName(entry.getId()));
|
||||||
register("trim_material", cache -> cache.trimMaterials, TrimRecipe::readTrimMaterial);
|
register("trim_material", cache -> cache.trimMaterials, (session, n, entry) -> TrimRecipe.readTrimMaterial(session, entry));
|
||||||
register("trim_pattern", cache -> cache.trimPatterns, TrimRecipe::readTrimPattern);
|
register("trim_pattern", cache -> cache.trimPatterns, (session, n, entry) -> TrimRecipe.readTrimPattern(session, entry));
|
||||||
register("worldgen/biome", (cache, array) -> cache.biomeTranslations = array, BiomeTranslator::loadServerBiome);
|
register("worldgen/biome", (cache, array) -> cache.biomeTranslations = array, BiomeTranslator::loadServerBiome);
|
||||||
register("banner_pattern", cache -> cache.bannerPatterns, ($, entry) -> BannerPattern.getByJavaIdentifier(entry.getId()));
|
register("banner_pattern", cache -> cache.bannerPatterns, ($, n, entry) -> BannerPattern.getByJavaIdentifier(entry.getId()));
|
||||||
register("wolf_variant", cache -> cache.wolfVariants, ($, entry) -> WolfEntity.BuiltInWolfVariant.getByJavaIdentifier(entry.getId().asString()));
|
register("wolf_variant", cache -> cache.wolfVariants, ($, n, entry) -> WolfEntity.BuiltInWolfVariant.getByJavaIdentifier(entry.getId().asString()));
|
||||||
|
|
||||||
// Load from MCProtocolLib's classloader
|
// Load from MCProtocolLib's classloader
|
||||||
NbtMap tag = MinecraftProtocol.loadNetworkCodec();
|
NbtMap tag = MinecraftProtocol.loadNetworkCodec();
|
||||||
|
|
@ -149,25 +149,34 @@ public final class RegistryCache {
|
||||||
* @param reader converts the RegistryEntry NBT into a class file
|
* @param reader converts the RegistryEntry NBT into a class file
|
||||||
* @param <T> the class that represents these entries.
|
* @param <T> the class that represents these entries.
|
||||||
*/
|
*/
|
||||||
private static <T> void register(String registry, Function<RegistryCache, JavaRegistry<T>> localCacheFunction, BiFunction<GeyserSession, RegistryEntry, T> reader) {
|
private static <T> void register(String registry, Function<RegistryCache, JavaRegistry<T>> localCacheFunction, TriFunction<GeyserSession, Function<Key, Integer>, RegistryEntry, T> reader) {
|
||||||
Key key = MinecraftKey.key(registry);
|
Key registryKey = MinecraftKey.key(registry);
|
||||||
REGISTRIES.put(key, (registryCache, entries) -> {
|
REGISTRIES.put(registryKey, (registryCache, entries) -> {
|
||||||
Map<Key, NbtMap> localRegistry = null;
|
Map<Key, NbtMap> localRegistry = null;
|
||||||
JavaRegistry<T> localCache = localCacheFunction.apply(registryCache);
|
JavaRegistry<T> localCache = localCacheFunction.apply(registryCache);
|
||||||
// Clear each local cache every time a new registry entry is given to us
|
// Clear each local cache every time a new registry entry is given to us
|
||||||
// (e.g. proxy server switches)
|
// (e.g. proxy server switches)
|
||||||
|
|
||||||
|
Map<Key, Integer> entryIdMap = new HashMap<>();
|
||||||
|
for (int i = 0; i < entries.size(); i++) {
|
||||||
|
entryIdMap.put(entries.get(i).getId(), i);
|
||||||
|
}
|
||||||
|
|
||||||
List<T> builder = new ArrayList<>(entries.size());
|
List<T> builder = new ArrayList<>(entries.size());
|
||||||
for (int i = 0; i < entries.size(); i++) {
|
for (int i = 0; i < entries.size(); i++) {
|
||||||
RegistryEntry entry = entries.get(i);
|
RegistryEntry entry = entries.get(i);
|
||||||
// If the data is null, that's the server telling us we need to use our default values.
|
// If the data is null, that's the server telling us we need to use our default values.
|
||||||
if (entry.getData() == null) {
|
if (entry.getData() == null) {
|
||||||
if (localRegistry == null) { // Lazy initialize
|
if (localRegistry == null) { // Lazy initialize
|
||||||
localRegistry = DEFAULTS.get(key);
|
localRegistry = DEFAULTS.get(registryKey);
|
||||||
}
|
}
|
||||||
entry = new RegistryEntry(entry.getId(), localRegistry.get(entry.getId()));
|
entry = new RegistryEntry(entry.getId(), localRegistry.get(entry.getId()));
|
||||||
}
|
}
|
||||||
// This is what Geyser wants to keep as a value for this registry.
|
// This is what Geyser wants to keep as a value for this registry.
|
||||||
T cacheEntry = reader.apply(registryCache.session, entry);
|
T cacheEntry = reader.apply(registryCache.session, key -> entryIdMap.getOrDefault(key, 0), entry);
|
||||||
|
if (cacheEntry instanceof Enchantment) {
|
||||||
|
System.out.println(cacheEntry);
|
||||||
|
}
|
||||||
builder.add(i, cacheEntry);
|
builder.add(i, cacheEntry);
|
||||||
}
|
}
|
||||||
localCache.reset(builder);
|
localCache.reset(builder);
|
||||||
|
|
|
||||||
|
|
@ -130,8 +130,12 @@ public final class TagCache {
|
||||||
return contains(values, item.javaId());
|
return contains(values, item.javaId());
|
||||||
}
|
}
|
||||||
|
|
||||||
public int[] get(EnchantmentTag tag) {
|
public int[] get(ItemTag itemTag) {
|
||||||
return this.enchantments[tag.ordinal()];
|
return this.items[itemTag.ordinal()];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] get(EnchantmentTag enchantmentTag) {
|
||||||
|
return this.enchantments[enchantmentTag.ordinal()];
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean contains(int[] array, int i) {
|
private static boolean contains(int[] array, int i) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue