package com.mojang.realmsclient.util.task;

import com.mojang.realmsclient.exception.RetryCallException;
import com.mojang.realmsclient.client.RealmsClient;
import net.minecraft.client.resources.language.I18n;
import javax.annotation.Nullable;
import com.mojang.realmsclient.dto.WorldTemplate;

public class ResettingWorldTask extends LongRunningTask {
    private final String seed;
    private final WorldTemplate worldTemplate;
    private final int levelType;
    private final boolean generateStructures;
    private final long serverId;
    private String title;
    private final Runnable callback;
    
    public ResettingWorldTask(@Nullable final String string1, @Nullable final WorldTemplate djd, final int integer, final boolean boolean4, final long long5, @Nullable final String string6, final Runnable runnable) {
        this.title = I18n.get("mco.reset.world.resetting.screen.title");
        this.seed = string1;
        this.worldTemplate = djd;
        this.levelType = integer;
        this.generateStructures = boolean4;
        this.serverId = long5;
        if (string6 != null) {
            this.title = string6;
        }
        this.callback = runnable;
    }
    
    @Override
    public void run() {
        final RealmsClient dia2 = RealmsClient.create();
        this.setTitle(this.title);
        for (int integer3 = 0; integer3 < 25; ++integer3) {
            try {
                if (this.aborted()) {
                    return;
                }
                if (this.worldTemplate != null) {
                    dia2.resetWorldWithTemplate(this.serverId, this.worldTemplate.id);
                }
                else {
                    dia2.resetWorldWithSeed(this.serverId, this.seed, this.levelType, this.generateStructures);
                }
                if (this.aborted()) {
                    return;
                }
                this.callback.run();
                return;
            }
            catch (RetryCallException dji4) {
                if (this.aborted()) {
                    return;
                }
                LongRunningTask.pause(dji4.delaySeconds);
            }
            catch (Exception exception4) {
                if (this.aborted()) {
                    return;
                }
                ResettingWorldTask.LOGGER.error("Couldn't reset world");
                this.error(exception4.toString());
                return;
            }
        }
    }
}
