···6677### WIP [1.4.0]
88#### Changed
99-- The loading logic of the storage file
99+- the loading logic of the storage file.
1010+- most static classes to non-static, should prevent leaks.
1111+- Changed out java Timers for a tick-based timer.
10121113#### Added
1214- **a config file!**
···172172 String worldString = player.serverLevel().dimension().location().toString();
173173174174 // Gets the player's storage and creates it if it doesn't exist
175175- Player playerStorage = StorageManager.STORAGE.addPlayer(player.getStringUUID());
175175+ Player playerStorage = StorageManager.storage.addPlayer(player.getStringUUID());
176176177177 // Create the NamedLocation
178178 NamedLocation warp = new NamedLocation(homeName, blockPos, worldString);
···1818import static java.util.Collections.unmodifiableList;
19192020public class StorageManager {
2121- public Path STORAGE_FOLDER;
2222- public Path STORAGE_FILE;
2323- public StorageClass STORAGE;
2121+ public Path storageFolder;
2222+ public Path storageFile;
2323+ public StorageClass storage;
24242525- private final Gson GSON = new GsonBuilder().create();
2525+ private final Gson gson = new GsonBuilder().create();
2626 private final int defaultVersion = new StorageClass().getVersion();
2727 private final TeleportCommands teleportCommands;
28282929 /// Initializes the StorageManager class and loads the storage from the filesystem.
3030 public StorageManager(TeleportCommands teleportCommands) {
3131 this.teleportCommands = teleportCommands;
3232- STORAGE_FOLDER = teleportCommands.saveDir.resolve("TeleportCommands/");
3333- STORAGE_FILE = STORAGE_FOLDER.resolve("storage.json");
3232+ storageFolder = teleportCommands.saveDir.resolve("TeleportCommands/");
3333+ storageFile = storageFolder.resolve("storage.json");
34343535 try {
3636 StorageLoader();
···4545 /// Loads the storage from the filesystem
4646 public void StorageLoader() throws Exception {
47474848- if (!STORAGE_FILE.toFile().exists() || STORAGE_FILE.toFile().length() == 0) {
4848+ if (!storageFile.toFile().exists() || storageFile.toFile().length() == 0) {
4949 Constants.LOGGER.warn("Storage file was not found or was empty! Initializing storage");
50505151- Files.createDirectories(STORAGE_FOLDER);
5252- STORAGE = new StorageClass();
5151+ Files.createDirectories(storageFolder);
5252+ storage = new StorageClass();
5353 StorageSaver();
5454 Constants.LOGGER.info("Storage created successfully!");
5555 }
56565757 StorageMigrator();
58585959- FileReader reader = new FileReader(STORAGE_FILE.toFile());
6060- STORAGE = GSON.fromJson(reader, StorageClass.class);
6161- if (STORAGE == null) {
5959+ FileReader reader = new FileReader(storageFile.toFile());
6060+ storage = gson.fromJson(reader, StorageClass.class);
6161+ if (storage == null) {
6262 Constants.LOGGER.warn("Storage file was empty! Initializing storage");
6363- STORAGE = new StorageClass();
6363+ storage = new StorageClass();
6464 StorageSaver();
6565 }
66666767- STORAGE.cleanup();
6767+ storage.cleanup();
68686969 StorageSaver(); // Save it so any missing values get added to the file.
7070 Constants.LOGGER.info("Storage loaded successfully!");
···72727373 /// This function checks what version the storage file is and migrates it to the current version of the mod.
7474 public void StorageMigrator() throws Exception {
7575- FileReader reader = new FileReader(STORAGE_FILE.toFile());
7676- JsonObject jsonObject = GSON.fromJson(reader, JsonObject.class);
7575+ FileReader reader = new FileReader(storageFile.toFile());
7676+ JsonObject jsonObject = gson.fromJson(reader, JsonObject.class);
77777878 int version = jsonObject.has("version") ? jsonObject.get("version").getAsInt() : 0;
7979···109109 }
110110111111 // Save the storage :3
112112- byte[] json = GSON.toJson(jsonObject, JsonArray.class).getBytes();
113113- Files.write(STORAGE_FILE, json, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
112112+ byte[] json = gson.toJson(jsonObject, JsonArray.class).getBytes();
113113+ Files.write(storageFile, json, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
114114115115 Constants.LOGGER.info("Storage file migrated to v{} successfully!", defaultVersion);
116116 } else if (version > defaultVersion) {
117117 String message = String.format("Teleport Commands: The storage file's version is newer than the supported version, found v%s, expected <= v%s.\n" +
118118 "If you intentionally backported then you can attempt to downgrade the storage file located at this location: \"%s\".\n",
119119- version, defaultVersion, STORAGE_FILE.toAbsolutePath());
119119+ version, defaultVersion, storageFile.toAbsolutePath());
120120121121 throw new IllegalStateException(message);
122122 }
···125125 /// Saves the storage to the filesystem
126126 public void StorageSaver() throws Exception {
127127 // todo! maybe throttle saves?
128128- byte[] json = GSON.toJson( this.STORAGE ).getBytes();
128128+ byte[] json = gson.toJson( this.storage).getBytes();
129129130130- Files.write(STORAGE_FILE, json, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
130130+ Files.write(storageFile, json, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
131131 }
132132133133···147147 List<NamedLocation> homes = player.getHomes();
148148149149 // Delete any homes with an invalid world_id (if enabled in config)
150150- if (teleportCommands.config.CONFIG.home.isDeleteInvalid()) {
150150+ if (teleportCommands.config.config.home.isDeleteInvalid()) {
151151 homes.removeIf(home -> home.getWorld().isEmpty());
152152 }
153153···158158 }
159159160160 // Delete any warps with an invalid world_id (if enabled in config)
161161- if (teleportCommands.config.CONFIG.warp.isDeleteInvalid()) {
161161+ if (teleportCommands.config.config.warp.isDeleteInvalid()) {
162162 Warps.removeIf(warp -> warp.getWorld().isEmpty());
163163 }
164164
···1010import java.nio.file.StandardOpenOption;
11111212public class configManager {
1313- public Path CONFIG_FILE;
1414- public ConfigClass CONFIG;
1313+ public Path configFile;
1414+ public ConfigClass config;
15151616- private final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
1616+ private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
1717 private final int defaultVersion = new ConfigClass().getVersion();
1818 private final TeleportCommands teleportCommands;
19192020 public configManager(TeleportCommands teleportCommands) {
2121 this.teleportCommands = teleportCommands;
2222- CONFIG_FILE = teleportCommands.configDir.resolve("teleport_commands.json");
2222+ configFile = teleportCommands.configDir.resolve("teleport_commands.json");
23232424 try {
2525 configLoader();
···33333434 /// This function loads the config from disk
3535 public void configLoader() throws Exception {
3636- if (!CONFIG_FILE.toFile().exists() || CONFIG_FILE.toFile().length() == 0) {
3636+ if (!configFile.toFile().exists() || configFile.toFile().length() == 0) {
3737 Files.createDirectories(teleportCommands.configDir);
38383939 Constants.LOGGER.warn("Config file was not found or was empty! Initializing config");
4040- CONFIG = new ConfigClass();
4040+ config = new ConfigClass();
4141 configSaver();
4242 Constants.LOGGER.info("Config created successfully!");
4343 }
44444545 configMigrator();
46464747- FileReader reader = new FileReader(CONFIG_FILE.toFile());
4848- CONFIG = GSON.fromJson(reader, ConfigClass.class);
4949- if (CONFIG == null) {
4747+ FileReader reader = new FileReader(configFile.toFile());
4848+ config = gson.fromJson(reader, ConfigClass.class);
4949+ if (config == null) {
5050 Constants.LOGGER.warn("Config file was empty! Loading defaults...");
5151- CONFIG = new ConfigClass();
5151+ config = new ConfigClass();
5252 configSaver();
5353 }
5454···58585959 /// This function checks what version the config file is and migrates it to the current version of the mod.
6060 public void configMigrator() throws Exception {
6161- FileReader reader = new FileReader(CONFIG_FILE.toFile());
6262- JsonObject jsonObject = GSON.fromJson(reader, JsonObject.class);
6161+ FileReader reader = new FileReader(configFile.toFile());
6262+ JsonObject jsonObject = gson.fromJson(reader, JsonObject.class);
63636464 int version = jsonObject.has("version") ? jsonObject.get("version").getAsInt() : 0;
6565···7474 } else if (version > defaultVersion) {
7575 String message = String.format("Teleport Commands: The config file's version is newer than the supported version, found v%s, expected <= v%s.\n" +
7676 "If you intentionally backported then you can attempt to downgrade the config file located at this location: \"%s\".\n",
7777- version, defaultVersion, CONFIG_FILE.toAbsolutePath());
7777+ version, defaultVersion, configFile.toAbsolutePath());
78787979 throw new IllegalStateException(message);
8080 }
···8383 /// Saves the config to disk
8484 public void configSaver() throws Exception {
8585 // todo! maybe throttle saves?
8686- byte[] json = GSON.toJson(CONFIG).getBytes();
8686+ byte[] json = gson.toJson(config).getBytes();
87878888- Files.write(CONFIG_FILE, json, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
8888+ Files.write(configFile, json, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
8989 }
90909191 public static class ConfigClass {
···102102 }
103103104104 public static final class Teleporting {
105105- private int delay = 5;
105105+ /// Delay before teleporting
106106+ private int delay = 3;
107107+ /// Cooldown before they can teleport again
108108+ private int cooldown = 5;
109109+ /// Allow moving while teleporting
106110 private boolean whileMoving = true;
111111+ /// Allow fighting while teleporting
107112 private boolean whileFighting = false;
113113+ /// Cooldown after fighting before they can teleport again
108114 private int fightCooldown = 10;
109115110116 public int getDelay() {
···138144 public void setFightCooldown(int fightCooldown) {
139145 this.fightCooldown = fightCooldown;
140146 }
147147+148148+ public int getCooldown() {
149149+ return cooldown;
150150+ }
151151+152152+ public void setCooldown(int cooldown) {
153153+ this.cooldown = cooldown;
154154+ }
141155 }
142156143157 public static final class Back {
144158 private boolean enabled = true;
159159+ /// Deletes the /back after teleporting, so you cant call /back twice.
145160 private boolean deleteAfterTeleport = false;
146161147162 public boolean isEnabled() {
···163178164179 public static final class Home {
165180 private boolean enabled = true;
181181+ /// The maximum amount of homes a player can have
166182 private int playerMaximum = 20;
183183+ /// If a home with an invalid dimension should get automatically deleted
167184 private boolean deleteInvalid = false;
168185169186 public boolean isEnabled() {
···205222206223 public static final class Warp {
207224 private boolean enabled = true;
225225+ /// If a warp with an invalid dimension should get automatically deleted
208226 private boolean deleteInvalid = false;
209227210228 public boolean isEnabled() {
···1414import net.minecraft.core.BlockPos;
1515import net.minecraft.network.chat.Component;
1616import net.minecraft.network.chat.MutableComponent;
1717+import net.minecraft.server.MinecraftServer;
1718import net.minecraft.server.level.ServerLevel;
1819import net.minecraft.server.level.ServerPlayer;
1920···212222232324public class tools {
2424-2525-2625 private static final Set<String> unsafeCollisionFreeBlocks = Set.of("block.minecraft.lava", "block.minecraft.flowing_lava", "block.minecraft.end_portal", "block.minecraft.end_gateway","block.minecraft.fire", "block.minecraft.soul_fire", "block.minecraft.powder_snow", "block.minecraft.nether_portal");
27262827 // checks a 7x7x7 location around the player in order to find a safe place to teleport them to.
···155154156155157156 // Gets the ids of all the worlds
158158- public static List<String> getWorldIds() {
159159- return StreamSupport.stream(teleportCommands.server.getAllLevels().spliterator(), false)
157157+ public static List<String> getWorldIds(MinecraftServer server) {
158158+ return StreamSupport.stream(server.getAllLevels().spliterator(), false)
160159 .map(level -> level.dimension().location().toString())
161160 .toList();
162161 }