From b765167809a298224ebbdae04cbd697b20e83b77 Mon Sep 17 00:00:00 2001 From: Picono435 Date: Wed, 22 Jun 2022 10:52:18 +0100 Subject: [PATCH] Working on performance improvements for RandomTP API --- build.gradle | 2 +- .../picono435/randomtp/api/RandomTPAPI.java | 57 ++++++++++--------- .../randomtp/commands/RTPBCommand.java | 6 +- .../randomtp/commands/RTPCommand.java | 8 +-- .../randomtp/commands/RTPDCommand.java | 10 ++-- .../randomtp/fabric/RandomTPModFabric.java | 1 - 6 files changed, 43 insertions(+), 41 deletions(-) diff --git a/build.gradle b/build.gradle index a27229d..9c9910d 100644 --- a/build.gradle +++ b/build.gradle @@ -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 { diff --git a/common/src/main/java/com/gmail/picono435/randomtp/api/RandomTPAPI.java b/common/src/main/java/com/gmail/picono435/randomtp/api/RandomTPAPI.java index df6db73..34e1457 100644 --- a/common/src/main/java/com/gmail/picono435/randomtp/api/RandomTPAPI.java +++ b/common/src/main/java/com/gmail/picono435/randomtp/api/RandomTPAPI.java @@ -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 { @@ -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 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; } @@ -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(); @@ -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 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()); } } diff --git a/common/src/main/java/com/gmail/picono435/randomtp/commands/RTPBCommand.java b/common/src/main/java/com/gmail/picono435/randomtp/commands/RTPBCommand.java index 86d1e52..605dbf8 100644 --- a/common/src/main/java/com/gmail/picono435/randomtp/commands/RTPBCommand.java +++ b/common/src/main/java/com/gmail/picono435/randomtp/commands/RTPBCommand.java @@ -49,7 +49,7 @@ 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()); @@ -57,12 +57,12 @@ private static int runCommand(ServerPlayer p, ResourceOrTagLocationArgument.Resu 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(); diff --git a/common/src/main/java/com/gmail/picono435/randomtp/commands/RTPCommand.java b/common/src/main/java/com/gmail/picono435/randomtp/commands/RTPCommand.java index 5f27b2b..c822afb 100644 --- a/common/src/main/java/com/gmail/picono435/randomtp/commands/RTPCommand.java +++ b/common/src/main/java/com/gmail/picono435/randomtp/commands/RTPCommand.java @@ -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())); @@ -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()); } diff --git a/common/src/main/java/com/gmail/picono435/randomtp/commands/RTPDCommand.java b/common/src/main/java/com/gmail/picono435/randomtp/commands/RTPDCommand.java index 8e43bd4..360d441 100644 --- a/common/src/main/java/com/gmail/picono435/randomtp/commands/RTPDCommand.java +++ b/common/src/main/java/com/gmail/picono435/randomtp/commands/RTPDCommand.java @@ -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(); @@ -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()); } diff --git a/fabric/src/main/java/com/gmail/picono435/randomtp/fabric/RandomTPModFabric.java b/fabric/src/main/java/com/gmail/picono435/randomtp/fabric/RandomTPModFabric.java index 2cf4bb4..e5bfe7b 100644 --- a/fabric/src/main/java/com/gmail/picono435/randomtp/fabric/RandomTPModFabric.java +++ b/fabric/src/main/java/com/gmail/picono435/randomtp/fabric/RandomTPModFabric.java @@ -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);