Add a method on Registry to get the size (#12182)

This commit is contained in:
Glicz
2025-02-25 22:02:48 +01:00
committed by GitHub
parent 5f2ee83ed4
commit b00875f86d
3 changed files with 33 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ package org.bukkit;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import io.papermc.paper.datacomponent.DataComponentType; import io.papermc.paper.datacomponent.DataComponentType;
import io.papermc.paper.registry.RegistryAccess; import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey; import io.papermc.paper.registry.RegistryKey;
@@ -297,6 +298,11 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
return MemoryKey.values().iterator(); return MemoryKey.values().iterator();
} }
@Override
public int size() {
return MemoryKey.values().size();
}
@Override @Override
public @Nullable MemoryKey get(final NamespacedKey key) { public @Nullable MemoryKey get(final NamespacedKey key) {
return MemoryKey.getByKey(key); return MemoryKey.getByKey(key);
@@ -536,6 +542,13 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
return (namespacedKey != null) ? this.get(namespacedKey) : null; return (namespacedKey != null) ? this.get(namespacedKey) : null;
} }
/**
* Gets the size of the registry.
*
* @return the size of the registry
*/
int size();
@ApiStatus.Internal @ApiStatus.Internal
class SimpleRegistry<T extends Enum<T> & Keyed> extends NotARegistry<T> { // Paper - remove final class SimpleRegistry<T extends Enum<T> & Keyed> extends NotARegistry<T> { // Paper - remove final
@@ -564,6 +577,11 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
return this.map.get(key); return this.map.get(key);
} }
@Override
public int size() {
return map.size();
}
@Override @Override
public Iterator<T> iterator() { public Iterator<T> iterator() {
return this.map.values().iterator(); return this.map.values().iterator();
@@ -584,6 +602,11 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
return StreamSupport.stream(this.spliterator(), false); return StreamSupport.stream(this.spliterator(), false);
} }
@Override
public int size() {
return Iterables.size(this);
}
@Override @Override
public NamespacedKey getKey(final A value) { public NamespacedKey getKey(final A value) {
return value.getKey(); return value.getKey();

View File

@@ -49,6 +49,11 @@ public final class DelayedRegistry<T extends Keyed, R extends Registry<T>> imple
return this.delegate().stream(); return this.delegate().stream();
} }
@Override
public int size() {
return this.delegate().size();
}
@Override @Override
public @Nullable NamespacedKey getKey(final T value) { public @Nullable NamespacedKey getKey(final T value) {
return this.delegate().getKey(value); return this.delegate().getKey(value);

View File

@@ -233,6 +233,11 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
return this.minecraftRegistry.keySet().stream().map(minecraftKey -> this.get(CraftNamespacedKey.fromMinecraft(minecraftKey))); return this.minecraftRegistry.keySet().stream().map(minecraftKey -> this.get(CraftNamespacedKey.fromMinecraft(minecraftKey)));
} }
@Override
public int size() {
return this.minecraftRegistry.size();
}
@Override @Override
public Iterator<B> iterator() { public Iterator<B> iterator() {
return this.stream().iterator(); return this.stream().iterator();