package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import javax.annotation.Nullable;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
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;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final DimensionType cbi4 = jsonObject.has("from") ? DimensionType.getByName(new ResourceLocation(GsonHelper.getAsString(jsonObject, "from"))) : null;
        final DimensionType cbi5 = jsonObject.has("to") ? DimensionType.getByName(new ResourceLocation(GsonHelper.getAsString(jsonObject, "to"))) : null;
        return new TriggerInstance(cbi4, cbi5);
    }
    
    public void trigger(final ServerPlayer xe, final DimensionType cbi2, final DimensionType cbi3) {
        this.trigger(xe.getAdvancements(), a -> a.matches(cbi2, cbi3));
    }
    
    static {
        ID = new ResourceLocation("changed_dimension");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        @Nullable
        private final DimensionType from;
        @Nullable
        private final DimensionType to;
        
        public TriggerInstance(@Nullable final DimensionType cbi1, @Nullable final DimensionType cbi2) {
            super(ChangeDimensionTrigger.ID);
            this.from = cbi1;
            this.to = cbi2;
        }
        
        public static TriggerInstance changedDimensionTo(final DimensionType cbi) {
            return new TriggerInstance(null, cbi);
        }
        
        public boolean matches(final DimensionType cbi1, final DimensionType cbi2) {
            return (this.from == null || this.from == cbi1) && (this.to == null || this.to == cbi2);
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            if (this.from != null) {
                jsonObject2.addProperty("from", DimensionType.getName(this.from).toString());
            }
            if (this.to != null) {
                jsonObject2.addProperty("to", DimensionType.getName(this.to).toString());
            }
            return jsonObject2;
        }
    }
}
