62 lines
3.1 KiB
Java
62 lines
3.1 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.datafixers.kinds.App
|
|
* com.mojang.datafixers.kinds.Applicative
|
|
* com.mojang.serialization.Codec
|
|
* com.mojang.serialization.codecs.RecordCodecBuilder
|
|
*/
|
|
package net.minecraft.advancements.criterion;
|
|
|
|
import com.mojang.datafixers.kinds.App;
|
|
import com.mojang.datafixers.kinds.Applicative;
|
|
import com.mojang.serialization.Codec;
|
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
|
import java.util.Optional;
|
|
import net.minecraft.advancements.criterion.ContextAwarePredicate;
|
|
import net.minecraft.advancements.criterion.CriterionValidator;
|
|
import net.minecraft.advancements.criterion.EntityPredicate;
|
|
import net.minecraft.advancements.criterion.SimpleCriterionTrigger;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.server.level.ServerPlayer;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.storage.loot.LootContext;
|
|
import net.minecraft.world.level.storage.loot.LootParams;
|
|
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
|
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
|
|
|
public class AnyBlockInteractionTrigger
|
|
extends SimpleCriterionTrigger<TriggerInstance> {
|
|
@Override
|
|
public Codec<TriggerInstance> codec() {
|
|
return TriggerInstance.CODEC;
|
|
}
|
|
|
|
public void trigger(ServerPlayer player, BlockPos pos, ItemStack itemStack) {
|
|
ServerLevel level = player.level();
|
|
BlockState state = level.getBlockState(pos);
|
|
LootParams params = new LootParams.Builder(level).withParameter(LootContextParams.ORIGIN, pos.getCenter()).withParameter(LootContextParams.THIS_ENTITY, player).withParameter(LootContextParams.BLOCK_STATE, state).withParameter(LootContextParams.TOOL, itemStack).create(LootContextParamSets.ADVANCEMENT_LOCATION);
|
|
LootContext context = new LootContext.Builder(params).create(Optional.empty());
|
|
this.trigger(player, t -> t.matches(context));
|
|
}
|
|
|
|
public record TriggerInstance(Optional<ContextAwarePredicate> player, Optional<ContextAwarePredicate> location) implements SimpleCriterionTrigger.SimpleInstance
|
|
{
|
|
public static final Codec<TriggerInstance> CODEC = RecordCodecBuilder.create(i -> i.group((App)EntityPredicate.ADVANCEMENT_CODEC.optionalFieldOf("player").forGetter(TriggerInstance::player), (App)ContextAwarePredicate.CODEC.optionalFieldOf("location").forGetter(TriggerInstance::location)).apply((Applicative)i, TriggerInstance::new));
|
|
|
|
public boolean matches(LootContext locationContext) {
|
|
return this.location.isEmpty() || this.location.get().matches(locationContext);
|
|
}
|
|
|
|
@Override
|
|
public void validate(CriterionValidator validator) {
|
|
SimpleCriterionTrigger.SimpleInstance.super.validate(validator);
|
|
this.location.ifPresent(predicate -> validator.validate((ContextAwarePredicate)predicate, LootContextParamSets.ADVANCEMENT_LOCATION, "location"));
|
|
}
|
|
}
|
|
}
|
|
|