Minestom/src/main/java/net/minestom/server/advancements/AdvancementManager.java

60 lines
2.1 KiB
Java
Raw Normal View History

2020-08-05 10:56:16 +02:00
package net.minestom.server.advancements;
import net.minestom.server.utils.validate.Check;
2020-11-09 18:29:30 +01:00
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
2020-08-06 07:42:00 +02:00
import java.util.Collection;
2020-08-05 10:56:16 +02:00
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
2020-08-05 10:56:16 +02:00
2020-08-06 07:42:00 +02:00
/**
2020-10-15 21:16:31 +02:00
* Used to manage all the registered {@link AdvancementTab}.
2020-10-11 14:58:19 +02:00
* <p>
2020-10-15 21:16:31 +02:00
* Use {@link #createTab(String, AdvancementRoot)} to create a tab with the appropriate {@link AdvancementRoot}.
2020-08-06 07:42:00 +02:00
*/
2020-08-05 10:56:16 +02:00
public class AdvancementManager {
2020-11-09 23:48:34 +01:00
// root identifier = its advancement tab
private final Map<String, AdvancementTab> advancementTabMap = new ConcurrentHashMap<>();
2020-08-05 10:56:16 +02:00
2020-08-06 07:42:00 +02:00
/**
2020-10-15 21:16:31 +02:00
* Creates a new {@link AdvancementTab} with a single {@link AdvancementRoot}.
2020-08-06 07:42:00 +02:00
*
* @param rootIdentifier the root identifier
* @param root the root advancement
* @return the newly created {@link AdvancementTab}
2020-08-06 07:42:00 +02:00
* @throws IllegalStateException if a tab with the identifier {@code rootIdentifier} already exists
*/
2020-11-09 18:29:30 +01:00
@NotNull
public AdvancementTab createTab(@NotNull String rootIdentifier, @NotNull AdvancementRoot root) {
Check.stateCondition(advancementTabMap.containsKey(rootIdentifier),
"A tab with the identifier '" + rootIdentifier + "' already exists");
2020-08-05 10:56:16 +02:00
final AdvancementTab advancementTab = new AdvancementTab(rootIdentifier, root);
this.advancementTabMap.put(rootIdentifier, advancementTab);
return advancementTab;
}
2020-08-06 07:42:00 +02:00
/**
2020-10-15 21:16:31 +02:00
* Gets an advancement tab by its root identifier.
2020-08-06 07:42:00 +02:00
*
* @param rootIdentifier the root identifier of the tab
2020-10-15 21:16:31 +02:00
* @return the {@link AdvancementTab} associated with the identifier, null if not any
2020-08-06 07:42:00 +02:00
*/
2020-11-09 18:29:30 +01:00
@Nullable
public AdvancementTab getTab(@NotNull String rootIdentifier) {
2020-08-05 10:56:16 +02:00
return advancementTabMap.get(rootIdentifier);
}
2020-08-06 07:42:00 +02:00
/**
2020-10-15 21:16:31 +02:00
* Gets all the created {@link AdvancementTab}.
2020-08-06 07:42:00 +02:00
*
* @return the collection containing all created {@link AdvancementTab}
*/
2020-11-09 18:29:30 +01:00
@NotNull
2020-08-06 07:42:00 +02:00
public Collection<AdvancementTab> getTabs() {
return advancementTabMap.values();
}
2020-08-05 10:56:16 +02:00
}