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.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.RandomValueBounds;
import javax.annotation.Nullable;

public class TimeCheck implements LootItemCondition {
    @Nullable
    private final Long period;
    private final RandomValueBounds value;
    
    private TimeCheck(@Nullable final Long long1, final RandomValueBounds csf) {
        this.period = long1;
        this.value = csf;
    }
    
    @Override
    public boolean test(final LootContext crx) {
        final ServerLevel xd3 = crx.getLevel();
        long long4 = xd3.getDayTime();
        if (this.period != null) {
            long4 %= this.period;
        }
        return this.value.matchesValue((int)long4);
    }
    
    public static class Serializer extends LootItemCondition.Serializer<TimeCheck> {
        public Serializer() {
            super(new ResourceLocation("time_check"), TimeCheck.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final TimeCheck cut, final JsonSerializationContext jsonSerializationContext) {
            jsonObject.addProperty("period", cut.period);
            jsonObject.add("value", jsonSerializationContext.serialize(cut.value));
        }
        
        @Override
        public TimeCheck deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final Long long4 = jsonObject.has("period") ? Long.valueOf(GsonHelper.getAsLong(jsonObject, "period")) : null;
            final RandomValueBounds csf5 = GsonHelper.<RandomValueBounds>getAsObject(jsonObject, "value", jsonDeserializationContext, RandomValueBounds.class);
            return new TimeCheck(long4, csf5, null);
        }
    }
}
