#893: Add a stream method to Registry to make it easier to use and to avoid unnecessary wrapping

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
Bukkit/Spigot 2023-09-06 20:56:22 +10:00
parent acf33ccb08
commit 085e8f6d9f
2 changed files with 48 additions and 1 deletions

View file

@ -8,6 +8,8 @@ import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.bukkit.advancement.Advancement;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.Biome;
@ -48,6 +50,12 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
return Bukkit.getAdvancement(key);
}
@NotNull
@Override
public Stream<Advancement> stream() {
return StreamSupport.stream(spliterator(), false);
}
@NotNull
@Override
public Iterator<Advancement> iterator() {
@ -86,6 +94,12 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
return Bukkit.getBossBar(key);
}
@NotNull
@Override
public Stream<KeyedBossBar> stream() {
return StreamSupport.stream(spliterator(), false);
}
@NotNull
@Override
public Iterator<KeyedBossBar> iterator() {
@ -105,6 +119,12 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
return Enchantment.getByKey(key);
}
@NotNull
@Override
public Stream<Enchantment> stream() {
return StreamSupport.stream(spliterator(), false);
}
@NotNull
@Override
public Iterator<Enchantment> iterator() {
@ -203,6 +223,12 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
public MemoryKey get(@NotNull NamespacedKey key) {
return MemoryKey.getByKey(key);
}
@NotNull
@Override
public Stream<MemoryKey> stream() {
return StreamSupport.stream(spliterator(), false);
}
};
/**
* Server fluids.
@ -231,6 +257,14 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
@Nullable
T get(@NotNull NamespacedKey key);
/**
* Returns a new stream, which contains all registry items, which are registered to the registry.
*
* @return a stream of all registry items
*/
@NotNull
Stream<T> stream();
/**
* Attempts to match the registered object with the given key.
* <p>
@ -275,6 +309,12 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
return map.get(key);
}
@NotNull
@Override
public Stream<T> stream() {
return StreamSupport.stream(spliterator(), false);
}
@NotNull
@Override
public Iterator<T> iterator() {

View file

@ -7,6 +7,7 @@ import java.lang.reflect.Proxy;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Stream;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.SimplePluginManager;
@ -87,7 +88,7 @@ public final class TestServer implements InvocationHandler {
@NotNull
@Override
public Iterator iterator() {
return null;
throw new UnsupportedOperationException();
}
@Nullable
@ -95,6 +96,12 @@ public final class TestServer implements InvocationHandler {
public Keyed get(@NotNull NamespacedKey key) {
return null;
}
@NotNull
@Override
public Stream stream() {
throw new UnsupportedOperationException();
}
};
}
}