minecraft-source/src/net/minecraft/advancements/AdvancementList.java

151 lines
5.0 KiB
Java

package net.minecraft.advancements;
import org.apache.logging.log4j.LogManager;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.function.Function;
import com.google.common.base.Functions;
import java.util.Iterator;
import com.google.common.collect.Sets;
import com.google.common.collect.Maps;
import java.util.Set;
import net.minecraft.resources.ResourceLocation;
import java.util.Map;
import org.apache.logging.log4j.Logger;
public class AdvancementList {
private static final Logger LOGGER;
private final Map<ResourceLocation, Advancement> advancements;
private final Set<Advancement> roots;
private final Set<Advancement> tasks;
private Listener listener;
public AdvancementList() {
this.advancements = Maps.newHashMap();
this.roots = Sets.newLinkedHashSet();
this.tasks = Sets.newLinkedHashSet();
}
private void remove(final Advancement u) {
for (final Advancement u2 : u.getChildren()) {
this.remove(u2);
}
AdvancementList.LOGGER.info("Forgot about advancement {}", u.getId());
this.advancements.remove(u.getId());
if (u.getParent() == null) {
this.roots.remove(u);
if (this.listener != null) {
this.listener.onRemoveAdvancementRoot(u);
}
}
else {
this.tasks.remove(u);
if (this.listener != null) {
this.listener.onRemoveAdvancementTask(u);
}
}
}
public void remove(final Set<ResourceLocation> set) {
for (final ResourceLocation sm4 : set) {
final Advancement u5 = this.advancements.get(sm4);
if (u5 == null) {
AdvancementList.LOGGER.warn("Told to remove advancement {} but I don't know what that is", sm4);
}
else {
this.remove(u5);
}
}
}
public void add(final Map<ResourceLocation, Advancement.Builder> map) {
final Function<ResourceLocation, Advancement> function3 = Functions.forMap(this.advancements, null);
while (!map.isEmpty()) {
boolean boolean4 = false;
final Iterator<Map.Entry<ResourceLocation, Advancement.Builder>> iterator5 = map.entrySet().iterator();
while (iterator5.hasNext()) {
final Map.Entry<ResourceLocation, Advancement.Builder> entry6 = iterator5.next();
final ResourceLocation sm7 = entry6.getKey();
final Advancement.Builder a8 = entry6.getValue();
if (a8.canBuild(function3)) {
final Advancement u9 = a8.build(sm7);
this.advancements.put(sm7, u9);
boolean4 = true;
iterator5.remove();
if (u9.getParent() == null) {
this.roots.add(u9);
if (this.listener == null) {
continue;
}
this.listener.onAddAdvancementRoot(u9);
}
else {
this.tasks.add(u9);
if (this.listener == null) {
continue;
}
this.listener.onAddAdvancementTask(u9);
}
}
}
if (!boolean4) {
for (final Map.Entry<ResourceLocation, Advancement.Builder> entry6 : map.entrySet()) {
AdvancementList.LOGGER.error("Couldn't load advancement {}: {}", entry6.getKey(), entry6.getValue());
}
break;
}
}
AdvancementList.LOGGER.info("Loaded {} advancements", this.advancements.size());
}
public void clear() {
this.advancements.clear();
this.roots.clear();
this.tasks.clear();
if (this.listener != null) {
this.listener.onAdvancementsCleared();
}
}
public Iterable<Advancement> getRoots() {
return this.roots;
}
public Collection<Advancement> getAllAdvancements() {
return this.advancements.values();
}
@Nullable
public Advancement get(final ResourceLocation sm) {
return this.advancements.get(sm);
}
public void setListener(@Nullable final Listener a) {
this.listener = a;
if (a != null) {
for (final Advancement u4 : this.roots) {
a.onAddAdvancementRoot(u4);
}
for (final Advancement u4 : this.tasks) {
a.onAddAdvancementTask(u4);
}
}
}
static {
LOGGER = LogManager.getLogger();
}
public interface Listener {
void onAddAdvancementRoot(final Advancement u);
void onRemoveAdvancementRoot(final Advancement u);
void onAddAdvancementTask(final Advancement u);
void onRemoveAdvancementTask(final Advancement u);
void onAdvancementsCleared();
}
}