diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 7520b46157..6fe06952b8 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -2147,56 +2147,73 @@ public final class CraftServer implements Server { @Override @SuppressWarnings("unchecked") public org.bukkit.Tag getTag(String registry, NamespacedKey tag, Class clazz) { + Validate.notNull(registry, "registry cannot be null"); + Validate.notNull(tag, "NamespacedKey cannot be null"); + Validate.notNull(clazz, "Class cannot be null"); MinecraftKey key = CraftNamespacedKey.toMinecraft(tag); switch (registry) { - case org.bukkit.Tag.REGISTRY_BLOCKS: + case org.bukkit.Tag.REGISTRY_BLOCKS -> { Preconditions.checkArgument(clazz == org.bukkit.Material.class, "Block namespace must have material type"); - - return (org.bukkit.Tag) new CraftBlockTag(IRegistry.BLOCK, TagKey.create(IRegistry.BLOCK_REGISTRY, key)); - case org.bukkit.Tag.REGISTRY_ITEMS: + TagKey blockTagKey = TagKey.create(IRegistry.BLOCK_REGISTRY, key); + if (IRegistry.BLOCK.isKnownTagName(blockTagKey)) { + return (org.bukkit.Tag) new CraftBlockTag(IRegistry.BLOCK, blockTagKey); + } + } + case org.bukkit.Tag.REGISTRY_ITEMS -> { Preconditions.checkArgument(clazz == org.bukkit.Material.class, "Item namespace must have material type"); - - return (org.bukkit.Tag) new CraftItemTag(IRegistry.ITEM, TagKey.create(IRegistry.ITEM_REGISTRY, key)); - case org.bukkit.Tag.REGISTRY_FLUIDS: + TagKey itemTagKey = TagKey.create(IRegistry.ITEM_REGISTRY, key); + if (IRegistry.ITEM.isKnownTagName(itemTagKey)) { + return (org.bukkit.Tag) new CraftItemTag(IRegistry.ITEM, itemTagKey); + } + } + case org.bukkit.Tag.REGISTRY_FLUIDS -> { Preconditions.checkArgument(clazz == org.bukkit.Fluid.class, "Fluid namespace must have fluid type"); - - return (org.bukkit.Tag) new CraftFluidTag(IRegistry.FLUID, TagKey.create(IRegistry.FLUID_REGISTRY, key)); - case org.bukkit.Tag.REGISTRY_ENTITY_TYPES: + TagKey fluidTagKey = TagKey.create(IRegistry.FLUID_REGISTRY, key); + if (IRegistry.FLUID.isKnownTagName(fluidTagKey)) { + return (org.bukkit.Tag) new CraftFluidTag(IRegistry.FLUID, fluidTagKey); + } + } + case org.bukkit.Tag.REGISTRY_ENTITY_TYPES -> { Preconditions.checkArgument(clazz == org.bukkit.entity.EntityType.class, "Entity type namespace must have entity type"); - - return (org.bukkit.Tag) new CraftEntityTag(IRegistry.ENTITY_TYPE, TagKey.create(IRegistry.ENTITY_TYPE_REGISTRY, key)); - default: - throw new IllegalArgumentException(); + TagKey> entityTagKey = TagKey.create(IRegistry.ENTITY_TYPE_REGISTRY, key); + if (IRegistry.ENTITY_TYPE.isKnownTagName(entityTagKey)) { + return (org.bukkit.Tag) new CraftEntityTag(IRegistry.ENTITY_TYPE, entityTagKey); + } + } + default -> throw new IllegalArgumentException(); } + + return null; } @Override @SuppressWarnings("unchecked") public Iterable> getTags(String registry, Class clazz) { + Validate.notNull(registry, "registry cannot be null"); + Validate.notNull(clazz, "Class cannot be null"); switch (registry) { - case org.bukkit.Tag.REGISTRY_BLOCKS: + case org.bukkit.Tag.REGISTRY_BLOCKS -> { Preconditions.checkArgument(clazz == org.bukkit.Material.class, "Block namespace must have material type"); - IRegistry blockTags = IRegistry.BLOCK; return blockTags.getTags().map(pair -> (org.bukkit.Tag) new CraftBlockTag(blockTags, pair.getFirst())).collect(ImmutableList.toImmutableList()); - case org.bukkit.Tag.REGISTRY_ITEMS: + } + case org.bukkit.Tag.REGISTRY_ITEMS -> { Preconditions.checkArgument(clazz == org.bukkit.Material.class, "Item namespace must have material type"); - IRegistry itemTags = IRegistry.ITEM; return itemTags.getTags().map(pair -> (org.bukkit.Tag) new CraftItemTag(itemTags, pair.getFirst())).collect(ImmutableList.toImmutableList()); - case org.bukkit.Tag.REGISTRY_FLUIDS: + } + case org.bukkit.Tag.REGISTRY_FLUIDS -> { Preconditions.checkArgument(clazz == org.bukkit.Material.class, "Fluid namespace must have fluid type"); - IRegistry fluidTags = IRegistry.FLUID; return fluidTags.getTags().map(pair -> (org.bukkit.Tag) new CraftFluidTag(fluidTags, pair.getFirst())).collect(ImmutableList.toImmutableList()); - case org.bukkit.Tag.REGISTRY_ENTITY_TYPES: + } + case org.bukkit.Tag.REGISTRY_ENTITY_TYPES -> { Preconditions.checkArgument(clazz == org.bukkit.entity.EntityType.class, "Entity type namespace must have entity type"); - IRegistry> entityTags = IRegistry.ENTITY_TYPE; return entityTags.getTags().map(pair -> (org.bukkit.Tag) new CraftEntityTag(entityTags, pair.getFirst())).collect(ImmutableList.toImmutableList()); - default: - throw new IllegalArgumentException(); + } + default -> throw new IllegalArgumentException(); } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftBlockTag.java b/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftBlockTag.java index e3f746589a..c00710a889 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftBlockTag.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftBlockTag.java @@ -29,6 +29,6 @@ public class CraftBlockTag extends CraftTag { @Override public Set getValues() { - return Collections.unmodifiableSet(getHandle().stream().map((block) -> CraftMagicNumbers.getMaterial(block.value())).collect(Collectors.toSet())); + return getHandle().stream().map((block) -> CraftMagicNumbers.getMaterial(block.value())).collect(Collectors.toUnmodifiableSet()); } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftEntityTag.java b/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftEntityTag.java index 86f85c2ffa..3c02a335ff 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftEntityTag.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftEntityTag.java @@ -1,6 +1,7 @@ package org.bukkit.craftbukkit.tag; import java.util.Collections; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import net.minecraft.core.IRegistry; @@ -24,6 +25,6 @@ public class CraftEntityTag extends CraftTag, EntityType> { @Override public Set getValues() { - return Collections.unmodifiableSet(getHandle().stream().map((nms) -> Registry.ENTITY_TYPE.get(CraftNamespacedKey.fromMinecraft(EntityTypes.getKey(nms.value())))).collect(Collectors.toSet())); + return getHandle().stream().map((nms) -> Registry.ENTITY_TYPE.get(CraftNamespacedKey.fromMinecraft(EntityTypes.getKey(nms.value())))).filter(Objects::nonNull).collect(Collectors.toUnmodifiableSet()); } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftFluidTag.java b/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftFluidTag.java index 9d94b4bb9a..2b63daee81 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftFluidTag.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftFluidTag.java @@ -22,6 +22,6 @@ public class CraftFluidTag extends CraftTag { @Override public Set getValues() { - return Collections.unmodifiableSet(getHandle().stream().map((fluid) -> CraftMagicNumbers.getFluid(fluid.value())).collect(Collectors.toSet())); + return getHandle().stream().map((fluid) -> CraftMagicNumbers.getFluid(fluid.value())).collect(Collectors.toUnmodifiableSet()); } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftItemTag.java b/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftItemTag.java index 24e6130f21..1cfdfd84de 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftItemTag.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftItemTag.java @@ -29,6 +29,6 @@ public class CraftItemTag extends CraftTag { @Override public Set getValues() { - return Collections.unmodifiableSet(getHandle().stream().map((item) -> CraftMagicNumbers.getMaterial(item.value())).collect(Collectors.toSet())); + return getHandle().stream().map((item) -> CraftMagicNumbers.getMaterial(item.value())).collect(Collectors.toUnmodifiableSet()); } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftTag.java b/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftTag.java index 77cefd4a41..d6a4e60449 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftTag.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/tag/CraftTag.java @@ -18,13 +18,10 @@ public abstract class CraftTag implements Tag { public CraftTag(IRegistry registry, TagKey tag) { this.registry = registry; this.tag = tag; + this.handle = registry.getTag(this.tag).orElseThrow(); } protected HolderSet.Named getHandle() { - if (handle == null) { - handle = registry.getTag(tag).get(); - } - return handle; }