| 1 | package net.minecraft.advancements;
|
| 2 |
|
| 3 | import com.google.common.collect.Iterables;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
|
| 7 | import java.util.Collection;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.Set;
|
| 11 | import net.minecraft.resources.Identifier;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 | import org.slf4j.Logger;
|
| 14 |
|
| 15 | public class AdvancementTree {
|
| 16 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 17 | private final Map<Identifier, AdvancementNode> nodes = new Object2ObjectLinkedOpenHashMap<>();
|
| 18 |
|
| 19 | private void remove(final AdvancementNode node) {
|
| 20 | for (AdvancementNode child : node.children()) {
|
| 21 | this.remove(child);
|
| 22 | }
|
| 23 |
|
| 24 | LOGGER.info("Forgot about advancement {}", node.holder());
|
| 25 | this.nodes.remove(node.holder().id());
|
| 26 | }
|
| 27 |
|
| 28 | public void remove(final Set<Identifier> ids) {
|
| 29 | for (Identifier id : ids) {
|
| 30 | AdvancementNode advancement = this.nodes.get(id);
|
| 31 | if (advancement == null) {
|
| 32 | LOGGER.warn("Told to remove advancement {} but I don't know what that is", id);
|
| 33 | } else {
|
| 34 | this.remove(advancement);
|
| 35 | }
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | public void addAll(final Iterable<AdvancementHolder> advancements) {
|
| 40 | List<AdvancementHolder> advancementsToAdd = Lists.newArrayList(advancements);
|
| 41 |
|
| 42 | while (!advancementsToAdd.isEmpty()) {
|
| 43 | if (!advancementsToAdd.removeIf(this::tryInsert)) {
|
| 44 | LOGGER.error("Couldn't load advancements: {}", advancementsToAdd);
|
| 45 | break;
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | LOGGER.info("Loaded {} advancements", this.nodes.size());
|
| 50 | }
|
| 51 |
|
| 52 | private boolean tryInsert(final AdvancementHolder holder) {
|
| 53 | Identifier parentId = holder.value().parent().orElse(null);
|
| 54 | AdvancementNode parentNode;
|
| 55 | if (parentId != null) {
|
| 56 | parentNode = this.nodes.get(parentId);
|
| 57 | if (parentNode == null) {
|
| 58 | return false;
|
| 59 | }
|
| 60 | } else {
|
| 61 | parentNode = null;
|
| 62 | }
|
| 63 |
|
| 64 | AdvancementNode node = new AdvancementNode(holder, parentNode);
|
| 65 | if (parentNode != null) {
|
| 66 | parentNode.addChild(node);
|
| 67 | }
|
| 68 |
|
| 69 | this.nodes.put(holder.id(), node);
|
| 70 | return true;
|
| 71 | }
|
| 72 |
|
| 73 | public void clear() {
|
| 74 | this.nodes.clear();
|
| 75 | }
|
| 76 |
|
| 77 | public Iterable<AdvancementNode> roots() {
|
| 78 | return Iterables.filter(this.nodes(), AdvancementNode::isRoot);
|
| 79 | }
|
| 80 |
|
| 81 | public Iterable<AdvancementNode> tasks() {
|
| 82 | return Iterables.filter(this.nodes(), AdvancementNode::isTask);
|
| 83 | }
|
| 84 |
|
| 85 | public Collection<AdvancementNode> nodes() {
|
| 86 | return this.nodes.values();
|
| 87 | }
|
| 88 |
|
| 89 | public @Nullable AdvancementNode get(final Identifier id) {
|
| 90 | return this.nodes.get(id);
|
| 91 | }
|
| 92 |
|
| 93 | public @Nullable AdvancementNode get(final AdvancementHolder advancement) {
|
| 94 | return this.nodes.get(advancement.id());
|
| 95 | }
|
| 96 |
|
| 97 | public void repositionNodes() {
|
| 98 | for (AdvancementNode root : this.roots()) {
|
| 99 | if (root.holder().value().display().isPresent()) {
|
| 100 | TreeNodePosition.run(root);
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|