2025-11-24 22:52:51 +03:00

88 lines
3.3 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.google.common.collect.Lists
* com.google.common.collect.Maps
* com.google.common.collect.Sets
*/
package net.minecraft.advancements.criterion;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import net.minecraft.advancements.CriterionTrigger;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.advancements.criterion.ContextAwarePredicate;
import net.minecraft.advancements.criterion.CriterionValidator;
import net.minecraft.advancements.criterion.EntityPredicate;
import net.minecraft.server.PlayerAdvancements;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.storage.loot.LootContext;
public abstract class SimpleCriterionTrigger<T extends SimpleInstance>
implements CriterionTrigger<T> {
private final Map<PlayerAdvancements, Set<CriterionTrigger.Listener<T>>> players = Maps.newIdentityHashMap();
@Override
public final void addPlayerListener(PlayerAdvancements player, CriterionTrigger.Listener<T> listener) {
this.players.computeIfAbsent(player, k -> Sets.newHashSet()).add(listener);
}
@Override
public final void removePlayerListener(PlayerAdvancements player, CriterionTrigger.Listener<T> listener) {
Set<CriterionTrigger.Listener<T>> listeners = this.players.get(player);
if (listeners != null) {
listeners.remove(listener);
if (listeners.isEmpty()) {
this.players.remove(player);
}
}
}
@Override
public final void removePlayerListeners(PlayerAdvancements player) {
this.players.remove(player);
}
protected void trigger(ServerPlayer player, Predicate<T> matcher) {
PlayerAdvancements advancements = player.getAdvancements();
Set<CriterionTrigger.Listener<T>> allListeners = this.players.get(advancements);
if (allListeners == null || allListeners.isEmpty()) {
return;
}
LootContext playerContext = EntityPredicate.createContext(player, player);
List listeners = null;
for (CriterionTrigger.Listener<Object> listener : allListeners) {
Optional<ContextAwarePredicate> predicate;
SimpleInstance triggerInstance = (SimpleInstance)listener.trigger();
if (!matcher.test(triggerInstance) || !(predicate = triggerInstance.player()).isEmpty() && !predicate.get().matches(playerContext)) continue;
if (listeners == null) {
listeners = Lists.newArrayList();
}
listeners.add(listener);
}
if (listeners != null) {
for (CriterionTrigger.Listener<Object> listener : listeners) {
listener.run(advancements);
}
}
}
public static interface SimpleInstance
extends CriterionTriggerInstance {
@Override
default public void validate(CriterionValidator validator) {
validator.validateEntity(this.player(), "player");
}
public Optional<ContextAwarePredicate> player();
}
}