mirror of
https://github.com/GeyserMC/Geyser.git
synced 2025-12-19 14:59:27 +00:00
Reduce direct access to TagCache
This commit is contained in:
@@ -50,7 +50,7 @@ public class DolphinEntity extends AgeableWaterEntity {
|
|||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
protected InteractiveTag testMobInteraction(@NonNull Hand hand, @NonNull GeyserItemStack itemInHand) {
|
protected InteractiveTag testMobInteraction(@NonNull Hand hand, @NonNull GeyserItemStack itemInHand) {
|
||||||
if (!itemInHand.isEmpty() && session.getTagCache().is(ItemTag.FISHES, itemInHand)) {
|
if (!itemInHand.isEmpty() && itemInHand.is(session, ItemTag.FISHES)) {
|
||||||
return InteractiveTag.FEED;
|
return InteractiveTag.FEED;
|
||||||
}
|
}
|
||||||
return super.testMobInteraction(hand, itemInHand);
|
return super.testMobInteraction(hand, itemInHand);
|
||||||
@@ -59,7 +59,7 @@ public class DolphinEntity extends AgeableWaterEntity {
|
|||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
protected InteractionResult mobInteract(@NonNull Hand hand, @NonNull GeyserItemStack itemInHand) {
|
protected InteractionResult mobInteract(@NonNull Hand hand, @NonNull GeyserItemStack itemInHand) {
|
||||||
if (!itemInHand.isEmpty() && session.getTagCache().is(ItemTag.FISHES, itemInHand)) {
|
if (!itemInHand.isEmpty() && itemInHand.is(session, ItemTag.FISHES)) {
|
||||||
// Feed
|
// Feed
|
||||||
return InteractionResult.SUCCESS;
|
return InteractionResult.SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,6 +62,6 @@ public class TadpoleEntity extends AbstractFishEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isFood(GeyserItemStack itemStack) {
|
private boolean isFood(GeyserItemStack itemStack) {
|
||||||
return session.getTagCache().is(ItemTag.FROG_FOOD, itemStack);
|
return itemStack.is(session, ItemTag.FROG_FOOD);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ public abstract class AnimalEntity extends AgeableEntity {
|
|||||||
if (tag == null) {
|
if (tag == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return session.getTagCache().is(tag, itemStack);
|
return itemStack.is(session, tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ public class HappyGhastEntity extends AnimalEntity implements ClientVehicle {
|
|||||||
return super.testMobInteraction(hand, itemInHand);
|
return super.testMobInteraction(hand, itemInHand);
|
||||||
} else {
|
} else {
|
||||||
if (!itemInHand.isEmpty()) {
|
if (!itemInHand.isEmpty()) {
|
||||||
if (session.getTagCache().is(ItemTag.HARNESSES, itemInHand)) {
|
if (itemInHand.is(session, ItemTag.HARNESSES)) {
|
||||||
if (getItemInSlot(EquipmentSlot.BODY).isEmpty()) {
|
if (getItemInSlot(EquipmentSlot.BODY).isEmpty()) {
|
||||||
// Harnesses the ghast
|
// Harnesses the ghast
|
||||||
return InteractiveTag.EQUIP_HARNESS;
|
return InteractiveTag.EQUIP_HARNESS;
|
||||||
@@ -151,7 +151,7 @@ public class HappyGhastEntity extends AnimalEntity implements ClientVehicle {
|
|||||||
return super.mobInteract(hand, itemInHand);
|
return super.mobInteract(hand, itemInHand);
|
||||||
} else {
|
} else {
|
||||||
if (!itemInHand.isEmpty()) {
|
if (!itemInHand.isEmpty()) {
|
||||||
if (session.getTagCache().is(ItemTag.HARNESSES, itemInHand)) {
|
if (itemInHand.is(session, ItemTag.HARNESSES)) {
|
||||||
if (getItemInSlot(EquipmentSlot.BODY).isEmpty()) {
|
if (getItemInSlot(EquipmentSlot.BODY).isEmpty()) {
|
||||||
// Harnesses the ghast
|
// Harnesses the ghast
|
||||||
return InteractionResult.SUCCESS;
|
return InteractionResult.SUCCESS;
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ public class MooshroomEntity extends CowEntity {
|
|||||||
} else if (!isBaby && isAlive() && itemInHand.asItem() == Items.SHEARS) {
|
} else if (!isBaby && isAlive() && itemInHand.asItem() == Items.SHEARS) {
|
||||||
// Shear items
|
// Shear items
|
||||||
return InteractionResult.SUCCESS;
|
return InteractionResult.SUCCESS;
|
||||||
} else if (isBrown && session.getTagCache().is(ItemTag.SMALL_FLOWERS, itemInHand)) {
|
} else if (isBrown && itemInHand.is(session, ItemTag.SMALL_FLOWERS)) {
|
||||||
// ?
|
// ?
|
||||||
return InteractionResult.SUCCESS;
|
return InteractionResult.SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,11 +53,11 @@ public class ParrotEntity extends TameableEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isTameFood(Item item) {
|
private boolean isTameFood(Item item) {
|
||||||
return session.getTagCache().is(ItemTag.PARROT_FOOD, item);
|
return item.is(session, ItemTag.PARROT_FOOD);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isPoisonousFood(Item item) {
|
private boolean isPoisonousFood(Item item) {
|
||||||
return session.getTagCache().is(ItemTag.PARROT_POISONOUS_FOOD, item);
|
return item.is(session, ItemTag.PARROT_POISONOUS_FOOD);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
|
|||||||
@@ -167,8 +167,7 @@ public class WolfEntity extends TameableEntity implements VariantIntHolder {
|
|||||||
&& (!isCurseOfBinding || session.getGameMode().equals(GameMode.CREATIVE))) {
|
&& (!isCurseOfBinding || session.getGameMode().equals(GameMode.CREATIVE))) {
|
||||||
return InteractiveTag.REMOVE_WOLF_ARMOR;
|
return InteractiveTag.REMOVE_WOLF_ARMOR;
|
||||||
}
|
}
|
||||||
if (getFlag(EntityFlag.SITTING) &&
|
if (getFlag(EntityFlag.SITTING) && itemInHand.is(session, repairableItems) &&
|
||||||
session.getTagCache().isItem(repairableItems, itemInHand.asItem()) &&
|
|
||||||
!getItemInSlot(EquipmentSlot.BODY).isEmpty() && getItemInSlot(EquipmentSlot.BODY).isDamaged()) {
|
!getItemInSlot(EquipmentSlot.BODY).isEmpty() && getItemInSlot(EquipmentSlot.BODY).isDamaged()) {
|
||||||
return InteractiveTag.REPAIR_WOLF_ARMOR;
|
return InteractiveTag.REPAIR_WOLF_ARMOR;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ public class CreeperEntity extends MonsterEntity {
|
|||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
protected InteractiveTag testMobInteraction(@NonNull Hand hand, @NonNull GeyserItemStack itemInHand) {
|
protected InteractiveTag testMobInteraction(@NonNull Hand hand, @NonNull GeyserItemStack itemInHand) {
|
||||||
if (session.getTagCache().is(ItemTag.CREEPER_IGNITERS, itemInHand)) {
|
if (itemInHand.is(session, ItemTag.CREEPER_IGNITERS)) {
|
||||||
return InteractiveTag.IGNITE_CREEPER;
|
return InteractiveTag.IGNITE_CREEPER;
|
||||||
} else {
|
} else {
|
||||||
return super.testMobInteraction(hand, itemInHand);
|
return super.testMobInteraction(hand, itemInHand);
|
||||||
@@ -76,7 +76,7 @@ public class CreeperEntity extends MonsterEntity {
|
|||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
protected InteractionResult mobInteract(@NonNull Hand hand, @NonNull GeyserItemStack itemInHand) {
|
protected InteractionResult mobInteract(@NonNull Hand hand, @NonNull GeyserItemStack itemInHand) {
|
||||||
if (session.getTagCache().is(ItemTag.CREEPER_IGNITERS, itemInHand)) {
|
if (itemInHand.is(session, ItemTag.CREEPER_IGNITERS)) {
|
||||||
// Ignite creeper - as of 1.19.3
|
// Ignite creeper - as of 1.19.3
|
||||||
session.playSoundEvent(SoundEvent.IGNITE, position);
|
session.playSoundEvent(SoundEvent.IGNITE, position);
|
||||||
return InteractionResult.SUCCESS;
|
return InteractionResult.SUCCESS;
|
||||||
|
|||||||
@@ -498,7 +498,7 @@ public class SessionPlayerEntity extends PlayerEntity {
|
|||||||
}
|
}
|
||||||
Vector3i pos = getPosition().down(EntityDefinitions.PLAYER.offset()).toInt();
|
Vector3i pos = getPosition().down(EntityDefinitions.PLAYER.offset()).toInt();
|
||||||
BlockState state = session.getGeyser().getWorldManager().blockAt(session, pos);
|
BlockState state = session.getGeyser().getWorldManager().blockAt(session, pos);
|
||||||
if (session.getTagCache().is(BlockTag.CLIMBABLE, state.block())) {
|
if (state.block().is(session, BlockTag.CLIMBABLE)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -683,7 +683,7 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BlockState blockState = ctx.centerBlock();
|
BlockState blockState = ctx.centerBlock();
|
||||||
if (vehicle.getSession().getTagCache().is(BlockTag.CLIMBABLE, blockState.block())) {
|
if (blockState.block().is(vehicle.getSession(), BlockTag.CLIMBABLE)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,12 +40,14 @@ import org.geysermc.geyser.registry.Registries;
|
|||||||
import org.geysermc.geyser.registry.type.ItemMapping;
|
import org.geysermc.geyser.registry.type.ItemMapping;
|
||||||
import org.geysermc.geyser.session.GeyserSession;
|
import org.geysermc.geyser.session.GeyserSession;
|
||||||
import org.geysermc.geyser.session.cache.BundleCache;
|
import org.geysermc.geyser.session.cache.BundleCache;
|
||||||
|
import org.geysermc.geyser.session.cache.registry.JavaRegistries;
|
||||||
import org.geysermc.geyser.session.cache.tags.Tag;
|
import org.geysermc.geyser.session.cache.tags.Tag;
|
||||||
import org.geysermc.geyser.translator.item.ItemTranslator;
|
import org.geysermc.geyser.translator.item.ItemTranslator;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
|
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
|
||||||
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.DataComponentTypes;
|
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentTypes;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;
|
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;
|
||||||
|
import org.geysermc.mcprotocollib.protocol.data.game.item.component.HolderSet;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.recipe.display.slot.EmptySlotDisplay;
|
import org.geysermc.mcprotocollib.protocol.data.game.recipe.display.slot.EmptySlotDisplay;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.recipe.display.slot.ItemSlotDisplay;
|
import org.geysermc.mcprotocollib.protocol.data.game.recipe.display.slot.ItemSlotDisplay;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.recipe.display.slot.ItemStackSlotDisplay;
|
import org.geysermc.mcprotocollib.protocol.data.game.recipe.display.slot.ItemStackSlotDisplay;
|
||||||
@@ -121,7 +123,11 @@ public class GeyserItemStack {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean is(GeyserSession session, Tag<Item> tag) {
|
public boolean is(GeyserSession session, Tag<Item> tag) {
|
||||||
return session.getTagCache().is(tag, this);
|
return session.getTagCache().is(tag, javaId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean is(GeyserSession session, HolderSet set) {
|
||||||
|
return session.getTagCache().is(set, JavaRegistries.ITEM, javaId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -312,7 +312,7 @@ 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.asItem());
|
boolean canApply = isEnchantedBook(input) || enchantment.supportedItems().contains(session, input.asItem());
|
||||||
|
|
||||||
List<Enchantment> incompatibleEnchantments = enchantment.exclusiveSet().resolve(session);
|
List<Enchantment> incompatibleEnchantments = enchantment.exclusiveSet().resolve(session);
|
||||||
for (Enchantment incompatible : incompatibleEnchantments) {
|
for (Enchantment incompatible : incompatibleEnchantments) {
|
||||||
@@ -401,7 +401,7 @@ public class AnvilInventoryUpdater extends InventoryUpdater {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return session.getTagCache().isItem(repairable, material.asItem());
|
return material.is(session, repairable);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isRenaming(GeyserSession session, AnvilContainer anvilContainer, boolean bedrock) {
|
private boolean isRenaming(GeyserSession session, AnvilContainer anvilContainer, boolean bedrock) {
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ import org.geysermc.geyser.registry.type.ItemMapping;
|
|||||||
import org.geysermc.geyser.registry.type.ItemMappings;
|
import org.geysermc.geyser.registry.type.ItemMappings;
|
||||||
import org.geysermc.geyser.session.GeyserSession;
|
import org.geysermc.geyser.session.GeyserSession;
|
||||||
import org.geysermc.geyser.session.cache.registry.JavaRegistries;
|
import org.geysermc.geyser.session.cache.registry.JavaRegistries;
|
||||||
|
import org.geysermc.geyser.session.cache.tags.Tag;
|
||||||
import org.geysermc.geyser.text.ChatColor;
|
import org.geysermc.geyser.text.ChatColor;
|
||||||
import org.geysermc.geyser.text.MinecraftLocale;
|
import org.geysermc.geyser.text.MinecraftLocale;
|
||||||
import org.geysermc.geyser.translator.item.BedrockItemBuilder;
|
import org.geysermc.geyser.translator.item.BedrockItemBuilder;
|
||||||
@@ -53,6 +54,7 @@ import org.geysermc.geyser.util.MinecraftKey;
|
|||||||
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.DataComponentTypes;
|
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentTypes;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;
|
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;
|
||||||
|
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.jetbrains.annotations.UnmodifiableView;
|
import org.jetbrains.annotations.UnmodifiableView;
|
||||||
|
|
||||||
@@ -101,6 +103,14 @@ public class Item {
|
|||||||
return baseComponents.getOrDefault(DataComponentTypes.MAX_STACK_SIZE, 1);
|
return baseComponents.getOrDefault(DataComponentTypes.MAX_STACK_SIZE, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean is(GeyserSession session, Tag<Item> tag) {
|
||||||
|
return session.getTagCache().is(tag, javaId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean is(GeyserSession session, HolderSet set) {
|
||||||
|
return session.getTagCache().is(set, JavaRegistries.ITEM, javaId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an unmodifiable {@link DataComponents} view containing known data components.
|
* Returns an unmodifiable {@link DataComponents} view containing known data components.
|
||||||
* Optionally, additional components can be provided to replace (or add to)
|
* Optionally, additional components can be provided to replace (or add to)
|
||||||
|
|||||||
@@ -41,7 +41,10 @@ import org.geysermc.geyser.level.block.property.Property;
|
|||||||
import org.geysermc.geyser.level.physics.PistonBehavior;
|
import org.geysermc.geyser.level.physics.PistonBehavior;
|
||||||
import org.geysermc.geyser.registry.BlockRegistries;
|
import org.geysermc.geyser.registry.BlockRegistries;
|
||||||
import org.geysermc.geyser.session.GeyserSession;
|
import org.geysermc.geyser.session.GeyserSession;
|
||||||
|
import org.geysermc.geyser.session.cache.registry.JavaRegistries;
|
||||||
|
import org.geysermc.geyser.session.cache.tags.Tag;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
|
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
|
||||||
|
import org.geysermc.mcprotocollib.protocol.data.game.item.component.HolderSet;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockEntityType;
|
import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockEntityType;
|
||||||
import org.intellij.lang.annotations.Subst;
|
import org.intellij.lang.annotations.Subst;
|
||||||
|
|
||||||
@@ -215,6 +218,14 @@ public class Block {
|
|||||||
this.javaId = javaId;
|
this.javaId = javaId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean is(GeyserSession session, Tag<Block> tag) {
|
||||||
|
return session.getTagCache().is(tag, javaId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean is(GeyserSession session, HolderSet set) {
|
||||||
|
return session.getTagCache().is(set, JavaRegistries.BLOCK, javaId);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Block{" +
|
return "Block{" +
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ public final class BundleCache {
|
|||||||
public void initialize(GeyserItemStack itemStack) {
|
public void initialize(GeyserItemStack itemStack) {
|
||||||
// Message before 1.21.4 - "Can't check for BUNDLE_CONTENTS, which may be missing if the bundle is empty."
|
// Message before 1.21.4 - "Can't check for BUNDLE_CONTENTS, which may be missing if the bundle is empty."
|
||||||
// Now irrelevant, but keeping as-is for the time being.
|
// Now irrelevant, but keeping as-is for the time being.
|
||||||
if (session.getTagCache().is(ItemTag.BUNDLES, itemStack)) {
|
if (itemStack.is(session, ItemTag.BUNDLES)) {
|
||||||
if (itemStack.getBundleData() != null) {
|
if (itemStack.getBundleData() != null) {
|
||||||
session.getGeyser().getLogger().warning("Stack has bundle data already! It should not!");
|
session.getGeyser().getLogger().warning("Stack has bundle data already! It should not!");
|
||||||
if (session.getGeyser().getConfig().isDebugMode()) {
|
if (session.getGeyser().getConfig().isDebugMode()) {
|
||||||
@@ -231,7 +231,7 @@ public final class BundleCache {
|
|||||||
* if Bedrock sends its own. */
|
* if Bedrock sends its own. */
|
||||||
|
|
||||||
public void awaitRelease() {
|
public void awaitRelease() {
|
||||||
if (session.getTagCache().is(ItemTag.BUNDLES, session.getPlayerInventory().getItemInHand())) {
|
if (session.getPlayerInventory().getItemInHand().is(session, ItemTag.BUNDLES)) {
|
||||||
releaseTick = session.getTicks() + 1;
|
releaseTick = session.getTicks() + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,9 +31,6 @@ import net.kyori.adventure.key.Key;
|
|||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
import org.geysermc.geyser.GeyserLogger;
|
import org.geysermc.geyser.GeyserLogger;
|
||||||
import org.geysermc.geyser.inventory.GeyserItemStack;
|
|
||||||
import org.geysermc.geyser.item.type.Item;
|
|
||||||
import org.geysermc.geyser.level.block.type.Block;
|
|
||||||
import org.geysermc.geyser.session.GeyserSession;
|
import org.geysermc.geyser.session.GeyserSession;
|
||||||
import org.geysermc.geyser.session.cache.registry.JavaRegistries;
|
import org.geysermc.geyser.session.cache.registry.JavaRegistries;
|
||||||
import org.geysermc.geyser.session.cache.registry.JavaRegistryKey;
|
import org.geysermc.geyser.session.cache.registry.JavaRegistryKey;
|
||||||
@@ -43,7 +40,6 @@ import org.geysermc.geyser.util.MinecraftKey;
|
|||||||
import org.geysermc.mcprotocollib.protocol.data.game.item.component.HolderSet;
|
import org.geysermc.mcprotocollib.protocol.data.game.item.component.HolderSet;
|
||||||
import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundUpdateTagsPacket;
|
import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundUpdateTagsPacket;
|
||||||
|
|
||||||
import javax.annotation.ParametersAreNonnullByDefault;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -52,8 +48,19 @@ import java.util.Map;
|
|||||||
* Manages information sent from the {@link ClientboundUpdateTagsPacket}. If that packet is not sent, all lists here
|
* Manages information sent from the {@link ClientboundUpdateTagsPacket}. If that packet is not sent, all lists here
|
||||||
* will remain empty, matching Java Edition behavior. Looking up a tag that wasn't listed in that packet will return an empty array.
|
* will remain empty, matching Java Edition behavior. Looking up a tag that wasn't listed in that packet will return an empty array.
|
||||||
* Only tags from registries in {@link JavaRegistries} are stored. Read {@link JavaRegistryKey} for more information.
|
* Only tags from registries in {@link JavaRegistries} are stored. Read {@link JavaRegistryKey} for more information.
|
||||||
|
*
|
||||||
|
* <p>To simply check if an element is in a tag, it's preferred to use the element's "{@code is}" method, if available. For example:</p>
|
||||||
|
*
|
||||||
|
* <ul>
|
||||||
|
* <li>{@link org.geysermc.geyser.level.block.type.Block#is(GeyserSession, Tag)}</li>
|
||||||
|
* <li>{@link org.geysermc.geyser.level.block.type.Block#is(GeyserSession, HolderSet)}</li>
|
||||||
|
* <li>{@link org.geysermc.geyser.item.type.Item#is(GeyserSession, Tag)}</li>
|
||||||
|
* <li>{@link org.geysermc.geyser.item.type.Item#is(GeyserSession, HolderSet)}</li>
|
||||||
|
* <li>{@link org.geysermc.geyser.inventory.GeyserItemStack#is(GeyserSession, Tag)}</li>
|
||||||
|
* <li>{@link org.geysermc.geyser.inventory.GeyserItemStack#is(GeyserSession, HolderSet)}</li>
|
||||||
|
* <li>{@link GeyserHolderSet#contains(GeyserSession, Object)}</li>
|
||||||
|
* </ul>
|
||||||
*/
|
*/
|
||||||
@ParametersAreNonnullByDefault
|
|
||||||
public final class TagCache {
|
public final class TagCache {
|
||||||
private final GeyserSession session;
|
private final GeyserSession session;
|
||||||
private final Map<Tag<?>, int[]> tags = new Object2ObjectOpenHashMap<>();
|
private final Map<Tag<?>, int[]> tags = new Object2ObjectOpenHashMap<>();
|
||||||
@@ -109,37 +116,40 @@ public final class TagCache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> boolean is(Tag<T> tag, T object) {
|
/**
|
||||||
|
* Should only be used when the network ID of an element is already known. If not, prefer using the {@link TagCache#is(Tag, Object)} shorthand method.
|
||||||
|
*/
|
||||||
|
public boolean is(@NonNull Tag<?> tag, int id) {
|
||||||
|
return contains(getRaw(tag), id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> boolean is(@NonNull Tag<T> tag, @NonNull T object) {
|
||||||
return contains(getRaw(tag), tag.registry().networkId(session, object));
|
return contains(getRaw(tag), tag.registry().networkId(session, object));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if the item tag is present and contains this item stack's Java ID.
|
* Prefer using {@link GeyserHolderSet#contains(GeyserSession, Object)}.
|
||||||
|
*
|
||||||
|
* @return true if the specified network ID is in the given {@link GeyserHolderSet}.
|
||||||
*/
|
*/
|
||||||
public boolean is(Tag<Item> tag, GeyserItemStack itemStack) {
|
public <T> boolean is(@NonNull GeyserHolderSet<T> holderSet, @Nullable T object) {
|
||||||
return is(tag, itemStack.asItem());
|
if (object == null) {
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return true if the specified network ID is in the given holder set.
|
|
||||||
*/
|
|
||||||
public <T> boolean is(@Nullable GeyserHolderSet<T> holderSet, @Nullable T object) {
|
|
||||||
if (holderSet == null || object == null) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return contains(holderSet.resolveRaw(this), holderSet.getRegistry().networkId(session, object));
|
return contains(holderSet.resolveRaw(this), holderSet.getRegistry().networkId(session, object));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accessible via the {@link #isItem(HolderSet, Item)} method.
|
|
||||||
* @return true if the specified network ID is in the given {@link HolderSet} set.
|
* @return true if the specified network ID is in the given {@link HolderSet} set.
|
||||||
*/
|
*/
|
||||||
private <T> boolean is(@Nullable HolderSet holderSet, @NonNull JavaRegistryKey<T> registry, int id) {
|
public <T> boolean is(@Nullable HolderSet holderSet, @NonNull JavaRegistryKey<T> registry, int id) {
|
||||||
if (holderSet == null) {
|
if (holderSet == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int[] entries = holderSet.resolve(key -> {
|
int[] entries = holderSet.resolve(key -> {
|
||||||
|
// This should never happen, since a key in a HolderSet is always a tag
|
||||||
|
// We check for it anyway
|
||||||
if (key.value().startsWith("#")) {
|
if (key.value().startsWith("#")) {
|
||||||
key = Key.key(key.namespace(), key.value().substring(1));
|
key = Key.key(key.namespace(), key.value().substring(1));
|
||||||
}
|
}
|
||||||
@@ -149,23 +159,14 @@ public final class TagCache {
|
|||||||
return contains(entries, id);
|
return contains(entries, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isItem(@Nullable HolderSet holderSet, @NonNull Item item) {
|
public <T> List<T> get(@NonNull Tag<T> tag) {
|
||||||
return is(holderSet, JavaRegistries.ITEM, item.javaId());
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isBlock(@Nullable HolderSet holderSet, @NonNull Block block) {
|
|
||||||
return is(holderSet, JavaRegistries.BLOCK, block.javaId());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public <T> List<T> get(Tag<T> tag) {
|
|
||||||
return mapRawArray(session, getRaw(tag), tag.registry());
|
return mapRawArray(session, getRaw(tag), tag.registry());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the network IDs in the given tag. This can be an empty list.
|
* @return the network IDs in the given tag. This can be an empty array.
|
||||||
*/
|
*/
|
||||||
public int[] getRaw(Tag<?> tag) {
|
public int[] getRaw(@NonNull Tag<?> tag) {
|
||||||
return this.tags.getOrDefault(tag, IntArrays.EMPTY_ARRAY);
|
return this.tags.getOrDefault(tag, IntArrays.EMPTY_ARRAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -95,6 +95,13 @@ public final class GeyserHolderSet<T> {
|
|||||||
return new GeyserHolderSet<>(registry, tag, holderSet.getHolders(), null);
|
return new GeyserHolderSet<>(registry, tag, holderSet.getHolders(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean contains(@NonNull GeyserSession session, @Nullable T object) {
|
||||||
|
if (object == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return session.getTagCache().is(this, object);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolves the HolderSet, and automatically maps the network IDs to their respective object types.
|
* Resolves the HolderSet, and automatically maps the network IDs to their respective object types.
|
||||||
* If the HolderSet is a list of IDs, this will be returned. If it is a tag, the tag will be resolved from the tag cache. If it is an inline HolderSet, the list of inline elements will be returned.
|
* If the HolderSet is a list of IDs, this will be returned. If it is a tag, the tag will be resolved from the tag cache. If it is an inline HolderSet, the list of inline elements will be returned.
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ public final class BlockUtils {
|
|||||||
|
|
||||||
for (ToolData.Rule rule : tool.getRules()) {
|
for (ToolData.Rule rule : tool.getRules()) {
|
||||||
if (rule.getCorrectForDrops() != null) {
|
if (rule.getCorrectForDrops() != null) {
|
||||||
if (session.getTagCache().isBlock(rule.getBlocks(), block)) {
|
if (block.is(session, rule.getBlocks())) {
|
||||||
return rule.getCorrectForDrops();
|
return rule.getCorrectForDrops();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,7 +96,7 @@ public final class BlockUtils {
|
|||||||
|
|
||||||
for (ToolData.Rule rule : tool.getRules()) {
|
for (ToolData.Rule rule : tool.getRules()) {
|
||||||
if (rule.getSpeed() != null) {
|
if (rule.getSpeed() != null) {
|
||||||
if (session.getTagCache().isBlock(rule.getBlocks(), block)) {
|
if (block.is(session, rule.getBlocks())) {
|
||||||
return rule.getSpeed();
|
return rule.getSpeed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -258,7 +258,7 @@ public final class BlockUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean blockMatchesPredicate(GeyserSession session, BlockState state, AdventureModePredicate.BlockPredicate predicate) {
|
public static boolean blockMatchesPredicate(GeyserSession session, BlockState state, AdventureModePredicate.BlockPredicate predicate) {
|
||||||
if (predicate.getBlocks() != null && !session.getTagCache().isBlock(predicate.getBlocks(), state.block())) {
|
if (predicate.getBlocks() != null && !state.block().is(session, predicate.getBlocks())) {
|
||||||
return false;
|
return false;
|
||||||
} else if (predicate.getProperties() != null) {
|
} else if (predicate.getProperties() != null) {
|
||||||
List<AdventureModePredicate.PropertyMatcher> matchers = predicate.getProperties();
|
List<AdventureModePredicate.PropertyMatcher> matchers = predicate.getProperties();
|
||||||
|
|||||||
@@ -359,7 +359,7 @@ public final class EntityUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
GeyserHolderSet<EntityType> holderSet = GeyserHolderSet.fromHolderSet(JavaRegistries.ENTITY_TYPE, equippable.allowedEntities());
|
GeyserHolderSet<EntityType> holderSet = GeyserHolderSet.fromHolderSet(JavaRegistries.ENTITY_TYPE, equippable.allowedEntities());
|
||||||
return session.getTagCache().is(holderSet, entity);
|
return holderSet.contains(session, entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
// From ViaVersion! thank u!!
|
// From ViaVersion! thank u!!
|
||||||
|
|||||||
@@ -379,7 +379,7 @@ public class InventoryUtils {
|
|||||||
&& Objects.equals(itemStack.getComponents(), other.getDataComponentsPatch());
|
&& Objects.equals(itemStack.getComponents(), other.getDataComponentsPatch());
|
||||||
}
|
}
|
||||||
if (slotDisplay instanceof TagSlotDisplay tagSlotDisplay) {
|
if (slotDisplay instanceof TagSlotDisplay tagSlotDisplay) {
|
||||||
return session.getTagCache().is(new Tag<>(JavaRegistries.ITEM, tagSlotDisplay.tag()), itemStack.asItem());
|
return itemStack.is(session, new Tag<>(JavaRegistries.ITEM, tagSlotDisplay.tag()));
|
||||||
}
|
}
|
||||||
session.getGeyser().getLogger().warning("Unknown slot display type: " + slotDisplay);
|
session.getGeyser().getLogger().warning("Unknown slot display type: " + slotDisplay);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user