package com.minelittlepony.unicopia.advancements; import java.util.List; import java.util.Map; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import net.minecraft.advancements.ICriterionTrigger; import net.minecraft.advancements.PlayerAdvancements; import net.minecraft.advancements.critereon.AbstractCriterionInstance; public abstract class AbstractTrigger, T extends AbstractCriterionInstance> implements ICriterionTrigger { protected final Map listeners = Maps.newHashMap(); @Override public void addListener(PlayerAdvancements key, Listener listener) { listeners.computeIfAbsent(key, this::createEntry).listeners.add(listener);; } @Override public void removeListener(PlayerAdvancements key, Listener listener) { if (listeners.containsKey(key)) { E entry = listeners.get(key); entry.listeners.remove(listener); if (entry.listeners.isEmpty()) { listeners.remove(key); } } } @Override public void removeAllListeners(PlayerAdvancements key) { if (listeners.containsKey(key)) { listeners.remove(key); } } protected abstract E createEntry(PlayerAdvancements advancement); protected static class Entry { protected final PlayerAdvancements advancement; protected final List> listeners = Lists.newArrayList(); Entry(PlayerAdvancements key) { advancement = key; } } }