Skip to content

Commit

Permalink
Working on performance improvements for RandomTP API
Browse files Browse the repository at this point in the history
  • Loading branch information
Picono435 committed Jun 22, 2022
1 parent 4d1dae7 commit b765167
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 41 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id "architectury-plugin" version "3.4-SNAPSHOT"
id "dev.architectury.loom" version "0.11.0-SNAPSHOT" apply false
id "dev.architectury.loom" version "0.12.0-SNAPSHOT" apply false
}

architectury {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;

import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.*;

public class RandomTPAPI {

Expand Down Expand Up @@ -54,18 +52,22 @@ public static void randomTeleport(ServerPlayer player, ServerLevel world, Biome
z = biomePos.getZ();
}
int maxTries = Config.getMaxTries();
while (!isSafe(world, x, y, z) && (maxTries == -1 || maxTries > 0)) {
BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos(x, y, z);
while (!isSafe(world, mutableBlockPos) && (maxTries == -1 || maxTries > 0)) {
y++;
if(y >= 120 || !isInBiomeWhitelist(getBiomeId(getBiomeFromKey(world.getBiome(new BlockPos(x, y, z)).unwrapKey().get())).toString())) {
mutableBlockPos.setY(y);
if(y >= 120 || !isInBiomeWhitelist(getBiomeId(getBiomeFromKey(world.getBiome(new BlockPos(x, y, z)).unwrapKey().get())))) {
x = r.nextInt(highX-lowX) + lowX;
y = 50;
z = r.nextInt(highZ-lowZ) + lowZ;
mutableBlockPos.set(x, y, z);
if(biome != null) {
ResourceOrTagLocationArgument.Result<Biome> result = new ResourceResult<>(getBiomeResourceKey(biome));
BlockPos biomePos = world.findClosestBiome3d(result, new BlockPos(x, y, z), 6400, 32, 64).getFirst();
x = biomePos.getX();
y = biomePos.getY();
z = biomePos.getZ();
mutableBlockPos.set(x, y, z);
}
continue;
}
Expand All @@ -74,14 +76,14 @@ public static void randomTeleport(ServerPlayer player, ServerLevel world, Biome
}
if(maxTries == 0) {
Component msg = Component.literal(Messages.getMaxTries().replaceAll("\\{playerName\\}", player.getName().getString()).replaceAll("&", "§"));
player.sendSystemMessage(msg, ChatType.CHAT);
player.sendSystemMessage(msg, ChatType.SYSTEM);
return;
}
}

player.teleportTo(world, x, y, z, player.getXRot(), player.getYRot());
Component successful = Component.literal(Messages.getSuccessful().replaceAll("\\{playerName\\}", player.getName().getString()).replaceAll("\\{blockX\\}", "" + (int)player.position().x).replaceAll("\\{blockY\\}", "" + (int)player.position().y).replaceAll("\\{blockZ\\}", "" + (int)player.position().z).replaceAll("&", "§"));
player.sendSystemMessage(successful, ChatType.CHAT);
player.sendSystemMessage(successful, ChatType.SYSTEM);
} catch(Exception ex) {
RandomTP.getLogger().info("Error executing command.");
ex.printStackTrace();
Expand Down Expand Up @@ -145,44 +147,45 @@ public static boolean hasPermission(CommandSourceStack source, String permission
throw new AssertionError();
}

public static boolean isSafe(ServerLevel world, int newX, int newY, int newZ) {
if(newX >= world.getWorldBorder().getMaxX() || newZ >= world.getWorldBorder().getMaxZ()) return false;
if ((isEmpty(world, newX, newY, newZ)) &&
(!isDangerBlock(world, newX, newY - 1, newZ))) {
public static boolean isSafe(ServerLevel world, BlockPos.MutableBlockPos mutableBlockPos) {
if(mutableBlockPos.getX() >= world.getWorldBorder().getMaxX() || mutableBlockPos.getZ() >= world.getWorldBorder().getMaxZ()) return false;
if ((isEmpty(world, mutableBlockPos)) &&
(!isDangerBlock(world, mutableBlockPos))) {
return true;
}
return false;
}

public static boolean isEmpty(Level world, int newX, int newY, int newZ) {
if ((world.isEmptyBlock(new BlockPos(newX, newY, newZ))) && (world.isEmptyBlock(new BlockPos(newX, newY + 1, newZ))) &&
(world.isEmptyBlock(new BlockPos(newX + 1, newY, newZ))) && (world.isEmptyBlock(new BlockPos(newX - 1, newY, newZ))) &&
(world.isEmptyBlock(new BlockPos(newX, newY, newZ + 1))) && (world.isEmptyBlock(new BlockPos(newX, newY, newZ - 1)))) {
public static boolean isEmpty(Level world, BlockPos.MutableBlockPos mutableBlockPos) {
if ((world.isEmptyBlock(new BlockPos(mutableBlockPos.getX(), mutableBlockPos.getY(), mutableBlockPos.getZ()))) && (world.isEmptyBlock(new BlockPos(mutableBlockPos.getX(), mutableBlockPos.getY() + 1, mutableBlockPos.getZ()))) &&
(world.isEmptyBlock(new BlockPos(mutableBlockPos.getX() + 1, mutableBlockPos.getY(), mutableBlockPos.getZ()))) && (world.isEmptyBlock(new BlockPos(mutableBlockPos.getX() - 1, mutableBlockPos.getY(), mutableBlockPos.getZ()))) &&
(world.isEmptyBlock(new BlockPos(mutableBlockPos.getX(), mutableBlockPos.getY(), mutableBlockPos.getZ() + 1))) && (world.isEmptyBlock(new BlockPos(mutableBlockPos.getX(), mutableBlockPos.getY(), mutableBlockPos.getZ() - 1)))) {
return true;
}
return false;
}

public static boolean isDangerBlock(Level world, int newX, int newY, int newZ) {
for (Block block : getDangerBlocks()) {
if (block.equals(world.getBlockState(new BlockPos(newX, newY, newZ)).getBlock())) {
return true;
}
}
return false;
public static boolean isDangerBlock(Level world, BlockPos.MutableBlockPos mutableBlockPos) {
return getDangerBlocks().contains(world.getBlockState(mutableBlockPos).getBlock());
}

public static Block[] getDangerBlocks() {
return new Block[] {Blocks.LAVA, Blocks.WATER, Blocks.AIR};
public static List<Block> getDangerBlocks() {
return Arrays.asList(Blocks.LAVA, Blocks.WATER, Blocks.AIR);
}

private static boolean isInBiomeWhitelist(String biome) {
private static boolean isInBiomeWhitelist(ResourceLocation biome) {
//WHITELIST
if(Config.useBiomeWhitelist()) {
return Config.getAllowedBiomes().contains(biome);
if(biome == null) {
return false;
}
return Config.getAllowedBiomes().contains(biome.toString());
//BLACKLIST
} else {
return !Config.getAllowedBiomes().contains(biome);
if(biome == null) {
return true;
}
return !Config.getAllowedBiomes().contains(biome.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@ private static int runCommand(ServerPlayer p, ResourceOrTagLocationArgument.Resu
if(!RandomTPAPI.checkCooldown(p, cooldowns) && !RandomTPAPI.hasPermission(p, "randomtp.cooldown.exempt")) {
long secondsLeft = RandomTPAPI.getCooldownLeft(p, cooldowns);
Component cooldownmes = Component.literal(Messages.getCooldown().replaceAll("\\{secondsLeft\\}", Long.toString(secondsLeft)).replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("&", "§"));
p.sendSystemMessage(cooldownmes, ChatType.CHAT);
p.sendSystemMessage(cooldownmes, ChatType.SYSTEM);
return 1;
} else {
cooldowns.remove(p.getName().getString());
ResourceKey<Biome> biomeKey = biome.unwrap().left().get();
ResourceLocation biomeLocation = biomeKey.location();
String biomeId = biomeLocation.getNamespace() + ":" + biomeLocation.getPath();
if(!inWhitelist(biomeId)) {
p.sendSystemMessage(Component.literal(Messages.getDimensionNotAllowed().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{biomeId\\}", biomeId.toString()).replace('&', '§')), ChatType.CHAT);
p.sendSystemMessage(Component.literal(Messages.getDimensionNotAllowed().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{biomeId\\}", biomeId.toString()).replace('&', '§')), ChatType.SYSTEM);
return 1;
}
if(Config.useOriginal()) {
Component finding = Component.literal(Messages.getFinding().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{blockX\\}", "" + (int)p.position().x).replaceAll("\\{blockY\\}", "" + (int)p.position().y).replaceAll("\\{blockZ\\}", "" + (int)p.position().z).replaceAll("&", "§"));
p.sendSystemMessage(finding, ChatType.CHAT);
p.sendSystemMessage(finding, ChatType.SYSTEM);
new Thread(() -> {
RandomTPAPI.randomTeleport(p, p.getLevel(), RandomTPAPI.getBiomeFromKey(biomeKey));
}).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ private static int runCommand(ServerPlayer p) {
if(!RandomTPAPI.checkCooldown(p, cooldowns) && !RandomTPAPI.hasPermission(p, "randomtp.cooldown.exempt")) {
long secondsLeft = RandomTPAPI.getCooldownLeft(p, cooldowns);
Component cooldownmes = Component.literal(Messages.getCooldown().replaceAll("\\{secondsLeft\\}", Long.toString(secondsLeft)).replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("&", "§"));
p.sendSystemMessage(cooldownmes, ChatType.CHAT);
p.sendSystemMessage(cooldownmes, ChatType.SYSTEM);
return 1;
} else {
cooldowns.remove(p.getName().getString());
if(Config.useOriginal()) {
Component finding = Component.literal(Messages.getFinding().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{blockX\\}", "" + (int)p.position().x).replaceAll("\\{blockY\\}", "" + (int)p.position().y).replaceAll("\\{blockZ\\}", "" + (int)p.position().z).replaceAll("&", "§"));
p.sendSystemMessage(finding, ChatType.CHAT);
p.sendSystemMessage(finding, ChatType.SYSTEM);
new Thread(() -> {
if(!Config.getDefaultWorld().equals("playerworld")) {
RandomTPAPI.randomTeleport(p, RandomTPAPI.getWorld(Config.getDefaultWorld(), p.getServer()));
Expand All @@ -56,11 +56,11 @@ private static int runCommand(ServerPlayer p) {
if(Config.getMaxDistance() == 0) {
p.getServer().getCommands().performCommand(p.getServer().createCommandSourceStack(), "spreadplayers " + p.getLevel().getWorldBorder().getCenterX() + " " + p.getLevel().getWorldBorder().getCenterZ() + " " + Config.getMinDistance() + " " + maxDistance + " false " + p.getName().getString().toLowerCase());
Component successful = Component.literal(Messages.getSuccessful().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{blockX\\}", "" + (int)p.position().x).replaceAll("\\{blockY\\}", "" + (int)p.position().y).replaceAll("\\{blockZ\\}", "" + (int)p.position().z).replaceAll("&", "§"));
p.sendSystemMessage(successful, ChatType.CHAT);
p.sendSystemMessage(successful, ChatType.SYSTEM);
} else {
p.getServer().getCommands().performCommand(p.getServer().createCommandSourceStack(), "spreadplayers " + p.getLevel().getWorldBorder().getCenterX() + " " + p.getLevel().getWorldBorder().getCenterZ() + " " + Config.getMinDistance() + " " + Config.getMaxDistance() + " false " + p.getName().getString().toLowerCase());
Component successful = Component.literal(Messages.getSuccessful().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{blockX\\}", "" + (int)p.position().x).replaceAll("\\{blockY\\}", "" + (int)p.position().y).replaceAll("\\{blockZ\\}", "" + (int)p.position().z).replaceAll("&", "§"));
p.sendSystemMessage(successful, ChatType.CHAT);
p.sendSystemMessage(successful, ChatType.SYSTEM);
}
cooldowns.put(p.getName().getString(), System.currentTimeMillis());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ private static int runCommand(ServerPlayer p, ServerLevel dim) {
if(!RandomTPAPI.checkCooldown(p, cooldowns) && !RandomTPAPI.hasPermission(p, "randomtp.cooldown.exempt")) {
long secondsLeft = RandomTPAPI.getCooldownLeft(p, cooldowns);
Component cooldownmes = Component.literal(Messages.getCooldown().replaceAll("\\{secondsLeft\\}", Long.toString(secondsLeft)).replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("&", "§"));
p.sendSystemMessage(cooldownmes, ChatType.CHAT);
p.sendSystemMessage(cooldownmes, ChatType.SYSTEM);
return 1;
} else {
cooldowns.remove(p.getName().getString());
String dimensionId = dim.dimension().location().getNamespace() + ":" + dim.dimension().location().getPath();
if(!inWhitelist(dimensionId)) {
p.sendSystemMessage(Component.literal(Messages.getDimensionNotAllowed().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{dimensionId\\}", dimensionId.toString()).replace('&', '§')), ChatType.CHAT);
p.sendSystemMessage(Component.literal(Messages.getDimensionNotAllowed().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{dimensionId\\}", dimensionId.toString()).replace('&', '§')), ChatType.SYSTEM);
return 1;
}
if(Config.useOriginal()) {
Component finding = Component.literal(Messages.getFinding().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{blockX\\}", "" + (int)p.position().x).replaceAll("\\{blockY\\}", "" + (int)p.position().y).replaceAll("\\{blockZ\\}", "" + (int)p.position().z).replaceAll("&", "§"));
p.sendSystemMessage(finding, ChatType.CHAT);
p.sendSystemMessage(finding, ChatType.SYSTEM);
new Thread(() -> {
RandomTPAPI.randomTeleport(p, dim);
}).start();
Expand All @@ -69,11 +69,11 @@ private static int runCommand(ServerPlayer p, ServerLevel dim) {
if(Config.getMaxDistance() == 0) {
p.getServer().getCommands().performCommand(p.getServer().createCommandSourceStack(), "spreadplayers " + p.getLevel().getWorldBorder().getCenterX() + " " + p.getLevel().getWorldBorder().getCenterZ() + " " + Config.getMinDistance() + " " + maxDistance + " false " + p.getName().getString().toLowerCase());
Component successful = Component.literal(Messages.getSuccessful().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{blockX\\}", "" + (int)p.position().x).replaceAll("\\{blockY\\}", "" + (int)p.position().y).replaceAll("\\{blockZ\\}", "" + (int)p.position().z).replaceAll("&", "§"));
p.sendSystemMessage(successful, ChatType.CHAT);
p.sendSystemMessage(successful, ChatType.SYSTEM);
} else {
p.getServer().getCommands().performCommand(p.getServer().createCommandSourceStack(), "spreadplayers " + p.getLevel().getWorldBorder().getCenterX() + " " + p.getLevel().getWorldBorder().getCenterZ() + " " + Config.getMinDistance() + " " + Config.getMaxDistance() + " false " + p.getName().getString().toLowerCase());
Component successful = Component.literal(Messages.getSuccessful().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{blockX\\}", "" + (int)p.position().x).replaceAll("\\{blockY\\}", "" + (int)p.position().y).replaceAll("\\{blockZ\\}", "" + (int)p.position().z).replaceAll("&", "§"));
p.sendSystemMessage(successful, ChatType.CHAT);
p.sendSystemMessage(successful, ChatType.SYSTEM);
}
cooldowns.put(p.getName().getString(), System.currentTimeMillis());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public void onInitialize() {
});

CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
if(!environment.includeDedicated) return;
RTPCommand.register(dispatcher);
if(Config.useDimension()) {
RTPDCommand.register(dispatcher);
Expand Down

0 comments on commit b765167

Please sign in to comment.