Skip to content

Commit

Permalink
Updated to MC1.15.2-3.1
Browse files Browse the repository at this point in the history
This version is the start of the new original RTP system! This system will be safer, faster and more optimized than the old one ( /spreadplayers command ). Remember that this new system it's still on beta so if you find any issue send me a message on discord, to change from the old system to the new one just change the option use-original to true. Also this version was not possible to make without the incredible code of GeiloUtils (https://github.com/Geilokowski/GeiloUtils). Here is a detailed change-log:
- Created a new system based on GeiloUtils of the /rtp command
- Added the ability to choose between the New System and Old System in the config (use-original)
- Solved minor issues

How to update:
1. Delete the old .jar file from the mods folder of the server root
2. Move the new .jar to the folder mods of the server root
  • Loading branch information
Picono435 authored Aug 5, 2020
1 parent 63a7645 commit 3b82fb6
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/gmail/picono435/randomtp/MainMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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());
Expand All @@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

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;
import com.gmail.picono435.randomtp.config.Messages;
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;
Expand All @@ -22,19 +26,21 @@ public class RTPCommand {

private static HashMap<String, Long> cooldowns = new HashMap<String, Long>();

private static Block[] dangerBlockArray = { Blocks.LAVA, Blocks.WATER, Blocks.AIR };

public static void register(CommandDispatcher<CommandSource> 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;
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

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;
import com.gmail.picono435.randomtp.config.Messages;
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;
Expand All @@ -24,31 +28,33 @@ public class RTPDCommand {

private static HashMap<String, Long> cooldowns = new HashMap<String, Long>();

private static Block[] dangerBlockArray = { Blocks.LAVA, Blocks.WATER, Blocks.AIR };

public static void register(CommandDispatcher<CommandSource> 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;
Expand All @@ -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);
Expand Down Expand Up @@ -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 + "");
}
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Config {
public static ForgeConfigSpec.ConfigValue<Boolean> only_op_dim;

public static ForgeConfigSpec.ConfigValue<Integer> cooldown;
public static ForgeConfigSpec.ConfigValue<Boolean> useOriginal;

public static ForgeConfigSpec.ConfigValue<Boolean> dim;
public static ForgeConfigSpec.ConfigValue<Boolean> useWhitelist;
Expand All @@ -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
Expand All @@ -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]")
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b82fb6

Please sign in to comment.