me.moros.bending.api.registry.Registry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bending-api Show documentation
Show all versions of bending-api Show documentation
Modern Bending api for Minecraft
The newest version!
/*
* Copyright 2020-2024 Moros
*
* This file is part of Bending.
*
* Bending is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bending is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bending. If not, see .
*/
package me.moros.bending.api.registry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
import me.moros.bending.api.registry.RegistryBuilder.IntermediaryRegistryBuilder;
import me.moros.bending.api.util.KeyUtil;
import me.moros.bending.api.util.data.DataKey;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Common interface for bending registries.
* @param the type of keys for this registry
* @param the type of values for this registry
*/
public interface Registry extends Container, TagHolder {
DataKey key();
/**
* Check if the registry contains a value for the specified key.
* @param key the key to check
* @return true if the key has an associated value, false otherwise
*/
boolean containsKey(K key);
/**
* Get the value for the specified key.
* @param key the key to check
* @return the value associated with the given key or null if not found
*/
@Nullable V get(K key);
/**
* Get the value for the specified key.
* @param key the key to check
* @return the value associated with the given key
* @throws NullPointerException if registry mapping is not found
*/
default V getOrThrow(K key) {
return Objects.requireNonNull(get(key), "No mapping found for " + key);
}
/**
* Get the value for the specified key.
* @param key the key to check
* @return the value associated with the given key or def if not found
*/
default Optional getIfExists(K key) {
return Optional.ofNullable(get(key));
}
/**
* Get the value for the specified key.
* @param input the key to check
* @return the value associated with the given key or null if not found
*/
@Nullable V fromString(String input);
/**
* Registers a value if it doesn't exist.
* @param value the value to register
* @return true if value was registered, false otherwise
* @throws RegistryModificationException if registry is locked
*/
boolean register(V value);
/**
* Registers all given values if they don't exist.
* @param values the values to register
* @return the amount of values that were registered
*/
default int register(Iterable values) {
int counter = 0;
if (!isLocked()) {
for (V value : values) {
if (register(value)) {
counter++;
}
}
}
return counter;
}
/**
* Lock this registry to prevent further modifications.
*/
void lock();
/**
* Check if this registry is locked.
* @return true if locked, false otherwise
*/
boolean isLocked();
/**
* Stream all the keys in this registry.
* @return a stream of this registry's keys
*/
Stream streamKeys();
/**
* A collection of all currently registered keys.
* @return an immutable collection of this registry's keys
*/
Set keys();
static IntermediaryRegistryBuilder builder(DataKey key) {
Objects.requireNonNull(key);
return new IntermediaryRegistryBuilder<>(key);
}
static RegistryBuilder simpleBuilder(DataKey key) {
return builder(key).inverseMapper(Keyed::key).keyMapper(KeyUtil.stringToKey(key.namespace()));
}
static RegistryBuilder vanillaBuilder(DataKey key) {
return builder(key).inverseMapper(Keyed::key).keyMapper(KeyUtil.VANILLA_KEY_MAPPER);
}
static Registry vanilla(String name, Class type) {
return vanillaBuilder(DataKey.wrap(KeyUtil.vanilla(name), type)).build();
}
static DefaultedRegistry vanillaDefaulted(String name, Class type, Function factory) {
return vanillaBuilder(DataKey.wrap(KeyUtil.vanilla(name), type)).buildDefaulted(factory);
}
}