diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/TeleportCommands.java b/common/src/main/java/dev/mrsnowy/teleport_commands/TeleportCommands.java index c2d215e..1cfab04 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/TeleportCommands.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/TeleportCommands.java @@ -1,43 +1,47 @@ package dev.mrsnowy.teleport_commands; -import com.google.gson.*; import com.mojang.brigadier.CommandDispatcher; import dev.mrsnowy.teleport_commands.storage.StorageManager; import dev.mrsnowy.teleport_commands.commands.*; import dev.mrsnowy.teleport_commands.storage.DeathLocationStorage; import dev.mrsnowy.teleport_commands.storage.ConfigManager; +import dev.mrsnowy.teleport_commands.utils.tools; import net.minecraft.commands.CommandSourceStack; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.level.storage.LevelResource; import net.minecraft.core.BlockPos; -import java.io.*; + import java.nio.file.Path; import java.nio.file.Paths; public class TeleportCommands { - public static String MOD_LOADER; - public static Path SAVE_DIR; - public static Path CONFIG_DIR; - public static MinecraftServer SERVER; - + public String modLoader; + public Path saveDir; + public Path configDir; + public MinecraftServer server; + public StorageManager storageManager; + public ConfigManager config; + public DeathLocationStorage deathLocationStorage; + public tools tools; // Gets ran when the server starts, initializes the mod :3 - public static void initializeMod(MinecraftServer server) { - Constants.LOGGER.info("Initializing Teleport Commands (V{})! Hello {}!", Constants.VERSION, MOD_LOADER); + public void initializeMod(MinecraftServer server) { + Constants.LOGGER.info("Initializing Teleport Commands (V{})! Hello {}!", Constants.VERSION, modLoader); - SAVE_DIR = Path.of(String.valueOf(server.getWorldPath(LevelResource.ROOT))); - CONFIG_DIR = Paths.get(System.getProperty("user.dir")).resolve("config"); // Construct the game directory path - SERVER = server; + saveDir = Path.of(String.valueOf(server.getWorldPath(LevelResource.ROOT))); + configDir = Paths.get(System.getProperty("user.dir")).resolve("config"); + this.server = server; + this.tools = new tools(); + this.storageManager = new StorageManager(); + this.config = new ConfigManager(); - StorageManager.StorageInit(); // Initialize the storage file - ConfigManager.ConfigInit(); - DeathLocationStorage.clearDeathLocations(); // Clear data of death locations. + deathLocationStorage = new DeathLocationStorage(); } // initialize commands, also allows me to easily disable any when there is a config - public static void registerCommands(CommandDispatcher dispatcher) { - back.register(dispatcher); + public void registerCommands(CommandDispatcher dispatcher) { + new back(dispatcher, this); home.register(dispatcher); tpa.register(dispatcher); warp.register(dispatcher); @@ -46,11 +50,11 @@ public class TeleportCommands { } // Runs when the playerDeath mixin calls it, updates the /back command position - public static void onPlayerDeath(ServerPlayer player) { + public void onPlayerDeath(ServerPlayer player) { BlockPos pos = new BlockPos(player.getBlockX(), player.getBlockY(), player.getBlockZ()); String world = player.serverLevel().dimension().location().toString(); String uuid = player.getStringUUID(); - DeathLocationStorage.setDeathLocation(uuid, pos, world); + this.deathLocationStorage.setDeathLocation(uuid, pos, world); } } \ No newline at end of file diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/back.java b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/back.java index 74c3fae..731fb76 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/back.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/back.java @@ -6,6 +6,7 @@ import dev.mrsnowy.teleport_commands.Constants; import java.util.*; +import dev.mrsnowy.teleport_commands.TeleportCommands; import dev.mrsnowy.teleport_commands.storage.DeathLocationStorage; import dev.mrsnowy.teleport_commands.common.DeathLocation; import dev.mrsnowy.teleport_commands.utils.tools; @@ -23,8 +24,10 @@ import static dev.mrsnowy.teleport_commands.utils.tools.*; import static net.minecraft.commands.Commands.argument; public class back { + TeleportCommands teleportCommands; - public static void register(CommandDispatcher commandDispatcher) { + public back(CommandDispatcher commandDispatcher, TeleportCommands teleportCommands) { + this.teleportCommands = teleportCommands; commandDispatcher.register(Commands.literal("back") .requires(source -> source.getPlayer() != null) @@ -64,8 +67,8 @@ public class back { // ----- // Gets the DeathLocation of the player and teleports the player to it - private static void ToDeathLocation(ServerPlayer player, boolean safetyDisabled) throws Exception { - DeathLocation deathLocation = DeathLocationStorage + private void ToDeathLocation(ServerPlayer player, boolean safetyDisabled) throws Exception { + DeathLocation deathLocation = teleportCommands.deathLocationStorage .getDeathLocation(player.getStringUUID()) .orElse(null); @@ -132,7 +135,7 @@ public class back { Vec3 teleportPos = new Vec3(teleportBlockPos.getX() + 0.5, teleportBlockPos.getY(), teleportBlockPos.getZ() + 0.5); player.displayClientMessage(getTranslatedText("commands.teleport_commands.back.go", player), true); - tools.Teleporter(player, deathLocationWorld, teleportPos); + teleportCommands.tools.Teleporter(player, deathLocationWorld, teleportPos); } } } diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/worldspawn.java b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/worldspawn.java index 7e5577a..19cb539 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/commands/worldspawn.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/commands/worldspawn.java @@ -62,7 +62,7 @@ public class worldspawn { private static void toWorldSpawn(ServerPlayer player, boolean safetyDisabled) throws NullPointerException { // todo! make the dimension customizable - ServerLevel world = TeleportCommands.SERVER.getLevel(OVERWORLD); + ServerLevel world = TeleportCommands.server.getLevel(OVERWORLD); BlockPos worldSpawn = Objects.requireNonNull(world,"Overworld cannot be null!").getSharedSpawnPos(); if (!safetyDisabled) { diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/common/DeathLocation.java b/common/src/main/java/dev/mrsnowy/teleport_commands/common/DeathLocation.java index 9f45e6a..84657e8 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/common/DeathLocation.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/common/DeathLocation.java @@ -31,7 +31,7 @@ public class DeathLocation { // function to quickly filter the worlds and get the ServerLevel for the string public Optional getWorld() { - return StreamSupport.stream( TeleportCommands.SERVER.getAllLevels().spliterator(), false ) // woa, this looks silly + return StreamSupport.stream( TeleportCommands.server.getAllLevels().spliterator(), false ) // woa, this looks silly .filter(level -> Objects.equals( level.dimension().location().toString(), this.world )) .findFirst(); } diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/common/NamedLocation.java b/common/src/main/java/dev/mrsnowy/teleport_commands/common/NamedLocation.java index 9af35ab..b5911c2 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/common/NamedLocation.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/common/NamedLocation.java @@ -53,7 +53,7 @@ public class NamedLocation { // function to quickly filter the worlds and get the ServerLevel for the string public Optional getWorld() { - return StreamSupport.stream( TeleportCommands.SERVER.getAllLevels().spliterator(), false ) // woa, this looks silly + return StreamSupport.stream( TeleportCommands.server.getAllLevels().spliterator(), false ) // woa, this looks silly .filter(level -> Objects.equals( level.dimension().location().toString(), this.world )) .findFirst(); } diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/storage/ConfigManager.java b/common/src/main/java/dev/mrsnowy/teleport_commands/storage/ConfigManager.java index 5de172f..718a1dd 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/storage/ConfigManager.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/storage/ConfigManager.java @@ -10,13 +10,13 @@ import java.nio.file.Path; import java.nio.file.StandardOpenOption; public class ConfigManager { - public static Path CONFIG_FILE; - public static ConfigClass CONFIG; - private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); - private static final int defaultVersion = new ConfigClass().getVersion(); + public Path CONFIG_FILE; + public ConfigClass CONFIG; + private final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + private final int defaultVersion = new ConfigClass().getVersion(); - public static void ConfigInit() { - CONFIG_FILE = TeleportCommands.CONFIG_DIR.resolve("teleport_commands.json"); + public ConfigManager() { + CONFIG_FILE = TeleportCommands.TeleportCommands.configDir.resolve("teleport_commands.json"); try { ConfigLoader(); @@ -28,9 +28,9 @@ public class ConfigManager { } } - public static void ConfigLoader() throws Exception { + public void ConfigLoader() throws Exception { if (!CONFIG_FILE.toFile().exists() || CONFIG_FILE.toFile().length() == 0) { - Files.createDirectories(TeleportCommands.CONFIG_DIR); + Files.createDirectories(TeleportCommands.configDir); Constants.LOGGER.warn("Config file was not found or was empty! Initializing config"); CONFIG = new ConfigClass(); @@ -53,7 +53,7 @@ public class ConfigManager { } /// This function checks what version the config file is and migrates it to the current version of the mod. - public static void ConfigMigrator() throws Exception { + public void ConfigMigrator() throws Exception { FileReader reader = new FileReader(CONFIG_FILE.toFile()); JsonObject jsonObject = GSON.fromJson(reader, JsonObject.class); @@ -76,14 +76,14 @@ public class ConfigManager { } } - public static void ConfigSaver() throws Exception { + public void ConfigSaver() throws Exception { // todo! maybe throttle saves? byte[] json = GSON.toJson( ConfigManager.CONFIG ).getBytes(); Files.write(CONFIG_FILE, json, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); } - public static class ConfigClass { + public class ConfigClass { private final int version = 0; public Teleporting teleporting = new Teleporting(); public Back back = new Back(); @@ -96,7 +96,7 @@ public class ConfigManager { return version; } - public static final class Teleporting { + public final class Teleporting { private int delay = 5; private boolean whileMoving = true; private boolean whileFighting = false; diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/storage/DeathLocationStorage.java b/common/src/main/java/dev/mrsnowy/teleport_commands/storage/DeathLocationStorage.java index 774a6c5..22f70a7 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/storage/DeathLocationStorage.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/storage/DeathLocationStorage.java @@ -7,15 +7,15 @@ import java.util.HashMap; import java.util.Optional; public class DeathLocationStorage { - private static final HashMap deathLocations = new HashMap<>(); + private final HashMap deathLocations = new HashMap<>(); // filters the deathLocationList and finds the one with the matching player uuid (if there is one) - public static Optional getDeathLocation(String uuid) { + public Optional getDeathLocation(String uuid) { return Optional.ofNullable(deathLocations.get(uuid)); } // updates the deathLocation of a player, if there is no existing entry it will create a new deathLocation. - public static void setDeathLocation(String uuid, BlockPos pos, String world) { + public void setDeathLocation(String uuid, BlockPos pos, String world) { if (deathLocations.containsKey(uuid)) { // modify existing deathLocation @@ -29,7 +29,7 @@ public class DeathLocationStorage { } } - public static void clearDeathLocations() { + public void clearDeathLocations() { deathLocations.clear(); } } \ No newline at end of file diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/storage/StorageManager.java b/common/src/main/java/dev/mrsnowy/teleport_commands/storage/StorageManager.java index 5689348..e77bf88 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/storage/StorageManager.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/storage/StorageManager.java @@ -18,15 +18,15 @@ import java.util.Optional; import static java.util.Collections.unmodifiableList; public class StorageManager { - public static Path STORAGE_FOLDER; - public static Path STORAGE_FILE; - public static StorageClass STORAGE; - private static final Gson GSON = new GsonBuilder().create(); - private static final int defaultVersion = new StorageClass().getVersion(); + public Path STORAGE_FOLDER; + public Path STORAGE_FILE; + public StorageClass STORAGE; + private final Gson GSON = new GsonBuilder().create(); + private final int defaultVersion = new StorageClass().getVersion(); /// Initializes the StorageManager class and loads the storage from the filesystem. - public static void StorageInit() { - STORAGE_FOLDER = TeleportCommands.SAVE_DIR.resolve("TeleportCommands/"); + public StorageManager() { + STORAGE_FOLDER = TeleportCommands.TeleportCommands.saveDir.resolve("TeleportCommands/"); STORAGE_FILE = STORAGE_FOLDER.resolve("storage.json"); try { @@ -40,7 +40,7 @@ public class StorageManager { } /// Loads the storage from the filesystem - public static void StorageLoader() throws Exception { + public void StorageLoader() throws Exception { if (!STORAGE_FILE.toFile().exists() || STORAGE_FILE.toFile().length() == 0) { Constants.LOGGER.warn("Storage file was not found or was empty! Initializing storage"); @@ -68,7 +68,7 @@ public class StorageManager { } /// This function checks what version the storage file is and migrates it to the current version of the mod. - public static void StorageMigrator() throws Exception { + public void StorageMigrator() throws Exception { FileReader reader = new FileReader(STORAGE_FILE.toFile()); JsonObject jsonObject = GSON.fromJson(reader, JsonObject.class); @@ -120,15 +120,15 @@ public class StorageManager { } /// Saves the storage to the filesystem - public static void StorageSaver() throws Exception { + public void StorageSaver() throws Exception { // todo! maybe throttle saves? - byte[] json = GSON.toJson( StorageManager.STORAGE ).getBytes(); + byte[] json = GSON.toJson( this.STORAGE ).getBytes(); Files.write(STORAGE_FILE, json, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); } - public static class StorageClass { + public class StorageClass { private final int version = 1; private final ArrayList Warps = new ArrayList<>(); private final ArrayList Players = new ArrayList<>(); @@ -144,7 +144,7 @@ public class StorageManager { List homes = player.getHomes(); // Delete any homes with an invalid world_id (if enabled in config) - if (ConfigManager.CONFIG.home.isDeleteInvalid()) { + if (TeleportCommands.TeleportCommands.config.CONFIG.home.isDeleteInvalid()) { homes.removeIf(home -> home.getWorld().isEmpty()); } @@ -155,7 +155,7 @@ public class StorageManager { } // Delete any warps with an invalid world_id (if enabled in config) - if (ConfigManager.CONFIG.warp.isDeleteInvalid()) { + if (TeleportCommands.TeleportCommands.config.CONFIG.warp.isDeleteInvalid()) { Warps.removeIf(warp -> warp.getWorld().isEmpty()); } diff --git a/common/src/main/java/dev/mrsnowy/teleport_commands/utils/tools.java b/common/src/main/java/dev/mrsnowy/teleport_commands/utils/tools.java index 38854c0..380f0fb 100644 --- a/common/src/main/java/dev/mrsnowy/teleport_commands/utils/tools.java +++ b/common/src/main/java/dev/mrsnowy/teleport_commands/utils/tools.java @@ -11,6 +11,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.StreamSupport; +import dev.mrsnowy.teleport_commands.storage.ConfigManager; import net.minecraft.core.BlockPos; import net.minecraft.core.particles.ParticleTypes; import net.minecraft.network.chat.Component; @@ -24,11 +25,39 @@ import net.minecraft.world.phys.Vec3; import static dev.mrsnowy.teleport_commands.Constants.MOD_ID; import static net.minecraft.sounds.SoundEvents.ENDERMAN_TELEPORT; + public class tools { + private final TeleportCommands teleportCommands; + + private final Set 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"); + + private final Map playerData = new HashMap<>(); + + private class PlayerData { + long lastTeleportTime = 0; + long lastHitTime = 0; + Vec3 lastPosition = Vec3.ZERO; + } + + public tools(TeleportCommands teleportCommands) { + this.teleportCommands = teleportCommands; + } + + /// Teleport the player :P + public void Teleporter(ServerPlayer player, ServerLevel world, Vec3 coords) { + // Check if user is allowed to teleport by config settings + + int delay = teleportCommands.config.CONFIG.teleporting.getDelay(); + + // save when they last teleported and check delay + + // save pos and check if they have moved. + + // check if they got hit? whileFighting + + // save when they last got hit and if it exceeds fightCooldown - private static final Set 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"); - public static void Teleporter(ServerPlayer player, ServerLevel world, Vec3 coords) { // teleportation effects & sounds before teleporting world.sendParticles(ParticleTypes.SNOWFLAKE, player.getX(), player.getY() + 1, player.getZ(), 20, 0.0D, 0.0D, 0.0D, 0.01); world.sendParticles(ParticleTypes.WHITE_SMOKE, player.getX(), player.getY(), player.getZ(), 15, 0.0D, 1.0D, 0.0D, 0.03); @@ -64,7 +93,7 @@ public class tools { // checks a 7x7x7 location around the player in order to find a safe place to teleport them to. - public static Optional getSafeBlockPos(BlockPos blockPos, ServerLevel world) { + public Optional getSafeBlockPos(BlockPos blockPos, ServerLevel world) { int row = 1; int rows = 3; @@ -108,7 +137,7 @@ public class tools { // 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!) - public static MutableComponent getTranslatedText(String key, ServerPlayer player, MutableComponent... args) { + public MutableComponent getTranslatedText(String key, ServerPlayer player, MutableComponent... args) { //todo! maybe make this also loaded in memory? String language = player.clientInformation().language().toLowerCase(); String regex = "%(\\d+)%"; @@ -193,15 +222,15 @@ public class tools { // Gets the ids of all the worlds - public static List getWorldIds() { - return StreamSupport.stream(TeleportCommands.SERVER.getAllLevels().spliterator(), false) + public List getWorldIds() { + return StreamSupport.stream(teleportCommands.server.getAllLevels().spliterator(), false) .map(level -> level.dimension().location().toString()) .toList(); } // checks if a BlockPos is safe, used by the teleportSafetyChecker. - private static boolean isBlockPosSafe(BlockPos bottomPlayer, ServerLevel world) { + private boolean isBlockPosSafe(BlockPos bottomPlayer, ServerLevel world) { // get the block below the player BlockPos belowPlayer = new BlockPos(bottomPlayer.getX(), bottomPlayer.getY() -1, bottomPlayer.getZ()); // below the player diff --git a/fabric/src/main/java/dev/mrsnowy/teleport_commands/fabricInit.java b/fabric/src/main/java/dev/mrsnowy/teleport_commands/fabricInit.java index e75952b..0ac7662 100644 --- a/fabric/src/main/java/dev/mrsnowy/teleport_commands/fabricInit.java +++ b/fabric/src/main/java/dev/mrsnowy/teleport_commands/fabricInit.java @@ -10,6 +10,6 @@ public class fabricInit implements ModInitializer { // However, some things (like resources) may still be uninitialized. // Proceed with mild caution. - TeleportCommands.MOD_LOADER = "Fabric"; + TeleportCommands.modLoader = "Fabric"; } } \ No newline at end of file diff --git a/neoforge/src/main/java/dev/mrsnowy/teleport_commands/neoforgeInit.java b/neoforge/src/main/java/dev/mrsnowy/teleport_commands/neoforgeInit.java index 830cfac..1634906 100644 --- a/neoforge/src/main/java/dev/mrsnowy/teleport_commands/neoforgeInit.java +++ b/neoforge/src/main/java/dev/mrsnowy/teleport_commands/neoforgeInit.java @@ -11,7 +11,7 @@ public class neoforgeInit { // to load your mod. You can access NeoForge and Common code in this // project. - TeleportCommands.MOD_LOADER = "NeoForge"; + TeleportCommands.modLoader = "NeoForge"; } }