Config class

Пакет: com.hypixel.hytale.server.core.util

Файл: com/hypixel/hytale/server/core/util/Config.java

Поля (1)

МодификаторыТипИмя
Config var4

Методы (5)

МодификаторыВозвратСигнатура
abstract throw new IllegalStateExceptionthrow new IllegalStateException("Config is not loaded")
public T getT get()
if if(this.loadingConfig != null)
if if(this.config == null && this.loadingConfig == null)
if if(this.config == null && this.loadingConfig == null)

Исходный код

Показать/скрыть
class="kw">package com.hypixel.hytale.server.core.util;

class="kw">import com.hypixel.hytale.codec.ExtraInfo;
class="kw">import com.hypixel.hytale.codec.builder.BuilderCodec;
class="kw">import com.hypixel.hytale.codec.util.RawJsonReader;
class="kw">import com.hypixel.hytale.logger.HytaleLogger;
class="kw">import com.hypixel.hytale.sneakythrow.SneakyThrow;
class="kw">import java.nio.file.Files;
class="kw">import java.nio.file.Path;
class="kw">import java.util.concurrent.CompletableFuture;
class="kw">import javax.annotation.Nonnull;
class="kw">import javax.annotation.Nullable;

class="kw">public class Config<T> {
   @Nonnull
   class="kw">private class="kw">final Path path;
   class="kw">private class="kw">final String name;
   class="kw">private class="kw">final BuilderCodec<T> codec;
   @Nullable
   class="kw">private T config;
   @Nullable
   class="kw">private CompletableFuture<T> loadingConfig;

   class="kw">public Config(@Nonnull Path var1, String var2, BuilderCodec<T> var3) {
      this.path = var1.resolve(var2 + ".json");
      this.name = var2;
      this.codec = var3;
   }

   @Nonnull
   @Deprecated(forRemoval = true)
   class="kw">public class="kw">static <T> Config<T> preloadedConfig(@Nonnull Path var0, String var1, BuilderCodec<T> var2, T var3) {
      Config var4 = new Config<>(var0, var1, var2);
      var4.config = var3;
      class="kw">return var4;
   }

   @Nonnull
   class="kw">public CompletableFuture<T> load() {
      if (this.loadingConfig != null) {
         class="kw">return this.loadingConfig;
      } else if (!Files.exists(this.path)) {
         this.config = this.codec.getDefaultValue();
         class="kw">return CompletableFuture.completedFuture(this.config);
      } else {
         class="kw">return this.loadingConfig = CompletableFuture.supplyAsync(SneakyThrow.sneakySupplier(() -> {
            this.config = RawJsonReader.readSync(this.path, this.codec, HytaleLogger.getLogger());
            this.loadingConfig = null;
            class="kw">return this.config;
         }));
      }
   }

   class="kw">public T get() {
      if (this.config == null && this.loadingConfig == null) {
         throw new IllegalStateException("Config is not loaded");
      } else {
         class="kw">return this.loadingConfig != null ? this.loadingConfig.join() : this.config;
      }
   }

   @Nonnull
   class="kw">public CompletableFuture<Void> save() {
      if (this.config == null && this.loadingConfig == null) {
         throw new IllegalStateException("Config is not loaded");
      } else {
         class="kw">return this.loadingConfig != null
            ? CompletableFuture.completedFuture(null)
            : BsonUtil.writeDocument(this.path, this.codec.encode(this.config, new ExtraInfo()));
      }
   }
}