mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
Add holderset class for geyser
This commit is contained in:
parent
5ec330d0b1
commit
fbe702077c
4 changed files with 84 additions and 36 deletions
|
|
@ -42,14 +42,12 @@ 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.Tag;
|
|
||||||
import org.geysermc.geyser.session.cache.tags.TagRegistry;
|
import org.geysermc.geyser.session.cache.tags.TagRegistry;
|
||||||
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;
|
||||||
|
|
||||||
|
|
@ -314,12 +312,10 @@ 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();
|
||||||
|
|
||||||
HolderSet supportedItems = enchantment.supportedItems();
|
int[] supportedItemIds = enchantment.supportedItems().resolve(session, TagRegistry.ITEM);
|
||||||
int[] supportedItemIds = supportedItems.resolve(tagId -> session.getTagCache().get(Tag.createTag(TagRegistry.ITEM, tagId)));
|
|
||||||
boolean canApply = isEnchantedBook(input) || IntStream.of(supportedItemIds).anyMatch(id -> id == input.getJavaId());
|
boolean canApply = isEnchantedBook(input) || IntStream.of(supportedItemIds).anyMatch(id -> id == input.getJavaId());
|
||||||
|
|
||||||
HolderSet exclusiveSet = enchantment.exclusiveSet();
|
int[] incompatibleEnchantments = enchantment.exclusiveSet().resolve(session, TagRegistry.ENCHANTMENT);
|
||||||
int[] incompatibleEnchantments = exclusiveSet.resolve(tagId -> session.getTagCache().get(Tag.createTag(TagRegistry.ENCHANTMENT, tagId)));
|
|
||||||
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)) {
|
||||||
|
|
|
||||||
|
|
@ -25,21 +25,18 @@
|
||||||
|
|
||||||
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.item.Items;
|
import org.geysermc.geyser.item.Items;
|
||||||
import org.geysermc.geyser.registry.Registries;
|
import org.geysermc.geyser.registry.Registries;
|
||||||
import org.geysermc.geyser.session.cache.registry.RegistryEntryContext;
|
import org.geysermc.geyser.session.cache.registry.RegistryEntryContext;
|
||||||
|
import org.geysermc.geyser.session.cache.tags.HolderSet;
|
||||||
import org.geysermc.geyser.translator.text.MessageTranslator;
|
import org.geysermc.geyser.translator.text.MessageTranslator;
|
||||||
|
|
||||||
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.
|
||||||
|
|
@ -58,12 +55,12 @@ public record Enchantment(String identifier,
|
||||||
NbtMap data = context.data();
|
NbtMap data = context.data();
|
||||||
Set<EnchantmentComponent> effects = readEnchantmentComponents(data.getCompound("effects"));
|
Set<EnchantmentComponent> effects = readEnchantmentComponents(data.getCompound("effects"));
|
||||||
|
|
||||||
HolderSet supportedItems = readHolderSet(data.get("supported_items"), itemId -> Registries.JAVA_ITEM_IDENTIFIERS.getOrDefault(itemId.asString(), Items.AIR).javaId());
|
HolderSet supportedItems = HolderSet.readHolderSet(data.get("supported_items"), itemId -> Registries.JAVA_ITEM_IDENTIFIERS.getOrDefault(itemId.asString(), Items.AIR).javaId());
|
||||||
|
|
||||||
int maxLevel = data.getInt("max_level");
|
int maxLevel = data.getInt("max_level");
|
||||||
int anvilCost = data.getInt("anvil_cost");
|
int anvilCost = data.getInt("anvil_cost");
|
||||||
|
|
||||||
HolderSet exclusiveSet = readHolderSet(data.getOrDefault("exclusive_set", null), context::getNetworkId);
|
HolderSet exclusiveSet = HolderSet.readHolderSet(data.get("exclusive_set"), context::getNetworkId);
|
||||||
|
|
||||||
BedrockEnchantment bedrockEnchantment = BedrockEnchantment.getByJavaIdentifier(context.id().asString());
|
BedrockEnchantment bedrockEnchantment = BedrockEnchantment.getByJavaIdentifier(context.id().asString());
|
||||||
|
|
||||||
|
|
@ -84,24 +81,4 @@ 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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,10 +25,6 @@
|
||||||
|
|
||||||
package org.geysermc.geyser.session.cache;
|
package org.geysermc.geyser.session.cache;
|
||||||
|
|
||||||
import static org.geysermc.geyser.session.cache.tags.BlockTag.ALL_BLOCK_TAGS;
|
|
||||||
import static org.geysermc.geyser.session.cache.tags.EnchantmentTag.ALL_ENCHANTMENT_TAGS;
|
|
||||||
import static org.geysermc.geyser.session.cache.tags.ItemTag.ALL_ITEM_TAGS;
|
|
||||||
|
|
||||||
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.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
|
||||||
79
core/src/main/java/org/geysermc/geyser/session/cache/tags/HolderSet.java
vendored
Normal file
79
core/src/main/java/org/geysermc/geyser/session/cache/tags/HolderSet.java
vendored
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
/*
|
||||||
|
* 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.session.cache.tags;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import lombok.Data;
|
||||||
|
import net.kyori.adventure.key.Key;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
import org.geysermc.geyser.session.GeyserSession;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public final class HolderSet {
|
||||||
|
|
||||||
|
private final @Nullable Key tagId;
|
||||||
|
private final int @Nullable [] holders;
|
||||||
|
|
||||||
|
public HolderSet(int @NonNull [] holders) {
|
||||||
|
this.tagId = null;
|
||||||
|
this.holders = holders;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HolderSet(@NonNull Key tagId) {
|
||||||
|
this.tagId = tagId;
|
||||||
|
this.holders = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] resolve(GeyserSession session, TagRegistry registry) {
|
||||||
|
if (holders != null) {
|
||||||
|
return holders;
|
||||||
|
}
|
||||||
|
|
||||||
|
return session.getTagCache().get(Tag.createTag(registry, tagId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public 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 if (stringTag.isEmpty()) {
|
||||||
|
return new HolderSet(new int[]{});
|
||||||
|
}
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue