Code cleanup
This commit is contained in:
1 parent
595b2a7ef3
commit
704b4f778a
10 files changed
+61
-85
No files matched your search
@@ -12,7 +12,6 @@ import org.popcraft.chunky.platform.BukkitConfig;
|
||||
import org.popcraft.chunky.platform.BukkitSender;
|
||||
import org.popcraft.chunky.platform.BukkitServer;
|
||||
import org.popcraft.chunky.platform.Sender;
|
||||
import org.popcraft.chunky.util.Limit;
|
||||
import org.popcraft.chunky.util.Version;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -28,11 +27,7 @@ public final class ChunkyBukkit extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
this.chunky = new Chunky(new BukkitServer(this));
|
||||
chunky.setConfig(new BukkitConfig(chunky, this));
|
||||
chunky.setLanguage(chunky.getConfig().getLanguage());
|
||||
chunky.loadCommands();
|
||||
Limit.set(chunky.getConfig());
|
||||
this.chunky = new Chunky(new BukkitServer(this), new BukkitConfig(this));
|
||||
final Version currentVersion = Version.getCurrentMinecraftVersion();
|
||||
if (Version.v1_13_2.isEqualTo(currentVersion) && !PaperLib.isPaper()) {
|
||||
getLogger().severe(() -> translate("error_version_spigot"));
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package org.popcraft.chunky.platform;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.popcraft.chunky.Chunky;
|
||||
import org.popcraft.chunky.ChunkyBukkit;
|
||||
import org.popcraft.chunky.GenerationTask;
|
||||
import org.popcraft.chunky.Selection;
|
||||
import org.popcraft.chunky.util.Input;
|
||||
import org.popcraft.chunky.util.Translator;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
@@ -14,15 +14,14 @@ import java.util.Optional;
|
||||
|
||||
public class BukkitConfig implements Config {
|
||||
private static final String TASKS_KEY = "tasks.";
|
||||
private final Chunky chunky;
|
||||
private final JavaPlugin plugin;
|
||||
private final ChunkyBukkit plugin;
|
||||
|
||||
public BukkitConfig(Chunky chunky, JavaPlugin plugin) {
|
||||
this.chunky = chunky;
|
||||
public BukkitConfig(ChunkyBukkit plugin) {
|
||||
this.plugin = plugin;
|
||||
plugin.getConfig().options().copyDefaults(true);
|
||||
plugin.getConfig().options().copyHeader(true);
|
||||
plugin.saveConfig();
|
||||
Translator.setLanguage(getLanguage());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -51,13 +50,13 @@ public class BukkitConfig implements Config {
|
||||
.shape(config.getString(worldKey + "shape", "square"));
|
||||
long count = config.getLong(worldKey + "count", 0);
|
||||
long time = config.getLong(worldKey + "time", 0);
|
||||
return Optional.of(new GenerationTask(chunky, selection.build(), count, time));
|
||||
return Optional.of(new GenerationTask(plugin.getChunky(), selection.build(), count, time));
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized List<GenerationTask> loadTasks() {
|
||||
List<GenerationTask> generationTasks = new ArrayList<>();
|
||||
chunky.getServer().getWorlds().forEach(world -> loadTask(world).ifPresent(generationTasks::add));
|
||||
plugin.getChunky().getServer().getWorlds().forEach(world -> loadTask(world).ifPresent(generationTasks::add));
|
||||
return generationTasks;
|
||||
}
|
||||
|
||||
@@ -83,7 +82,7 @@ public class BukkitConfig implements Config {
|
||||
|
||||
@Override
|
||||
public synchronized void saveTasks() {
|
||||
chunky.getGenerationTasks().values().forEach(this::saveTask);
|
||||
plugin.getChunky().getGenerationTasks().values().forEach(this::saveTask);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,36 +5,43 @@ import org.popcraft.chunky.platform.Config;
|
||||
import org.popcraft.chunky.platform.Sender;
|
||||
import org.popcraft.chunky.platform.Server;
|
||||
import org.popcraft.chunky.platform.World;
|
||||
import org.popcraft.chunky.util.Input;
|
||||
import org.popcraft.chunky.util.PendingAction;
|
||||
import org.popcraft.chunky.util.RegionCache;
|
||||
import org.popcraft.chunky.util.TaskScheduler;
|
||||
import org.popcraft.chunky.util.Translator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class Chunky {
|
||||
private final Server server;
|
||||
private final TaskScheduler scheduler = new TaskScheduler();
|
||||
private final Map<World, GenerationTask> generationTasks;
|
||||
private final Config config;
|
||||
private final Selection.Builder selection;
|
||||
private final Options options;
|
||||
private final TaskScheduler scheduler = new TaskScheduler();
|
||||
private final Map<World, GenerationTask> generationTasks = new ConcurrentHashMap<>();
|
||||
private final Options options = new Options();
|
||||
private final Map<String, PendingAction> pendingActions = new HashMap<>();
|
||||
private final RegionCache regionCache;
|
||||
private Config config;
|
||||
private Map<String, ChunkyCommand> commands;
|
||||
private final RegionCache regionCache = new RegionCache();
|
||||
private final double limit;
|
||||
private final Map<String, ChunkyCommand> commands;
|
||||
|
||||
public Chunky(Server server) {
|
||||
public Chunky(Server server, Config config) {
|
||||
this.server = server;
|
||||
this.generationTasks = new ConcurrentHashMap<>();
|
||||
this.config = config;
|
||||
this.selection = Selection.builder(server.getWorlds().get(0));
|
||||
this.options = new Options();
|
||||
this.regionCache = new RegionCache();
|
||||
this.limit = loadLimit().orElse(Double.MAX_VALUE);
|
||||
this.commands = loadCommands();
|
||||
}
|
||||
|
||||
public void loadCommands() {
|
||||
private Map<String, ChunkyCommand> loadCommands() {
|
||||
final Map<String, ChunkyCommand> commandMap = new HashMap<>();
|
||||
commandMap.put("cancel", new CancelCommand(this));
|
||||
commandMap.put("center", new CenterCommand(this));
|
||||
@@ -55,7 +62,18 @@ public class Chunky {
|
||||
commandMap.put("trim", new TrimCommand(this));
|
||||
commandMap.put("worldborder", new WorldBorderCommand(this));
|
||||
commandMap.put("world", new WorldCommand(this));
|
||||
this.commands = commandMap;
|
||||
return commandMap;
|
||||
}
|
||||
|
||||
private Optional<Double> loadLimit() {
|
||||
final Path limitFile = config.getDirectory().resolve(".chunky.properties");
|
||||
try (final InputStream input = Files.newInputStream(limitFile)) {
|
||||
final Properties properties = new Properties();
|
||||
properties.load(input);
|
||||
return Input.tryDouble(properties.getProperty("radius-limit"));
|
||||
} catch (IOException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
@@ -76,10 +94,6 @@ public class Chunky {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void setConfig(Config config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public Map<World, GenerationTask> getGenerationTasks() {
|
||||
return generationTasks;
|
||||
}
|
||||
@@ -113,4 +127,8 @@ public class Chunky {
|
||||
public RegionCache getRegionCache() {
|
||||
return regionCache;
|
||||
}
|
||||
|
||||
public double getLimit() {
|
||||
return limit;
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import org.popcraft.chunky.platform.Sender;
|
||||
import org.popcraft.chunky.platform.World;
|
||||
import org.popcraft.chunky.util.Formatting;
|
||||
import org.popcraft.chunky.util.Input;
|
||||
import org.popcraft.chunky.util.Limit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -73,8 +72,8 @@ public class StartCommand extends ChunkyCommand {
|
||||
sender.sendMessagePrefixed("format_started_already", current.world().getName());
|
||||
return;
|
||||
}
|
||||
if (current.radiusX() > Limit.get()) {
|
||||
sender.sendMessagePrefixed("format_start_limit", Formatting.number(Limit.get()));
|
||||
if (current.radiusX() > chunky.getLimit()) {
|
||||
sender.sendMessagePrefixed("format_start_limit", Formatting.number(chunky.getLimit()));
|
||||
return;
|
||||
}
|
||||
final Runnable startAction = () -> {
|
||||
|
||||
@@ -9,8 +9,11 @@ import org.popcraft.chunky.Selection;
|
||||
import org.popcraft.chunky.platform.Config;
|
||||
import org.popcraft.chunky.platform.World;
|
||||
import org.popcraft.chunky.util.Input;
|
||||
import org.popcraft.chunky.util.Translator;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.nio.file.Files;
|
||||
@@ -20,15 +23,16 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class GsonConfig implements Config {
|
||||
private final Chunky chunky;
|
||||
private final Supplier<Chunky> chunky;
|
||||
private final Gson gson;
|
||||
private final Path configPath;
|
||||
private ConfigModel configModel;
|
||||
|
||||
public GsonConfig(Chunky chunky, File configFile) {
|
||||
public GsonConfig(Supplier<Chunky> chunky, File configFile) {
|
||||
this.chunky = chunky;
|
||||
this.gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
this.configPath = configFile.toPath();
|
||||
@@ -47,6 +51,7 @@ public class GsonConfig implements Config {
|
||||
} else {
|
||||
reload();
|
||||
}
|
||||
Translator.setLanguage(getLanguage());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -76,13 +81,13 @@ public class GsonConfig implements Config {
|
||||
.shape(taskModel.shape);
|
||||
long count = taskModel.count;
|
||||
long time = taskModel.time;
|
||||
return Optional.of(new GenerationTask(chunky, selection.build(), count, time));
|
||||
return Optional.of(new GenerationTask(chunky.get(), selection.build(), count, time));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GenerationTask> loadTasks() {
|
||||
List<GenerationTask> generationTasks = new ArrayList<>();
|
||||
chunky.getServer().getWorlds().forEach(world -> loadTask(world).ifPresent(generationTasks::add));
|
||||
chunky.get().getServer().getWorlds().forEach(world -> loadTask(world).ifPresent(generationTasks::add));
|
||||
return generationTasks;
|
||||
}
|
||||
|
||||
@@ -115,7 +120,7 @@ public class GsonConfig implements Config {
|
||||
|
||||
@Override
|
||||
public void saveTasks() {
|
||||
chunky.getGenerationTasks().values().forEach(this::saveTask);
|
||||
chunky.get().getGenerationTasks().values().forEach(this::saveTask);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -162,7 +167,7 @@ public class GsonConfig implements Config {
|
||||
}
|
||||
|
||||
public void saveConfig() {
|
||||
try (Writer writer = Files.newBufferedWriter(configPath)) {
|
||||
try (Writer writer = new BufferedWriter(new FileWriter(configPath.toFile()))) {
|
||||
gson.toJson(configModel, new TypeToken<ConfigModel>() {
|
||||
}.getType(), writer);
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
package org.popcraft.chunky.util;
|
||||
|
||||
import org.popcraft.chunky.platform.Config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Properties;
|
||||
|
||||
public class Limit {
|
||||
private static double limit = Integer.MAX_VALUE;
|
||||
|
||||
public static void set(final Config config) {
|
||||
final Path limitFile = config.getDirectory().resolve(".chunky.properties");
|
||||
try (final InputStream input = Files.newInputStream(limitFile)) {
|
||||
final Properties properties = new Properties();
|
||||
properties.load(input);
|
||||
Input.tryDouble(properties.getProperty("radius-limit")).ifPresent(radius -> limit = radius);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
public static double get() {
|
||||
return limit;
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,6 @@ import org.popcraft.chunky.platform.FabricSender;
|
||||
import org.popcraft.chunky.platform.FabricServer;
|
||||
import org.popcraft.chunky.platform.Sender;
|
||||
import org.popcraft.chunky.platform.impl.GsonConfig;
|
||||
import org.popcraft.chunky.util.Limit;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
@@ -33,12 +32,8 @@ public class ChunkyFabric implements ModInitializer {
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
ServerLifecycleEvents.SERVER_STARTED.register(minecraftServer -> {
|
||||
this.chunky = new Chunky(new FabricServer(this, minecraftServer));
|
||||
File configFile = new File(FabricLoader.getInstance().getConfigDir().toFile(), "chunky.json");
|
||||
chunky.setConfig(new GsonConfig(chunky, configFile));
|
||||
chunky.setLanguage(chunky.getConfig().getLanguage());
|
||||
chunky.loadCommands();
|
||||
Limit.set(chunky.getConfig());
|
||||
this.chunky = new Chunky(new FabricServer(this, minecraftServer), new GsonConfig(() -> chunky, configFile));
|
||||
if (chunky.getConfig().getContinueOnRestart()) {
|
||||
chunky.getCommands().get("continue").execute(chunky.getServer().getConsoleSender(), new String[]{});
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ import org.popcraft.chunky.platform.ForgeSender;
|
||||
import org.popcraft.chunky.platform.ForgeServer;
|
||||
import org.popcraft.chunky.platform.Sender;
|
||||
import org.popcraft.chunky.platform.impl.GsonConfig;
|
||||
import org.popcraft.chunky.util.Limit;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
@@ -42,12 +41,8 @@ public class ChunkyForge {
|
||||
@SubscribeEvent
|
||||
public void onServerStarting(FMLServerStartingEvent event) {
|
||||
MinecraftServer server = event.getServer();
|
||||
this.chunky = new Chunky(new ForgeServer(this, server));
|
||||
File configFile = new File(event.getServer().getServerDirectory(), "config/chunky.json");
|
||||
chunky.setConfig(new GsonConfig(chunky, configFile));
|
||||
chunky.setLanguage(chunky.getConfig().getLanguage());
|
||||
chunky.loadCommands();
|
||||
Limit.set(chunky.getConfig());
|
||||
this.chunky = new Chunky(new ForgeServer(this, server), new GsonConfig(() -> chunky, configFile));
|
||||
if (chunky.getConfig().getContinueOnRestart()) {
|
||||
chunky.getCommands().get("continue").execute(chunky.getServer().getConsoleSender(), new String[]{});
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import org.popcraft.chunky.command.suggestion.SuggestionProviders;
|
||||
import org.popcraft.chunky.platform.SpongeConfig;
|
||||
import org.popcraft.chunky.platform.SpongeSender;
|
||||
import org.popcraft.chunky.platform.SpongeServer;
|
||||
import org.popcraft.chunky.util.Limit;
|
||||
import org.spongepowered.api.Game;
|
||||
import org.spongepowered.api.ResourceKey;
|
||||
import org.spongepowered.api.Server;
|
||||
@@ -48,11 +47,7 @@ public class ChunkySponge {
|
||||
|
||||
@Listener
|
||||
public void onLoadedGame(final LoadedGameEvent event) {
|
||||
this.chunky = new Chunky(new SpongeServer(this));
|
||||
chunky.setConfig(new SpongeConfig(this));
|
||||
chunky.setLanguage(chunky.getConfig().getLanguage());
|
||||
chunky.loadCommands();
|
||||
Limit.set(chunky.getConfig());
|
||||
this.chunky = new Chunky(new SpongeServer(this), new SpongeConfig(this));
|
||||
if (chunky.getConfig().getContinueOnRestart()) {
|
||||
chunky.getCommands().get("continue").execute(chunky.getServer().getConsoleSender(), new String[]{});
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.popcraft.chunky.ChunkySponge;
|
||||
import org.popcraft.chunky.GenerationTask;
|
||||
import org.popcraft.chunky.Selection;
|
||||
import org.popcraft.chunky.util.Input;
|
||||
import org.popcraft.chunky.util.Translator;
|
||||
import org.spongepowered.configurate.CommentedConfigurationNode;
|
||||
import org.spongepowered.configurate.ConfigurationNode;
|
||||
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
|
||||
@@ -57,6 +58,7 @@ public class SpongeConfig implements Config {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Translator.setLanguage(getLanguage());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user