org.anvilpowered.anvil.base.registry.BaseRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of anvil-api Show documentation
Show all versions of anvil-api Show documentation
A cross-platform database API / ORM / entity framework with useful services for minecraft plugins
The newest version!
/*
* Anvil - AnvilPowered
* Copyright (C) 2020
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package org.anvilpowered.anvil.base.registry;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.anvilpowered.anvil.api.Anvil;
import org.anvilpowered.anvil.api.registry.Key;
import org.anvilpowered.anvil.api.registry.Keys;
import org.anvilpowered.anvil.api.registry.Registry;
import org.anvilpowered.anvil.api.registry.RegistryScope;
import org.anvilpowered.anvil.api.registry.RegistryScoped;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
@Singleton
@SuppressWarnings("unchecked")
public class BaseRegistry implements Registry {
@Inject
protected Logger logger;
final Map, Object> defaultMap, valueMap;
private final Map> listeners;
private Registry coreRegistry;
@Nullable
private String stringRepresentation;
public BaseRegistry() {
defaultMap = new HashMap<>();
valueMap = new HashMap<>();
listeners = new HashMap<>();
}
@Override
public T getUnsafe(Key key) {
T t = (T) valueMap.get(key);
if (t == null) {
throw new NoSuchElementException("Could not find value for key " + key);
}
return t;
}
@Override
public Optional get(Key key) {
return Optional.ofNullable((T) valueMap.get(key));
}
@Override
public T getDefault(Key key) {
T result = (T) defaultMap.get(key);
return result == null ? key.getFallbackValue() : result;
}
@Override
public void set(Key key, T value) {
valueMap.put(key, value);
stringRepresentation = null;
}
@RegistryScoped
protected void setDefault(Key key, T value) {
defaultMap.put(key, value);
stringRepresentation = null;
}
@Override
public T getExtraSafe(Key key) {
if (coreRegistry == null) {
coreRegistry = Anvil.getRegistry();
}
if (this != coreRegistry
&& getOrDefault(Keys.USE_SHARED_ENVIRONMENT)) {
if (key.equals(Keys.DATA_STORE_NAME)
|| key.equals(Keys.MONGODB_HOSTNAME)
|| key.equals(Keys.MONGODB_PORT)
|| key.equals(Keys.MONGODB_USE_SRV)) {
return coreRegistry.getOrDefault(key);
} else if (getOrDefault(Keys.USE_SHARED_CREDENTIALS)) {
if (key.equals(Keys.MONGODB_USE_CONNECTION_STRING)
|| key.equals(Keys.MONGODB_CONNECTION_STRING)
|| key.equals(Keys.MONGODB_USERNAME)
|| key.equals(Keys.MONGODB_PASSWORD)
|| key.equals(Keys.MONGODB_AUTH_DB)
|| key.equals(Keys.MONGODB_USE_AUTH)) {
return coreRegistry.getOrDefault(key);
}
}
}
return getOrDefault(key);
}
@Override
public void remove(Key key) {
valueMap.remove(key);
stringRepresentation = null;
}
@Override
public void transform(Key key, BiFunction super Key, ? super T, ? extends T> transformer) {
valueMap.compute(key, (BiFunction super Key>, ? super Object, ?>) transformer);
stringRepresentation = null;
}
@Override
public void transform(Key key, Function super T, ? extends T> transformer) {
transform(key, (k, v) -> transformer.apply((T) v));
stringRepresentation = null;
}
@Override
public void addToCollection(Key extends Collection> key, T value) {
((Collection) valueMap.get(key)).add(value);
stringRepresentation = null;
}
@Override
public void removeFromCollection(Key extends Collection> key, T value) {
((Collection) valueMap.get(key)).remove(value);
stringRepresentation = null;
}
@Override
public void putInMap(Key extends Map> key, K mapKey, T mapValue) {
((Map) valueMap.get(key)).put(mapKey, mapValue);
stringRepresentation = null;
}
@Override
public void removeFromMap(Key extends Map> key, K mapKey) {
((Map) valueMap.get(key)).remove(mapKey);
stringRepresentation = null;
}
@Override
public ListenerRegistrationEnd whenLoaded(Runnable listener) {
return new ListenerRegistrationEndImpl(listener);
}
private class ListenerRegistrationEndImpl implements ListenerRegistrationEnd {
private final Runnable listener;
private int order;
private RegistryScope registryScope;
public ListenerRegistrationEndImpl(Runnable listener) {
this.listener = listener;
order = 0;
registryScope = RegistryScope.DEFAULT;
}
@Override
public ListenerRegistrationEnd order(int order) {
this.order = order;
return this;
}
@Override
public ListenerRegistrationEnd scope(RegistryScope registryScope) {
this.registryScope = registryScope;
return this;
}
@Override
public void register() {
listeners.computeIfAbsent(order, o -> new HashMap<>())
.put(listener, registryScope);
}
}
@Override
public void load(RegistryScope registryScope) {
final int ordinal = registryScope.ordinal();
if (ordinal <= RegistryScope.DEFAULT.ordinal()) {
loadDefaultScope();
}
loadOrdinal(ordinal);
}
protected final void loadOrdinal(int ordinal) {
listeners.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEach(e -> e.getValue().forEach((r, c) -> {
if (ordinal <= c.ordinal()) {
r.run();
}
}));
}
/**
* Override this method to load values into this registry on normal reloads
*/
@RegistryScoped
protected void loadDefaultScope() {
}
@Override
public String toString() {
if (stringRepresentation != null) {
return stringRepresentation;
}
Set> keys = new HashSet<>();
int[] width = {0, 32, 32};
Consumer super Key>> addToKeys = key -> {
final int keyLength = key.toString().length();
if (keyLength > width[0]) {
width[0] = keyLength;
}
keys.add(key);
};
valueMap.keySet().forEach(addToKeys);
defaultMap.keySet().forEach(addToKeys);
width[0] += 5;
return stringRepresentation = String.format("%-" + width[0] + "s", "Key")
+ String.format("%-" + width[1] + "s", "Value")
+ String.format("%-" + width[2] + "s", "Default")
+ '\n'
+ keys.stream().map(key ->
String.format("%-" + width[0] + "s", key.toString())
+ String.format("%-" + width[1] + "s", valueMap.get(key))
+ String.format("%-" + width[2] + "s", defaultMap.get(key))
).collect(Collectors.joining("\n"));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy