diff --git a/build.gradle b/build.gradle index 6101778..353eec3 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ apply plugin: 'net.minecraftforge.gradle' apply plugin: 'eclipse' apply plugin: 'maven-publish' -version = 'MC1.15.2-3.0' +version = 'MC1.15.2-3.1' group = 'com.gmail.picono435.randomtp' // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = 'RandomTP' diff --git a/src/main/java/com/gmail/picono435/randomtp/MainMod.java b/src/main/java/com/gmail/picono435/randomtp/MainMod.java index 62b0218..3c9a9b2 100644 --- a/src/main/java/com/gmail/picono435/randomtp/MainMod.java +++ b/src/main/java/com/gmail/picono435/randomtp/MainMod.java @@ -7,6 +7,7 @@ import com.gmail.picono435.randomtp.commands.RTPCommand; import com.gmail.picono435.randomtp.commands.RTPDCommand; +import com.gmail.picono435.randomtp.config.Config; import com.gmail.picono435.randomtp.config.ConfigHandler; import net.minecraft.server.MinecraftServer; @@ -27,7 +28,6 @@ public class MainMod { public static final String MODID = "randomtp"; public static final String NAME = "Random Teleport Mod"; - public static final String VERSION = "1.3"; public static final String NEW_LINE; @@ -46,7 +46,6 @@ public MainMod() { ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, ConfigHandler.messages, "RandomTP" + File.separatorChar + "messages.toml"); FMLJavaModLoadingContext.get().getModEventBus().addListener(this::preInit); - FMLJavaModLoadingContext.get().getModEventBus().addListener(this::init); ConfigHandler.loadConfig(ConfigHandler.config, FMLPaths.CONFIGDIR.get().resolve("RandomTP" + File.separatorChar + "config.toml").toString()); ConfigHandler.loadConfig(ConfigHandler.messages, FMLPaths.CONFIGDIR.get().resolve("RandomTP" + File.separatorChar + "messages.toml").toString()); @@ -55,23 +54,24 @@ public MainMod() { } @SubscribeEvent - public void preInit(FMLCommonSetupEvent event) - { + public void preInit(FMLCommonSetupEvent event) { logger = LogManager.getLogger(); } @SubscribeEvent - public void init(FMLServerStartingEvent event) - { + public void init(FMLServerStartingEvent event) { logger.info("Initalized Random Teleport Mod."); server = event.getServer(); RTPCommand.register(event.getCommandDispatcher()); - RTPDCommand.register(event.getCommandDispatcher()); + + if(Config.dim.get()) { + RTPDCommand.register(event.getCommandDispatcher()); + } logger.info("Configs files loaded."); PermissionAPI.registerNode("randomtp.command.basic", DefaultPermissionLevel.OP, "The permission to execute the command /randomtp"); PermissionAPI.registerNode("randomtp.command.interdim", DefaultPermissionLevel.OP, "The permission to execute the command /randomtp"); } -} +} \ No newline at end of file diff --git a/src/main/java/com/gmail/picono435/randomtp/commands/RTPCommand.java b/src/main/java/com/gmail/picono435/randomtp/commands/RTPCommand.java index faa3fd8..91201f6 100644 --- a/src/main/java/com/gmail/picono435/randomtp/commands/RTPCommand.java +++ b/src/main/java/com/gmail/picono435/randomtp/commands/RTPCommand.java @@ -2,6 +2,7 @@ import java.math.BigDecimal; import java.util.HashMap; +import java.util.Random; import com.gmail.picono435.randomtp.MainMod; import com.gmail.picono435.randomtp.config.Config; @@ -9,10 +10,13 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.minecraft.block.Block; +import net.minecraft.block.Blocks; import net.minecraft.command.CommandSource; import net.minecraft.command.Commands; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.MinecraftServer; +import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.StringTextComponent; import net.minecraft.world.World; import net.minecraft.world.border.WorldBorder; @@ -22,19 +26,21 @@ public class RTPCommand { private static HashMap cooldowns = new HashMap(); + private static Block[] dangerBlockArray = { Blocks.LAVA, Blocks.WATER, Blocks.AIR }; + public static void register(CommandDispatcher dispatcher) { dispatcher.register(Commands.literal("rtp").requires(source -> hasPermission(source)) - .executes(context -> randomTeleport(context.getSource().asPlayer()) + .executes(context -> runCommand(context.getSource().asPlayer()) )); dispatcher.register(Commands.literal("randomtp").requires(source -> hasPermission(source)) - .executes(context -> randomTeleport(context.getSource().asPlayer()) + .executes(context -> runCommand(context.getSource().asPlayer()) )); dispatcher.register(Commands.literal("randomteleport").requires(source -> hasPermission(source)) - .executes(context -> randomTeleport(context.getSource().asPlayer()) + .executes(context -> runCommand(context.getSource().asPlayer()) )); } - private static int randomTeleport(PlayerEntity p) { + private static int runCommand(PlayerEntity p) { World world = p.getEntityWorld(); WorldBorder border = world.getWorldBorder(); MinecraftServer server = MainMod.server; @@ -45,6 +51,11 @@ private static int randomTeleport(PlayerEntity p) { p.sendMessage(cooldownmes); return 1; } else { + if(Config.useOriginal.get()) { + randomTeleport(p); + cooldowns.put(p.getName().getString(), System.currentTimeMillis()); + return 1; + } double cal = border.getDiameter()/2; BigDecimal num = new BigDecimal(cal); String maxDistance = num.toPlainString(); @@ -88,4 +99,69 @@ private static boolean hasPermission(CommandSource source) { return false; } } + + private static void randomTeleport(PlayerEntity p) { + try { + Random r = new Random(); + int low = Config.min_distance.get(); + int high = Config.max_distance.get(); + if(high == 0) { + high = (int) (p.world.getWorldBorder().getDiameter() / 2); + } + int x = r.nextInt(high-low) + low; + int y = 50; + int z = r.nextInt(high-low) + low; + int maxTries = -1; + while (!isSafe(p, x, y, z) && (maxTries == -1 || maxTries > 0)) { + y++; + if(y >= 120) { + x = r.nextInt(high-low) + low; + y = 50; + z = r.nextInt(high-low) + low; + continue; + } + if(maxTries > 0){ + maxTries--; + } + if(maxTries == 0) { + StringTextComponent msg = new StringTextComponent("Error, please try again."); + p.sendMessage(msg); + return; + } + } + + p.setPositionAndUpdate(x, y, z); + StringTextComponent succefull = new StringTextComponent(Messages.succefully.get().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{blockZ\\}", "" + p.getPositionVector().z).replaceAll("\\{blockX\\}", "" + p.getPositionVector().x).replaceAll("&", "§")); + p.sendMessage(succefull); + } catch(Exception ex) { + MainMod.logger.info("Error executing command."); + ex.printStackTrace(); + } + } + + private static boolean isSafe(PlayerEntity player, int newX, int newY, int newZ) { + if ((isEmpty(player.world, newX, newY, newZ)) && + (!isDangerBlock(player.world, newX, newY - 1, newZ))) { + return true; + } + return false; + } + + private static boolean isEmpty(World world, int newX, int newY, int newZ) { + if ((world.isAirBlock(new BlockPos(newX, newY, newZ))) && (world.isAirBlock(new BlockPos(newX, newY + 1, newZ))) && + (world.isAirBlock(new BlockPos(newX + 1, newY, newZ))) && (world.isAirBlock(new BlockPos(newX - 1, newY, newZ))) && + (world.isAirBlock(new BlockPos(newX, newY, newZ + 1))) && (world.isAirBlock(new BlockPos(newX, newY, newZ - 1)))) { + return true; + } + return false; + } + + private static boolean isDangerBlock(World world, int newX, int newY, int newZ) { + for (Block block : dangerBlockArray) { + if (block.equals(world.getBlockState(new BlockPos(newX, newY, newZ)).getBlock())) { + return true; + } + } + return false; + } } diff --git a/src/main/java/com/gmail/picono435/randomtp/commands/RTPDCommand.java b/src/main/java/com/gmail/picono435/randomtp/commands/RTPDCommand.java index 3497802..4be2361 100644 --- a/src/main/java/com/gmail/picono435/randomtp/commands/RTPDCommand.java +++ b/src/main/java/com/gmail/picono435/randomtp/commands/RTPDCommand.java @@ -2,6 +2,7 @@ import java.math.BigDecimal; import java.util.HashMap; +import java.util.Random; import com.gmail.picono435.randomtp.MainMod; import com.gmail.picono435.randomtp.config.Config; @@ -9,11 +10,14 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.minecraft.block.Block; +import net.minecraft.block.Blocks; import net.minecraft.command.CommandSource; import net.minecraft.command.Commands; import net.minecraft.command.arguments.DimensionArgument; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.MinecraftServer; +import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.StringTextComponent; import net.minecraft.world.World; import net.minecraft.world.border.WorldBorder; @@ -24,31 +28,33 @@ public class RTPDCommand { private static HashMap cooldowns = new HashMap(); + private static Block[] dangerBlockArray = { Blocks.LAVA, Blocks.WATER, Blocks.AIR }; + public static void register(CommandDispatcher dispatcher) { dispatcher.register(Commands.literal("rtpd").requires(source -> hasPermission(source)) .then( Commands.argument("dimension", DimensionArgument.getDimension()) .executes(context -> - randomTeleport(context.getSource().asPlayer(), DimensionArgument.getDimensionArgument(context, "dimension")) + runCommand(context.getSource().asPlayer(), DimensionArgument.getDimensionArgument(context, "dimension")) ) )); dispatcher.register(Commands.literal("randomteleportdimension").requires(source -> hasPermission(source)) .then( Commands.argument("dimension", DimensionArgument.getDimension()) .executes(context -> - randomTeleport(context.getSource().asPlayer(), DimensionArgument.getDimensionArgument(context, "dimension")) + runCommand(context.getSource().asPlayer(), DimensionArgument.getDimensionArgument(context, "dimension")) ) )); dispatcher.register(Commands.literal("randomtpd").requires(source -> hasPermission(source)) .then( Commands.argument("dimension", DimensionArgument.getDimension()) - .executes(context -> - randomTeleport(context.getSource().asPlayer(), DimensionArgument.getDimensionArgument(context, "dimension")) + .executes(context -> + runCommand(context.getSource().asPlayer(), DimensionArgument.getDimensionArgument(context, "dimension")) ) )); } - private static int randomTeleport(PlayerEntity p, DimensionType dim) { + private static int runCommand(PlayerEntity p, DimensionType dim) { World world = p.getEntityWorld(); WorldBorder border = world.getWorldBorder(); MinecraftServer server = MainMod.server; @@ -69,6 +75,16 @@ private static int randomTeleport(PlayerEntity p, DimensionType dim) { return 1; } p.changeDimension(dim); + if(Config.useOriginal.get()) { + randomTeleport(p); + cooldowns.put(p.getName().getString(), System.currentTimeMillis()); + return 1; + } + if(Config.useOriginal.get()) { + randomTeleport(p); + cooldowns.put(p.getName().getString(), System.currentTimeMillis()); + return 1; + } if(Config.max_distance.get() == 0) { server.getCommandManager().handleCommand(server.getCommandSource(), "spreadplayers " + border.getCenterX() + " " + border.getCenterZ() + " " + Config.min_distance.get() + " " + maxDistance + " false " + p.getName().getString().toLowerCase()); p.sendMessage(succefull); @@ -108,7 +124,7 @@ public static boolean inWhitelist(int dimension) { return Config.allowedDimensions.get().contains(dimension + ""); //BLACKLIST } else { - return !Config.allowedDimensions.get().contains(dimension + ""); + return !Config.allowedDimensions.get().contains(dimension + ""); } } @@ -119,4 +135,69 @@ private static boolean hasPermission(CommandSource source) { return false; } } + + private static void randomTeleport(PlayerEntity p) { + try { + Random r = new Random(); + int low = Config.min_distance.get(); + int high = Config.max_distance.get(); + if(high == 0) { + high = (int) (p.world.getWorldBorder().getDiameter() / 2); + } + int x = r.nextInt(high-low) + low; + int y = 50; + int z = r.nextInt(high-low) + low; + int maxTries = -1; + while (!isSafe(p, x, y, z) && (maxTries == -1 || maxTries > 0)) { + y++; + if(y >= 120) { + x = r.nextInt(high-low) + low; + y = 50; + z = r.nextInt(high-low) + low; + continue; + } + if(maxTries > 0){ + maxTries--; + } + if(maxTries == 0) { + StringTextComponent msg = new StringTextComponent("Error, please try again."); + p.sendMessage(msg); + return; + } + } + + p.setPositionAndUpdate(x, y, z); + StringTextComponent succefull = new StringTextComponent(Messages.succefully.get().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{blockZ\\}", "" + p.getPositionVector().z).replaceAll("\\{blockX\\}", "" + p.getPositionVector().x).replaceAll("&", "§")); + p.sendMessage(succefull); + } catch(Exception ex) { + MainMod.logger.info("Error executing command."); + ex.printStackTrace(); + } + } + + private static boolean isSafe(PlayerEntity player, int newX, int newY, int newZ) { + if ((isEmpty(player.world, newX, newY, newZ)) && + (!isDangerBlock(player.world, newX, newY - 1, newZ))) { + return true; + } + return false; + } + + private static boolean isEmpty(World world, int newX, int newY, int newZ) { + if ((world.isAirBlock(new BlockPos(newX, newY, newZ))) && (world.isAirBlock(new BlockPos(newX, newY + 1, newZ))) && + (world.isAirBlock(new BlockPos(newX + 1, newY, newZ))) && (world.isAirBlock(new BlockPos(newX - 1, newY, newZ))) && + (world.isAirBlock(new BlockPos(newX, newY, newZ + 1))) && (world.isAirBlock(new BlockPos(newX, newY, newZ - 1)))) { + return true; + } + return false; + } + + private static boolean isDangerBlock(World world, int newX, int newY, int newZ) { + for (Block block : dangerBlockArray) { + if (block.equals(world.getBlockState(new BlockPos(newX, newY, newZ)).getBlock())) { + return true; + } + } + return false; + } } diff --git a/src/main/java/com/gmail/picono435/randomtp/config/Config.java b/src/main/java/com/gmail/picono435/randomtp/config/Config.java index 847fd63..399471c 100644 --- a/src/main/java/com/gmail/picono435/randomtp/config/Config.java +++ b/src/main/java/com/gmail/picono435/randomtp/config/Config.java @@ -14,6 +14,7 @@ public class Config { public static ForgeConfigSpec.ConfigValue only_op_dim; public static ForgeConfigSpec.ConfigValue cooldown; + public static ForgeConfigSpec.ConfigValue useOriginal; public static ForgeConfigSpec.ConfigValue dim; public static ForgeConfigSpec.ConfigValue useWhitelist; @@ -24,7 +25,7 @@ public static void initConfig(ForgeConfigSpec.Builder config) { //DISTANCE CATEGORY max_distance = config - .comment(" Max distance that you want to a person be teleported. (auto = world border size / 2) [default: auto]") + .comment(" Max distance that you want to a person be teleported. (0 = world border size / 2) [default: auto]") .defineInRange("distance.max_distance", 0, 0, Integer.MAX_VALUE); min_distance = config @@ -50,6 +51,10 @@ public static void initConfig(ForgeConfigSpec.Builder config) { .comment("How much cooldown do you want for the command (put 0 for none) [range: 0 ~ 1000, default: 0]") .defineInRange("others.cooldown", 0, 0, Integer.MAX_VALUE); + useOriginal = config + .comment("If you want to use the original RTP system or the /spreadplayers system. *ORIGINAL SYSTEM STILL ON BETA*") + .define("others.use-original", false); + //PERMISSION CATEGORY only_op_basic = config .comment("If you want only op players or with the required permission node to execute the basic /rtp command. (Permission node: randomtp.command.basic) [default: true]") diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index e0fe448..5811320 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -3,7 +3,7 @@ loaderVersion="[28,)" #mandatory (28 is current forge version) issueTrackerURL="https://github.com/Picono435/RandomTP/issues" #optional [[mods]] #mandatory modId="randomtp" #mandatory -version="MC1.15.2-3.0" #mandatory +version="MC1.15.2-3.1" #mandatory displayName="Random Teleport Mod" #mandatory displayURL="https://www.piconodev.tk/" #optional logoFile="logo.jpeg" #optional