A Minecraft server-side mod that adds various teleportation related commands
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

bump, wip

MrSnowy 004481b5 c5e3f4a3

+145 -112
+7 -6
common/src/main/java/dev/mrsnowy/teleport_commands/TeleportCommands.java
··· 4 4 import dev.mrsnowy.teleport_commands.storage.StorageManager; 5 5 import dev.mrsnowy.teleport_commands.commands.*; 6 6 import dev.mrsnowy.teleport_commands.storage.DeathLocationStorage; 7 - import dev.mrsnowy.teleport_commands.storage.ConfigManager; 7 + import dev.mrsnowy.teleport_commands.storage.configManager; 8 + import dev.mrsnowy.teleport_commands.utils.teleporter; 8 9 import dev.mrsnowy.teleport_commands.utils.tools; 9 10 import net.minecraft.commands.CommandSourceStack; 10 11 import net.minecraft.server.MinecraftServer; ··· 21 22 public Path configDir; 22 23 public MinecraftServer server; 23 24 public StorageManager storageManager; 24 - public ConfigManager config; 25 + public configManager config; 25 26 public DeathLocationStorage deathLocationStorage; 26 - public tools tools; 27 + public teleporter teleporter; 27 28 28 29 // Gets ran when the server starts, initializes the mod :3 29 30 public void initializeMod(MinecraftServer server) { ··· 32 33 saveDir = Path.of(String.valueOf(server.getWorldPath(LevelResource.ROOT))); 33 34 configDir = Paths.get(System.getProperty("user.dir")).resolve("config"); 34 35 this.server = server; 35 - this.tools = new tools(); 36 - this.storageManager = new StorageManager(); 37 - this.config = new ConfigManager(); 38 36 37 + storageManager = new StorageManager(this); 38 + config = new configManager(this); 39 39 deathLocationStorage = new DeathLocationStorage(); 40 + teleporter = new teleporter(this); 40 41 } 41 42 42 43 // initialize commands, also allows me to easily disable any when there is a config
+2 -4
common/src/main/java/dev/mrsnowy/teleport_commands/commands/main.java
··· 6 6 import com.mojang.brigadier.suggestion.SuggestionProvider; 7 7 import dev.mrsnowy.teleport_commands.Constants; 8 8 import dev.mrsnowy.teleport_commands.TeleportCommands; 9 - import dev.mrsnowy.teleport_commands.storage.ConfigManager; 9 + import dev.mrsnowy.teleport_commands.storage.configManager; 10 10 import net.minecraft.ChatFormatting; 11 11 import net.minecraft.commands.CommandSourceStack; 12 12 import net.minecraft.commands.Commands; ··· 16 16 import net.minecraft.server.level.ServerPlayer; 17 17 18 18 import java.util.Arrays; 19 - 20 - import static dev.mrsnowy.teleport_commands.utils.tools.*; 21 19 22 20 public class main { 23 21 ··· 45 43 .then(Commands.literal("reloadConfig") 46 44 .executes(context -> { 47 45 try { 48 - ConfigManager.ConfigLoader(); 46 + configManager.configLoader(); 49 47 } catch (Exception e) { 50 48 Constants.LOGGER.error("Failed to reload config!", e); 51 49 throw new SimpleCommandExceptionType(Component.literal(e.toString())).create();
+25 -20
common/src/main/java/dev/mrsnowy/teleport_commands/storage/ConfigManager.java common/src/main/java/dev/mrsnowy/teleport_commands/storage/configManager.java
··· 9 9 import java.nio.file.Path; 10 10 import java.nio.file.StandardOpenOption; 11 11 12 - public class ConfigManager { 12 + public class configManager { 13 13 public Path CONFIG_FILE; 14 14 public ConfigClass CONFIG; 15 + 15 16 private final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); 16 17 private final int defaultVersion = new ConfigClass().getVersion(); 18 + private final TeleportCommands teleportCommands; 17 19 18 - public ConfigManager() { 19 - CONFIG_FILE = TeleportCommands.TeleportCommands.configDir.resolve("teleport_commands.json"); 20 + public configManager(TeleportCommands teleportCommands) { 21 + this.teleportCommands = teleportCommands; 22 + CONFIG_FILE = teleportCommands.configDir.resolve("teleport_commands.json"); 20 23 21 24 try { 22 - ConfigLoader(); 25 + configLoader(); 23 26 24 27 } catch (Exception e) { 25 28 // crashing is probably better here, otherwise the whole mod will be broken ··· 28 31 } 29 32 } 30 33 31 - public void ConfigLoader() throws Exception { 34 + /// This function loads the config from disk 35 + public void configLoader() throws Exception { 32 36 if (!CONFIG_FILE.toFile().exists() || CONFIG_FILE.toFile().length() == 0) { 33 - Files.createDirectories(TeleportCommands.configDir); 37 + Files.createDirectories(teleportCommands.configDir); 34 38 35 39 Constants.LOGGER.warn("Config file was not found or was empty! Initializing config"); 36 40 CONFIG = new ConfigClass(); 37 - ConfigSaver(); 41 + configSaver(); 38 42 Constants.LOGGER.info("Config created successfully!"); 39 43 } 40 44 41 - ConfigMigrator(); 45 + configMigrator(); 42 46 43 47 FileReader reader = new FileReader(CONFIG_FILE.toFile()); 44 48 CONFIG = GSON.fromJson(reader, ConfigClass.class); 45 49 if (CONFIG == null) { 46 50 Constants.LOGGER.warn("Config file was empty! Loading defaults..."); 47 51 CONFIG = new ConfigClass(); 48 - ConfigSaver(); 52 + configSaver(); 49 53 } 50 54 51 - ConfigSaver(); // Save it so any missing values get added to the file. 55 + configSaver(); // Save it so any missing values get added to the file. 52 56 Constants.LOGGER.info("Config loaded successfully!"); 53 57 } 54 58 55 59 /// This function checks what version the config file is and migrates it to the current version of the mod. 56 - public void ConfigMigrator() throws Exception { 60 + public void configMigrator() throws Exception { 57 61 FileReader reader = new FileReader(CONFIG_FILE.toFile()); 58 62 JsonObject jsonObject = GSON.fromJson(reader, JsonObject.class); 59 63 ··· 76 80 } 77 81 } 78 82 79 - public void ConfigSaver() throws Exception { 83 + /// Saves the config to disk 84 + public void configSaver() throws Exception { 80 85 // todo! maybe throttle saves? 81 - byte[] json = GSON.toJson( ConfigManager.CONFIG ).getBytes(); 86 + byte[] json = GSON.toJson(CONFIG).getBytes(); 82 87 83 88 Files.write(CONFIG_FILE, json, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); 84 89 } 85 90 86 - public class ConfigClass { 91 + public static class ConfigClass { 87 92 private final int version = 0; 88 93 public Teleporting teleporting = new Teleporting(); 89 94 public Back back = new Back(); ··· 96 101 return version; 97 102 } 98 103 99 - public final class Teleporting { 104 + public static final class Teleporting { 100 105 private int delay = 5; 101 106 private boolean whileMoving = true; 102 107 private boolean whileFighting = false; ··· 135 140 } 136 141 } 137 142 138 - public final class Back { 143 + public static final class Back { 139 144 private boolean enabled = true; 140 145 private boolean deleteAfterTeleport = false; 141 146 ··· 156 161 } 157 162 } 158 163 159 - public final class Home { 164 + public static final class Home { 160 165 private boolean enabled = true; 161 166 private int playerMaximum = 20; 162 167 private boolean deleteInvalid = false; ··· 186 191 } 187 192 } 188 193 189 - public final class Tpa { 194 + public static final class Tpa { 190 195 private boolean enabled = true; 191 196 192 197 public boolean isEnabled() { ··· 198 203 } 199 204 } 200 205 201 - public final class Warp { 206 + public static final class Warp { 202 207 private boolean enabled = true; 203 208 private boolean deleteInvalid = false; 204 209 ··· 219 224 } 220 225 } 221 226 222 - public final class WorldSpawn { 227 + public static final class WorldSpawn { 223 228 private boolean enabled = true; 224 229 private String world_id = "minecraft:overworld"; 225 230
+3 -3
common/src/main/java/dev/mrsnowy/teleport_commands/storage/DeathLocationStorage.java
··· 29 29 } 30 30 } 31 31 32 - public void clearDeathLocations() { 33 - deathLocations.clear(); 34 - } 32 + // public void clearDeathLocations() { 33 + // deathLocations.clear(); 34 + // } 35 35 }
+7 -4
common/src/main/java/dev/mrsnowy/teleport_commands/storage/StorageManager.java
··· 21 21 public Path STORAGE_FOLDER; 22 22 public Path STORAGE_FILE; 23 23 public StorageClass STORAGE; 24 + 24 25 private final Gson GSON = new GsonBuilder().create(); 25 26 private final int defaultVersion = new StorageClass().getVersion(); 27 + private final TeleportCommands teleportCommands; 26 28 27 29 /// Initializes the StorageManager class and loads the storage from the filesystem. 28 - public StorageManager() { 29 - STORAGE_FOLDER = TeleportCommands.TeleportCommands.saveDir.resolve("TeleportCommands/"); 30 + public StorageManager(TeleportCommands teleportCommands) { 31 + this.teleportCommands = teleportCommands; 32 + STORAGE_FOLDER = teleportCommands.saveDir.resolve("TeleportCommands/"); 30 33 STORAGE_FILE = STORAGE_FOLDER.resolve("storage.json"); 31 34 32 35 try { ··· 144 147 List<NamedLocation> homes = player.getHomes(); 145 148 146 149 // Delete any homes with an invalid world_id (if enabled in config) 147 - if (TeleportCommands.TeleportCommands.config.CONFIG.home.isDeleteInvalid()) { 150 + if (teleportCommands.config.CONFIG.home.isDeleteInvalid()) { 148 151 homes.removeIf(home -> home.getWorld().isEmpty()); 149 152 } 150 153 ··· 155 158 } 156 159 157 160 // Delete any warps with an invalid world_id (if enabled in config) 158 - if (TeleportCommands.TeleportCommands.config.CONFIG.warp.isDeleteInvalid()) { 161 + if (teleportCommands.config.CONFIG.warp.isDeleteInvalid()) { 159 162 Warps.removeIf(warp -> warp.getWorld().isEmpty()); 160 163 } 161 164
+8 -2
common/src/main/java/dev/mrsnowy/teleport_commands/suggestions/HomeSuggestionProvider.java
··· 9 9 import java.util.Optional; 10 10 import java.util.concurrent.CompletableFuture; 11 11 12 + import dev.mrsnowy.teleport_commands.TeleportCommands; 12 13 import dev.mrsnowy.teleport_commands.common.NamedLocation; 13 14 import dev.mrsnowy.teleport_commands.common.Player; 14 15 import net.minecraft.commands.CommandSourceStack; 15 16 import net.minecraft.server.level.ServerPlayer; 16 17 17 - import static dev.mrsnowy.teleport_commands.storage.StorageManager.STORAGE; 18 18 19 19 public class HomeSuggestionProvider implements SuggestionProvider<CommandSourceStack> { 20 + private final TeleportCommands teleportCommands; 21 + 22 + public HomeSuggestionProvider(TeleportCommands teleportCommands) { 23 + this.teleportCommands = teleportCommands; 24 + } 25 + 20 26 @Override 21 27 public CompletableFuture<Suggestions> getSuggestions(CommandContext<CommandSourceStack> context, SuggestionsBuilder builder) { 22 28 try { 23 29 ServerPlayer player = context.getSource().getPlayerOrException(); 24 - Optional<Player> optionalPlayerStorage = STORAGE.getPlayer(player.getStringUUID()); 30 + Optional<Player> optionalPlayerStorage = teleportCommands.storageManager.STORAGE.getPlayer(player.getStringUUID()); 25 31 26 32 if (optionalPlayerStorage.isPresent()) { 27 33 Player playerStorage = optionalPlayerStorage.get();
+8 -1
common/src/main/java/dev/mrsnowy/teleport_commands/suggestions/WarpSuggestionProvider.java
··· 6 6 import com.mojang.brigadier.suggestion.SuggestionsBuilder; 7 7 8 8 import dev.mrsnowy.teleport_commands.Constants; 9 + import dev.mrsnowy.teleport_commands.TeleportCommands; 9 10 import dev.mrsnowy.teleport_commands.storage.StorageManager; 10 11 import dev.mrsnowy.teleport_commands.common.NamedLocation; 11 12 ··· 16 17 17 18 18 19 public class WarpSuggestionProvider implements SuggestionProvider<CommandSourceStack> { 20 + private final TeleportCommands teleportCommands; 21 + 22 + public WarpSuggestionProvider(TeleportCommands teleportCommands) { 23 + this.teleportCommands = teleportCommands; 24 + } 25 + 19 26 @Override 20 27 public CompletableFuture<Suggestions> getSuggestions(CommandContext<CommandSourceStack> context, SuggestionsBuilder builder) { 21 28 try { 22 - List<NamedLocation> WarpStorage = StorageManager.STORAGE.getWarps(); 29 + List<NamedLocation> WarpStorage = teleportCommands.storageManager.STORAGE.getWarps(); 23 30 24 31 for (NamedLocation currentWarp : WarpStorage) { 25 32 builder.suggest(currentWarp.getName());
+80
common/src/main/java/dev/mrsnowy/teleport_commands/utils/teleporter.java
··· 1 + package dev.mrsnowy.teleport_commands.utils; 2 + 3 + import dev.mrsnowy.teleport_commands.TeleportCommands; 4 + import net.minecraft.core.particles.ParticleTypes; 5 + import net.minecraft.server.level.ServerLevel; 6 + import net.minecraft.server.level.ServerPlayer; 7 + import net.minecraft.sounds.SoundEvent; 8 + import net.minecraft.sounds.SoundSource; 9 + import net.minecraft.world.phys.Vec3; 10 + 11 + import java.util.*; 12 + 13 + import static net.minecraft.sounds.SoundEvents.ENDERMAN_TELEPORT; 14 + 15 + public class teleporter { 16 + private final TeleportCommands teleportCommands; 17 + private final Map<UUID, PlayerData> playerData = new HashMap<>(); 18 + 19 + private static class PlayerData { 20 + long lastTeleportTime = 0; 21 + long lastHitTime = 0; 22 + Vec3 lastPosition = Vec3.ZERO; 23 + } 24 + 25 + public teleporter(TeleportCommands teleportCommands) { 26 + this.teleportCommands = teleportCommands; 27 + } 28 + 29 + /// Teleport the player :P 30 + public void teleport(ServerPlayer player, ServerLevel world, Vec3 coords) { 31 + // Check if user is allowed to teleport by config settings 32 + 33 + int delay = teleportCommands.config.CONFIG.teleporting.getDelay(); 34 + UUID playerUUID = player.getUUID(); 35 + // save when they last teleported and check delay 36 + if (playerData.containsKey(playerUUID)) { 37 + PlayerData playerdata = playerData.get(playerUUID); 38 + } 39 + 40 + // save pos and check if they have moved. 41 + 42 + // check if they got hit? whileFighting 43 + 44 + // save when they last got hit and if it exceeds fightCooldown 45 + 46 + 47 + // teleportation effects & sounds before teleporting 48 + world.sendParticles(ParticleTypes.SNOWFLAKE, player.getX(), player.getY() + 1, player.getZ(), 20, 0.0D, 0.0D, 0.0D, 0.01); 49 + world.sendParticles(ParticleTypes.WHITE_SMOKE, player.getX(), player.getY(), player.getZ(), 15, 0.0D, 1.0D, 0.0D, 0.03); 50 + world.playSound(null, player.blockPosition(), SoundEvent.createVariableRangeEvent(ENDERMAN_TELEPORT.location()), SoundSource.PLAYERS, 0.4f, 1.0f); 51 + 52 + // check if the player is currently flying 53 + boolean flying = player.getAbilities().flying; 54 + 55 + // teleport! 56 + player.teleportTo(world, coords.x, coords.y, coords.z, Set.of(), player.getYRot(), player.getXRot(), false); 57 + 58 + // Restore flying when teleporting trough dimensions 59 + if (flying) { 60 + player.getAbilities().flying = true; 61 + player.onUpdateAbilities(); 62 + } 63 + 64 + // teleportation sound after teleport 65 + world.playSound(null, player.blockPosition(), SoundEvent.createVariableRangeEvent(ENDERMAN_TELEPORT.location()), SoundSource.PLAYERS, 0.4f, 1.0f); 66 + 67 + // delay visual effects so the player can see it when switching dimensions 68 + Timer timer = new Timer(); 69 + timer.schedule( 70 + new TimerTask() { 71 + @Override 72 + public void run() { 73 + world.sendParticles(ParticleTypes.SNOWFLAKE, player.getX(), player.getY() , player.getZ(), 20, 0.0D, 1.0D, 0.0D, 0.01); 74 + world.sendParticles(ParticleTypes.WHITE_SMOKE, player.getX(), player.getY(), player.getZ(), 15, 0.0D, 0.0D, 0.0D, 0.03); 75 + } 76 + }, 100 // hopefully a good delay, ~ 2 ticks 77 + ); 78 + } 79 + 80 + }
+5 -72
common/src/main/java/dev/mrsnowy/teleport_commands/utils/tools.java
··· 11 11 import java.util.regex.Pattern; 12 12 import java.util.stream.StreamSupport; 13 13 14 - import dev.mrsnowy.teleport_commands.storage.ConfigManager; 15 14 import net.minecraft.core.BlockPos; 16 - import net.minecraft.core.particles.ParticleTypes; 17 15 import net.minecraft.network.chat.Component; 18 16 import net.minecraft.network.chat.MutableComponent; 19 17 import net.minecraft.server.level.ServerLevel; 20 18 import net.minecraft.server.level.ServerPlayer; 21 - import net.minecraft.sounds.SoundEvent; 22 - import net.minecraft.sounds.SoundSource; 23 - import net.minecraft.world.phys.Vec3; 24 19 25 20 import static dev.mrsnowy.teleport_commands.Constants.MOD_ID; 26 - import static net.minecraft.sounds.SoundEvents.ENDERMAN_TELEPORT; 27 21 28 22 29 23 public class tools { 30 - private final TeleportCommands teleportCommands; 31 24 32 - 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"); 33 - 34 - private final Map<UUID, PlayerData> playerData = new HashMap<>(); 35 25 36 - private class PlayerData { 37 - long lastTeleportTime = 0; 38 - long lastHitTime = 0; 39 - Vec3 lastPosition = Vec3.ZERO; 40 - } 41 - 42 - public tools(TeleportCommands teleportCommands) { 43 - this.teleportCommands = teleportCommands; 44 - } 45 - 46 - /// Teleport the player :P 47 - public void Teleporter(ServerPlayer player, ServerLevel world, Vec3 coords) { 48 - // Check if user is allowed to teleport by config settings 49 - 50 - int delay = teleportCommands.config.CONFIG.teleporting.getDelay(); 51 - 52 - // save when they last teleported and check delay 53 - 54 - // save pos and check if they have moved. 55 - 56 - // check if they got hit? whileFighting 57 - 58 - // save when they last got hit and if it exceeds fightCooldown 59 - 60 - 61 - // teleportation effects & sounds before teleporting 62 - world.sendParticles(ParticleTypes.SNOWFLAKE, player.getX(), player.getY() + 1, player.getZ(), 20, 0.0D, 0.0D, 0.0D, 0.01); 63 - world.sendParticles(ParticleTypes.WHITE_SMOKE, player.getX(), player.getY(), player.getZ(), 15, 0.0D, 1.0D, 0.0D, 0.03); 64 - world.playSound(null, player.blockPosition(), SoundEvent.createVariableRangeEvent(ENDERMAN_TELEPORT.location()), SoundSource.PLAYERS, 0.4f, 1.0f); 65 - 66 - // check if the player is currently flying 67 - boolean flying = player.getAbilities().flying; 68 - 69 - // teleport! 70 - player.teleportTo(world, coords.x, coords.y, coords.z, Set.of(), player.getYRot(), player.getXRot(), false); 71 - 72 - // Restore flying when teleporting trough dimensions 73 - if (flying) { 74 - player.getAbilities().flying = true; 75 - player.onUpdateAbilities(); 76 - } 77 - 78 - // teleportation sound after teleport 79 - world.playSound(null, player.blockPosition(), SoundEvent.createVariableRangeEvent(ENDERMAN_TELEPORT.location()), SoundSource.PLAYERS, 0.4f, 1.0f); 80 - 81 - // delay visual effects so the player can see it when switching dimensions 82 - Timer timer = new Timer(); 83 - timer.schedule( 84 - new TimerTask() { 85 - @Override 86 - public void run() { 87 - world.sendParticles(ParticleTypes.SNOWFLAKE, player.getX(), player.getY() , player.getZ(), 20, 0.0D, 1.0D, 0.0D, 0.01); 88 - world.sendParticles(ParticleTypes.WHITE_SMOKE, player.getX(), player.getY(), player.getZ(), 15, 0.0D, 0.0D, 0.0D, 0.03); 89 - } 90 - }, 100 // hopefully a good delay, ~ 2 ticks 91 - ); 92 - } 93 - 26 + 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"); 94 27 95 28 // checks a 7x7x7 location around the player in order to find a safe place to teleport them to. 96 - public Optional<BlockPos> getSafeBlockPos(BlockPos blockPos, ServerLevel world) { 29 + public static Optional<BlockPos> getSafeBlockPos(BlockPos blockPos, ServerLevel world) { 97 30 int row = 1; 98 31 int rows = 3; 99 32 ··· 137 70 138 71 139 72 // 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!) 140 - public MutableComponent getTranslatedText(String key, ServerPlayer player, MutableComponent... args) { 73 + public static MutableComponent getTranslatedText(String key, ServerPlayer player, MutableComponent... args) { 141 74 //todo! maybe make this also loaded in memory? 142 75 String language = player.clientInformation().language().toLowerCase(); 143 76 String regex = "%(\\d+)%"; ··· 222 155 223 156 224 157 // Gets the ids of all the worlds 225 - public List<String> getWorldIds() { 158 + public static List<String> getWorldIds() { 226 159 return StreamSupport.stream(teleportCommands.server.getAllLevels().spliterator(), false) 227 160 .map(level -> level.dimension().location().toString()) 228 161 .toList(); ··· 230 163 231 164 232 165 // checks if a BlockPos is safe, used by the teleportSafetyChecker. 233 - private boolean isBlockPosSafe(BlockPos bottomPlayer, ServerLevel world) { 166 + private static boolean isBlockPosSafe(BlockPos bottomPlayer, ServerLevel world) { 234 167 235 168 // get the block below the player 236 169 BlockPos belowPlayer = new BlockPos(bottomPlayer.getX(), bottomPlayer.getY() -1, bottomPlayer.getZ()); // below the player