Skip to content

Commit

Permalink
MC1.12.2-1.3.5 (Forge 1.12.2)
Browse files Browse the repository at this point in the history
This version adds a default world to RTP command and fixes a lot of issues! Here is a detailed change-log:
- Added new default_world option
- Big code clean-up
- Optimized teleporting between dimension
- Fixed issue with min distance and max distance
- Fixed issue with colliding with world border
- Fixed issue #3
- Fixed issue #1
- Fixed 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

Permissions:
randomtp.command.basic - Execute /randomtp [DEFAULT: ALL]
randomtp.command.interdim - Execute /randomtpdimension [DEFAULT: ALL]
randomtp.cooldown.exempt - Ignore cooldown [DEFAULT: OP]
  • Loading branch information
Picono435 authored Apr 17, 2021
1 parent bf9528f commit 89028a3
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 201 deletions.
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

version = 'MC1.12.2-1.3.4'
version = 'MC1.12.2-1.3.5'
group = 'com.gmail.picono435.randomtp' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'RandomTP'

sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8' // Need this here so eclipse task generates correctly.
compileJava {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}

minecraft {
// The mappings can be changed at any time, and must be in the following format.
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/com/gmail/picono435/randomtp/api/RandomTPAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.gmail.picono435.randomtp.api;

import java.util.Map;
import java.util.Random;

import com.gmail.picono435.randomtp.config.Config;
import com.gmail.picono435.randomtp.config.Messages;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ITeleporter;

public class RandomTPAPI {

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

public static void randomTeleport(EntityPlayer p, World world) {
Random r = new Random();
int lowX = ((int)Math.round(Math.abs(p.getPosition().getX())) + Config.min_distance) * -1;
int highX = Math.abs((int)Math.round(p.getPosition().getX()) + Config.max_distance);
int lowZ = ((int)Math.round(Math.abs(p.getPosition().getZ())) + Config.min_distance) * -1;
int highZ = Math.abs((int)Math.round(p.getPosition().getZ()) + Config.max_distance);
if(Config.max_distance == 0) {
highX = (int) (world.getWorldBorder().getDiameter() / 2);
highZ = (int) (world.getWorldBorder().getDiameter() / 2);
}
int x = r.nextInt(highX-lowX) + lowX;
int y = 50;
int z = r.nextInt(highZ-lowZ) + lowZ;
int maxTries = Config.maxTries;
while (!isSafe(world, x, y, z) && (maxTries == -1 || maxTries > 0)) {
y++;
if(y >= 120) {
x = r.nextInt(highX-lowX) + lowX;
y = 50;
z = r.nextInt(highZ-lowZ) + lowZ;
continue;
}
if(maxTries > 0){
maxTries--;
}
if(maxTries == 0) {
TextComponentString msg = new TextComponentString(Messages.maxTries.replaceAll("\\{playerName\\}", p.getName()).replaceAll("&", "§"));
p.sendMessage(msg);
return;
}
}

final int xClas = x;
final int yClas = y;
final int zClas = z;
p.changeDimension(world.provider.getDimension(), new ITeleporter() {
@Override
public void placeEntity(World world, Entity entity, float yaw) {
entity.setWorld(world);
entity.setPositionAndUpdate(xClas, yClas, zClas);
}
});
TextComponentString succefull = new TextComponentString(Messages.succefully.replaceAll("\\{playerName\\}", p.getName()).replaceAll("\\{blockX\\}", "" + (int)p.getPositionVector().x).replaceAll("\\{blockY\\}", "" + (int)p.getPositionVector().y).replaceAll("\\{blockZ\\}", "" + (int)p.getPositionVector().z).replaceAll("&", "§"));
p.sendMessage(succefull);
}

public static boolean checkCooldown(EntityPlayer p, Map<String, Long> cooldowns) {
int cooldownTime = Config.cooldown;
if(cooldowns.containsKey(p.getName())) {
long secondsLeft = ((cooldowns.get(p.getName())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
if(secondsLeft > 0) {
return false;
} else {
return true;
}
} else {
return true;
}
}

public static long getCooldownLeft(EntityPlayer p, Map<String, Long> cooldowns) {
int cooldownTime = Config.cooldown;
long secondsLeft = ((cooldowns.get(p.getName())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
return secondsLeft;
}

public static boolean isSafe(World world, int newX, int newY, int newZ) {
if(newX >= world.getWorldBorder().maxX() || newZ >= world.getWorldBorder().maxZ()) return false;
if ((isEmpty(world, newX, newY, newZ)) &&
(!isDangerBlock(world, newX, newY - 1, newZ))) {
return true;
}
return false;
}

public 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;
}

public 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;
}
}
113 changes: 14 additions & 99 deletions src/main/java/com/gmail/picono435/randomtp/commands/RTPCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,61 @@

import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.Map;

import com.gmail.picono435.randomtp.MainMod;
import com.gmail.picono435.randomtp.api.RandomTPAPI;
import com.gmail.picono435.randomtp.config.Config;
import com.gmail.picono435.randomtp.config.Messages;
import com.google.common.collect.Lists;

import net.minecraft.block.Block;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.PlayerNotFoundException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraft.world.border.WorldBorder;
import net.minecraftforge.server.permission.PermissionAPI;

public class RTPCommand extends CommandBase {

private final List<String> aliases = Lists.newArrayList(MainMod.MODID, "randomtp", "rtp", "randomteleport", "rteleport", "rndtp", "rndteleport");

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

public static Block[] dangerBlockArray = { Blocks.LAVA, Blocks.FLOWING_LAVA, Blocks.WATER, Blocks.FLOWING_WATER, Blocks.AIR };

public Map<String, Long> cooldowns = new HashMap<String, Long>();

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] params) throws CommandException {
World world = getCommandSenderAsPlayer(sender).getEntityWorld();
WorldBorder border = world.getWorldBorder();
EntityPlayer p = getCommandSenderAsPlayer(sender);
if(!checkCooldown(p) && !PermissionAPI.hasPermission(p, "randomtp.cooldown.exempt")) {
long secondsLeft = getCooldownLeft(p);
if(!RandomTPAPI.checkCooldown(p, cooldowns) && !PermissionAPI.hasPermission(p, "randomtp.cooldown.exempt")) {
long secondsLeft = RandomTPAPI.getCooldownLeft(p, cooldowns);
TextComponentString cooldownmes = new TextComponentString(Messages.cooldown.replaceAll("\\{secondsLeft\\}", Long.toString(secondsLeft)).replaceAll("\\{playerName\\}", p.getName()).replaceAll("&", "§"));
p.sendMessage(cooldownmes);
return;
} else {
cooldowns.remove(p.getName());
if(Config.useOriginal) {
randomTeleport(p);
if(!Config.default_world.equals("playerworld")) {
RandomTPAPI.randomTeleport(p, p.getServer().getWorld(Integer.parseInt(Config.default_world)));
} else {
RandomTPAPI.randomTeleport(p, p.world);
}
cooldowns.put(sender.getName(), System.currentTimeMillis());
return;
}
if(Config.max_distance == 0) {
server.getCommandManager().executeCommand(server, "spreadplayers " + border.getCenterX() + " " + border.getCenterZ() + " " + Config.min_distance + " " + border.getDiameter()/2 + " false " + p.getDisplayNameString());
server.getCommandManager().executeCommand(server, "spreadplayers " + p.world.getWorldBorder().getCenterX() + " " + p.world.getWorldBorder().getCenterZ() + " " + Config.min_distance + " " + p.world.getWorldBorder().getDiameter()/2 + " false " + p.getDisplayNameString());
TextComponentString succefull = new TextComponentString(Messages.succefully.replaceAll("\\{playerName\\}", p.getName()).replaceAll("\\{blockX\\}", "" + (int)p.getPositionVector().x).replaceAll("\\{blockY\\}", "" + (int)p.getPositionVector().y).replaceAll("\\{blockZ\\}", "" + (int)p.getPositionVector().z).replaceAll("&", "§"));
p.sendMessage(succefull);
} else {
server.getCommandManager().executeCommand(server, "spreadplayers " + border.getCenterX() + " " + border.getCenterZ() + " " + Config.min_distance + " " + Config.max_distance + " false " + p.getDisplayNameString());
server.getCommandManager().executeCommand(server, "spreadplayers " + p.world.getWorldBorder().getCenterX() + " " + p.world.getWorldBorder().getCenterZ() + " " + Config.min_distance + " " + Config.max_distance + " false " + p.getDisplayNameString());
TextComponentString succefull = new TextComponentString(Messages.succefully.replaceAll("\\{playerName\\}", p.getName()).replaceAll("\\{blockX\\}", "" + (int)p.getPositionVector().x).replaceAll("\\{blockY\\}", "" + (int)p.getPositionVector().y).replaceAll("\\{blockZ\\}", "" + (int)p.getPositionVector().z).replaceAll("&", "§"));
p.sendMessage(succefull);
}
cooldowns.put(sender.getName(), System.currentTimeMillis());
}
}

public boolean checkCooldown(EntityPlayer p) {
int cooldownTime = Config.cooldown;
if(cooldowns.containsKey(p.getName())) {
long secondsLeft = ((cooldowns.get(p.getName())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
if(secondsLeft > 0) {
return false;
} else {
cooldowns.remove(p.getName());
return true;
}
} else {
return true;
}
}

public long getCooldownLeft(EntityPlayer p) {
int cooldownTime = Config.cooldown;
long secondsLeft = ((cooldowns.get(p.getName())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
return secondsLeft;
}

@Override
public String getName() {
return "randomtp";
Expand All @@ -106,65 +82,4 @@ public boolean checkPermission(MinecraftServer server, ICommandSender sender) {
return false;
}
}


private void randomTeleport(EntityPlayer p) {
Random r = new Random();
int low = Config.min_distance;
int high = Config.max_distance;
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 = Config.maxTries;
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) {
TextComponentString msg = new TextComponentString(Messages.maxTries.replaceAll("\\{playerName\\}", p.getName()).replaceAll("&", "§"));
p.sendMessage(msg);
return;
}
}

p.setPositionAndUpdate(x, y, z);
TextComponentString succefull = new TextComponentString(Messages.succefully.replaceAll("\\{playerName\\}", p.getName()).replaceAll("\\{blockX\\}", "" + (int)p.getPositionVector().x).replaceAll("\\{blockY\\}", "" + (int)p.getPositionVector().y).replaceAll("\\{blockZ\\}", "" + (int)p.getPositionVector().z).replaceAll("&", "§"));
p.sendMessage(succefull);
}

private boolean isSafe(EntityPlayer 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 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 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;
}
}
Loading

0 comments on commit 89028a3

Please sign in to comment.