···44import dev.mrsnowy.teleport_commands.storage.StorageManager;
55import dev.mrsnowy.teleport_commands.commands.*;
66import dev.mrsnowy.teleport_commands.storage.DeathLocationStorage;
77-import dev.mrsnowy.teleport_commands.storage.ConfigManager;
77+import dev.mrsnowy.teleport_commands.storage.configManager;
88+import dev.mrsnowy.teleport_commands.utils.teleporter;
89import dev.mrsnowy.teleport_commands.utils.tools;
910import net.minecraft.commands.CommandSourceStack;
1011import net.minecraft.server.MinecraftServer;
···2122 public Path configDir;
2223 public MinecraftServer server;
2324 public StorageManager storageManager;
2424- public ConfigManager config;
2525+ public configManager config;
2526 public DeathLocationStorage deathLocationStorage;
2626- public tools tools;
2727+ public teleporter teleporter;
27282829 // Gets ran when the server starts, initializes the mod :3
2930 public void initializeMod(MinecraftServer server) {
···3233 saveDir = Path.of(String.valueOf(server.getWorldPath(LevelResource.ROOT)));
3334 configDir = Paths.get(System.getProperty("user.dir")).resolve("config");
3435 this.server = server;
3535- this.tools = new tools();
3636- this.storageManager = new StorageManager();
3737- this.config = new ConfigManager();
38363737+ storageManager = new StorageManager(this);
3838+ config = new configManager(this);
3939 deathLocationStorage = new DeathLocationStorage();
4040+ teleporter = new teleporter(this);
4041 }
41424243 // initialize commands, also allows me to easily disable any when there is a config
···99import java.nio.file.Path;
1010import java.nio.file.StandardOpenOption;
11111212-public class ConfigManager {
1212+public class configManager {
1313 public Path CONFIG_FILE;
1414 public ConfigClass CONFIG;
1515+1516 private final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
1617 private final int defaultVersion = new ConfigClass().getVersion();
1818+ private final TeleportCommands teleportCommands;
17191818- public ConfigManager() {
1919- CONFIG_FILE = TeleportCommands.TeleportCommands.configDir.resolve("teleport_commands.json");
2020+ public configManager(TeleportCommands teleportCommands) {
2121+ this.teleportCommands = teleportCommands;
2222+ CONFIG_FILE = teleportCommands.configDir.resolve("teleport_commands.json");
20232124 try {
2222- ConfigLoader();
2525+ configLoader();
23262427 } catch (Exception e) {
2528 // crashing is probably better here, otherwise the whole mod will be broken
···2831 }
2932 }
30333131- public void ConfigLoader() throws Exception {
3434+ /// This function loads the config from disk
3535+ public void configLoader() throws Exception {
3236 if (!CONFIG_FILE.toFile().exists() || CONFIG_FILE.toFile().length() == 0) {
3333- Files.createDirectories(TeleportCommands.configDir);
3737+ Files.createDirectories(teleportCommands.configDir);
34383539 Constants.LOGGER.warn("Config file was not found or was empty! Initializing config");
3640 CONFIG = new ConfigClass();
3737- ConfigSaver();
4141+ configSaver();
3842 Constants.LOGGER.info("Config created successfully!");
3943 }
40444141- ConfigMigrator();
4545+ configMigrator();
42464347 FileReader reader = new FileReader(CONFIG_FILE.toFile());
4448 CONFIG = GSON.fromJson(reader, ConfigClass.class);
4549 if (CONFIG == null) {
4650 Constants.LOGGER.warn("Config file was empty! Loading defaults...");
4751 CONFIG = new ConfigClass();
4848- ConfigSaver();
5252+ configSaver();
4953 }
50545151- ConfigSaver(); // Save it so any missing values get added to the file.
5555+ configSaver(); // Save it so any missing values get added to the file.
5256 Constants.LOGGER.info("Config loaded successfully!");
5357 }
54585559 /// This function checks what version the config file is and migrates it to the current version of the mod.
5656- public void ConfigMigrator() throws Exception {
6060+ public void configMigrator() throws Exception {
5761 FileReader reader = new FileReader(CONFIG_FILE.toFile());
5862 JsonObject jsonObject = GSON.fromJson(reader, JsonObject.class);
5963···7680 }
7781 }
78827979- public void ConfigSaver() throws Exception {
8383+ /// Saves the config to disk
8484+ public void configSaver() throws Exception {
8085 // todo! maybe throttle saves?
8181- byte[] json = GSON.toJson( ConfigManager.CONFIG ).getBytes();
8686+ byte[] json = GSON.toJson(CONFIG).getBytes();
82878388 Files.write(CONFIG_FILE, json, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
8489 }
85908686- public class ConfigClass {
9191+ public static class ConfigClass {
8792 private final int version = 0;
8893 public Teleporting teleporting = new Teleporting();
8994 public Back back = new Back();
···96101 return version;
97102 }
981039999- public final class Teleporting {
104104+ public static final class Teleporting {
100105 private int delay = 5;
101106 private boolean whileMoving = true;
102107 private boolean whileFighting = false;
···135140 }
136141 }
137142138138- public final class Back {
143143+ public static final class Back {
139144 private boolean enabled = true;
140145 private boolean deleteAfterTeleport = false;
141146···156161 }
157162 }
158163159159- public final class Home {
164164+ public static final class Home {
160165 private boolean enabled = true;
161166 private int playerMaximum = 20;
162167 private boolean deleteInvalid = false;
···186191 }
187192 }
188193189189- public final class Tpa {
194194+ public static final class Tpa {
190195 private boolean enabled = true;
191196192197 public boolean isEnabled() {
···198203 }
199204 }
200205201201- public final class Warp {
206206+ public static final class Warp {
202207 private boolean enabled = true;
203208 private boolean deleteInvalid = false;
204209···219224 }
220225 }
221226222222- public final class WorldSpawn {
227227+ public static final class WorldSpawn {
223228 private boolean enabled = true;
224229 private String world_id = "minecraft:overworld";
225230
···2121 public Path STORAGE_FOLDER;
2222 public Path STORAGE_FILE;
2323 public StorageClass STORAGE;
2424+2425 private final Gson GSON = new GsonBuilder().create();
2526 private final int defaultVersion = new StorageClass().getVersion();
2727+ private final TeleportCommands teleportCommands;
26282729 /// Initializes the StorageManager class and loads the storage from the filesystem.
2828- public StorageManager() {
2929- STORAGE_FOLDER = TeleportCommands.TeleportCommands.saveDir.resolve("TeleportCommands/");
3030+ public StorageManager(TeleportCommands teleportCommands) {
3131+ this.teleportCommands = teleportCommands;
3232+ STORAGE_FOLDER = teleportCommands.saveDir.resolve("TeleportCommands/");
3033 STORAGE_FILE = STORAGE_FOLDER.resolve("storage.json");
31343235 try {
···144147 List<NamedLocation> homes = player.getHomes();
145148146149 // Delete any homes with an invalid world_id (if enabled in config)
147147- if (TeleportCommands.TeleportCommands.config.CONFIG.home.isDeleteInvalid()) {
150150+ if (teleportCommands.config.CONFIG.home.isDeleteInvalid()) {
148151 homes.removeIf(home -> home.getWorld().isEmpty());
149152 }
150153···155158 }
156159157160 // Delete any warps with an invalid world_id (if enabled in config)
158158- if (TeleportCommands.TeleportCommands.config.CONFIG.warp.isDeleteInvalid()) {
161161+ if (teleportCommands.config.CONFIG.warp.isDeleteInvalid()) {
159162 Warps.removeIf(warp -> warp.getWorld().isEmpty());
160163 }
161164
···1111import java.util.regex.Pattern;
1212import java.util.stream.StreamSupport;
13131414-import dev.mrsnowy.teleport_commands.storage.ConfigManager;
1514import net.minecraft.core.BlockPos;
1616-import net.minecraft.core.particles.ParticleTypes;
1715import net.minecraft.network.chat.Component;
1816import net.minecraft.network.chat.MutableComponent;
1917import net.minecraft.server.level.ServerLevel;
2018import net.minecraft.server.level.ServerPlayer;
2121-import net.minecraft.sounds.SoundEvent;
2222-import net.minecraft.sounds.SoundSource;
2323-import net.minecraft.world.phys.Vec3;
24192520import static dev.mrsnowy.teleport_commands.Constants.MOD_ID;
2626-import static net.minecraft.sounds.SoundEvents.ENDERMAN_TELEPORT;
272128222923public class tools {
3030- private final TeleportCommands teleportCommands;
31243232- private 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");
3333-3434- private final Map<UUID, PlayerData> playerData = new HashMap<>();
35253636- private class PlayerData {
3737- long lastTeleportTime = 0;
3838- long lastHitTime = 0;
3939- Vec3 lastPosition = Vec3.ZERO;
4040- }
4141-4242- public tools(TeleportCommands teleportCommands) {
4343- this.teleportCommands = teleportCommands;
4444- }
4545-4646- /// Teleport the player :P
4747- public void Teleporter(ServerPlayer player, ServerLevel world, Vec3 coords) {
4848- // Check if user is allowed to teleport by config settings
4949-5050- int delay = teleportCommands.config.CONFIG.teleporting.getDelay();
5151-5252- // save when they last teleported and check delay
5353-5454- // save pos and check if they have moved.
5555-5656- // check if they got hit? whileFighting
5757-5858- // save when they last got hit and if it exceeds fightCooldown
5959-6060-6161- // teleportation effects & sounds before teleporting
6262- world.sendParticles(ParticleTypes.SNOWFLAKE, player.getX(), player.getY() + 1, player.getZ(), 20, 0.0D, 0.0D, 0.0D, 0.01);
6363- world.sendParticles(ParticleTypes.WHITE_SMOKE, player.getX(), player.getY(), player.getZ(), 15, 0.0D, 1.0D, 0.0D, 0.03);
6464- world.playSound(null, player.blockPosition(), SoundEvent.createVariableRangeEvent(ENDERMAN_TELEPORT.location()), SoundSource.PLAYERS, 0.4f, 1.0f);
6565-6666- // check if the player is currently flying
6767- boolean flying = player.getAbilities().flying;
6868-6969- // teleport!
7070- player.teleportTo(world, coords.x, coords.y, coords.z, Set.of(), player.getYRot(), player.getXRot(), false);
7171-7272- // Restore flying when teleporting trough dimensions
7373- if (flying) {
7474- player.getAbilities().flying = true;
7575- player.onUpdateAbilities();
7676- }
7777-7878- // teleportation sound after teleport
7979- world.playSound(null, player.blockPosition(), SoundEvent.createVariableRangeEvent(ENDERMAN_TELEPORT.location()), SoundSource.PLAYERS, 0.4f, 1.0f);
8080-8181- // delay visual effects so the player can see it when switching dimensions
8282- Timer timer = new Timer();
8383- timer.schedule(
8484- new TimerTask() {
8585- @Override
8686- public void run() {
8787- world.sendParticles(ParticleTypes.SNOWFLAKE, player.getX(), player.getY() , player.getZ(), 20, 0.0D, 1.0D, 0.0D, 0.01);
8888- world.sendParticles(ParticleTypes.WHITE_SMOKE, player.getX(), player.getY(), player.getZ(), 15, 0.0D, 0.0D, 0.0D, 0.03);
8989- }
9090- }, 100 // hopefully a good delay, ~ 2 ticks
9191- );
9292- }
9393-2626+ 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");
94279528 // checks a 7x7x7 location around the player in order to find a safe place to teleport them to.
9696- public Optional<BlockPos> getSafeBlockPos(BlockPos blockPos, ServerLevel world) {
2929+ public static Optional<BlockPos> getSafeBlockPos(BlockPos blockPos, ServerLevel world) {
9730 int row = 1;
9831 int rows = 3;
9932···137701387113972 // Gets the translated text for each player based on their language, this is fully server side and actually works (UNLIKE MOJANG'S TRANSLATED KEY'S WHICH ARE CLIENT SIDE) (I'm not mad, I swear!)
140140- public MutableComponent getTranslatedText(String key, ServerPlayer player, MutableComponent... args) {
7373+ public static MutableComponent getTranslatedText(String key, ServerPlayer player, MutableComponent... args) {
14174 //todo! maybe make this also loaded in memory?
14275 String language = player.clientInformation().language().toLowerCase();
14376 String regex = "%(\\d+)%";
···222155223156224157 // Gets the ids of all the worlds
225225- public List<String> getWorldIds() {
158158+ public static List<String> getWorldIds() {
226159 return StreamSupport.stream(teleportCommands.server.getAllLevels().spliterator(), false)
227160 .map(level -> level.dimension().location().toString())
228161 .toList();
···230163231164232165 // checks if a BlockPos is safe, used by the teleportSafetyChecker.
233233- private boolean isBlockPosSafe(BlockPos bottomPlayer, ServerLevel world) {
166166+ private static boolean isBlockPosSafe(BlockPos bottomPlayer, ServerLevel world) {
234167235168 // get the block below the player
236169 BlockPos belowPlayer = new BlockPos(bottomPlayer.getX(), bottomPlayer.getY() -1, bottomPlayer.getZ()); // below the player