package net.minecraft.advancements.critereon;

import javax.annotation.Nullable;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.Level;
import net.minecraft.resources.ResourceKey;
import net.minecraft.util.GsonHelper;
import net.minecraft.core.Registry;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class ChangeDimensionTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return ChangeDimensionTrigger.ID;
    }
    
    public TriggerInstance createInstance(final JsonObject jsonObject, final EntityPredicate.Composite b, final DeserializationContext av) {
        final ResourceKey<Level> ug5 = jsonObject.has("from") ? ResourceKey.<Level>create(Registry.DIMENSION_REGISTRY, new ResourceLocation(GsonHelper.getAsString(jsonObject, "from"))) : null;
        final ResourceKey<Level> ug6 = jsonObject.has("to") ? ResourceKey.<Level>create(Registry.DIMENSION_REGISTRY, new ResourceLocation(GsonHelper.getAsString(jsonObject, "to"))) : null;
        return new TriggerInstance(b, ug5, ug6);
    }
    
    public void trigger(final ServerPlayer ze, final ResourceKey<Level> ug2, final ResourceKey<Level> ug3) {
        this.trigger(ze, a -> a.matches(ug2, ug3));
    }
    
    static {
        ID = new ResourceLocation("changed_dimension");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        @Nullable
        private final ResourceKey<Level> from;
        @Nullable
        private final ResourceKey<Level> to;
        
        public TriggerInstance(final EntityPredicate.Composite b, @Nullable final ResourceKey<Level> ug2, @Nullable final ResourceKey<Level> ug3) {
            super(ChangeDimensionTrigger.ID, b);
            this.from = ug2;
            this.to = ug3;
        }
        
        public static TriggerInstance changedDimensionTo(final ResourceKey<Level> ug) {
            return new TriggerInstance(EntityPredicate.Composite.ANY, null, ug);
        }
        
        public boolean matches(final ResourceKey<Level> ug1, final ResourceKey<Level> ug2) {
            return (this.from == null || this.from == ug1) && (this.to == null || this.to == ug2);
        }
        
        @Override
        public JsonObject serializeToJson(final SerializationContext cg) {
            final JsonObject jsonObject3 = super.serializeToJson(cg);
            if (this.from != null) {
                jsonObject3.addProperty("from", this.from.location().toString());
            }
            if (this.to != null) {
                jsonObject3.addProperty("to", this.to.location().toString());
            }
            return jsonObject3;
        }
    }
}
