package net.minecraft.world.level.storage.loot.predicates;

import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import net.minecraft.world.level.storage.loot.Serializer;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.core.BlockPos;
import net.minecraft.advancements.critereon.LocationPredicate;

public class LocationCheck implements LootItemCondition {
    private final LocationPredicate predicate;
    private final BlockPos offset;
    
    private LocationCheck(final LocationPredicate bu, final BlockPos fu) {
        this.predicate = bu;
        this.offset = fu;
    }
    
    @Override
    public LootItemConditionType getType() {
        return LootItemConditions.LOCATION_CHECK;
    }
    
    @Override
    public boolean test(final LootContext dat) {
        final BlockPos fu3 = dat.<BlockPos>getParamOrNull(LootContextParams.BLOCK_POS);
        return fu3 != null && this.predicate.matches(dat.getLevel(), (float)(fu3.getX() + this.offset.getX()), (float)(fu3.getY() + this.offset.getY()), (float)(fu3.getZ() + this.offset.getZ()));
    }
    
    public static Builder checkLocation(final LocationPredicate.Builder a) {
        return () -> new LocationCheck(a.build(), BlockPos.ZERO);
    }
    
    public static Builder checkLocation(final LocationPredicate.Builder a, final BlockPos fu) {
        return () -> new LocationCheck(a.build(), fu);
    }
    
    public static class Serializer implements net.minecraft.world.level.storage.loot.Serializer<LocationCheck> {
        @Override
        public void serialize(final JsonObject jsonObject, final LocationCheck ddk, final JsonSerializationContext jsonSerializationContext) {
            jsonObject.add("predicate", ddk.predicate.serializeToJson());
            if (ddk.offset.getX() != 0) {
                jsonObject.addProperty("offsetX", ddk.offset.getX());
            }
            if (ddk.offset.getY() != 0) {
                jsonObject.addProperty("offsetY", ddk.offset.getY());
            }
            if (ddk.offset.getZ() != 0) {
                jsonObject.addProperty("offsetZ", ddk.offset.getZ());
            }
        }
        
        @Override
        public LocationCheck deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final LocationPredicate bu4 = LocationPredicate.fromJson(jsonObject.get("predicate"));
            final int integer5 = GsonHelper.getAsInt(jsonObject, "offsetX", 0);
            final int integer6 = GsonHelper.getAsInt(jsonObject, "offsetY", 0);
            final int integer7 = GsonHelper.getAsInt(jsonObject, "offsetZ", 0);
            return new LocationCheck(bu4, new BlockPos(integer5, integer6, integer7), null);
        }
    }
}
