From 21c349ef3c408e5fdb1c4c1205567303e6a01042 Mon Sep 17 00:00:00 2001 From: zontreck Date: Thu, 11 Jan 2024 00:34:15 -0700 Subject: [PATCH] Fix RTP, update libzontreck --- build.gradle | 2 +- gradle.properties | 4 +- .../zontreck/essentials/AriasEssentials.java | 11 +++ .../commands/gui/HeartsCommand.java | 2 +- .../commands/homes/HomeCommand.java | 8 +- .../commands/homes/HomesCommand.java | 19 +++-- .../commands/homes/SetHomeCommand.java | 3 +- .../commands/teleport/TeleportRunnable.java | 2 + .../commands/warps/RTPWarpCommand.java | 3 +- .../commands/warps/SetWarpCommand.java | 3 +- .../commands/warps/WarpsCommand.java | 38 ++++++++- .../essentials/configs/AEServerConfig.java | 5 +- .../NoSuchHomeException.java | 2 +- .../dev/zontreck/essentials/homes/Home.java | 12 ++- .../dev/zontreck/essentials/homes/Homes.java | 1 + .../essentials/networking/ModMessages.java | 1 + .../{ => packets/s2c}/S2CUpdateHearts.java | 3 +- .../java/dev/zontreck/essentials/rtp/RTP.java | 65 +++++++++------- .../rtp/RTPCachesEventHandlers.java | 18 ++++- .../zontreck/essentials/rtp/RTPContainer.java | 37 --------- .../essentials/rtp/RandomPositionLocator.java | 77 ++++++------------- .../dev/zontreck/essentials/warps/Warp.java | 25 +++++- 22 files changed, 192 insertions(+), 149 deletions(-) rename src/main/java/dev/zontreck/essentials/{homes => exceptions}/NoSuchHomeException.java (57%) rename src/main/java/dev/zontreck/essentials/networking/{ => packets/s2c}/S2CUpdateHearts.java (90%) delete mode 100644 src/main/java/dev/zontreck/essentials/rtp/RTPContainer.java diff --git a/build.gradle b/build.gradle index 765acb8..0305be8 100644 --- a/build.gradle +++ b/build.gradle @@ -89,7 +89,7 @@ minecraft { server { property 'forge.enabledGameTestNamespaces', mod_id - args '--nogui' + //args '--nogui' } // This run config launches GameTestServer and runs all registered gametests, then exits. diff --git a/gradle.properties b/gradle.properties index b61d29d..e484412 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx3G org.gradle.daemon=false -libzontreck=1.10.010224.1940 +libzontreck=1.10.011024.0312 ## Environment Properties @@ -49,7 +49,7 @@ mod_name=Aria's Essentials # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=GPLv3 # The mod version. See https://semver.org/ -mod_version=1.2.010224.2002 +mod_version=1.2.011124.0032 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/dev/zontreck/essentials/AriasEssentials.java b/src/main/java/dev/zontreck/essentials/AriasEssentials.java index 18fb9b4..695032a 100644 --- a/src/main/java/dev/zontreck/essentials/AriasEssentials.java +++ b/src/main/java/dev/zontreck/essentials/AriasEssentials.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Random; import java.util.UUID; +import java.util.concurrent.Executor; import dev.zontreck.ariaslib.util.DelayedExecutorService; import dev.zontreck.essentials.client.Keybindings; @@ -14,6 +15,7 @@ import dev.zontreck.essentials.configs.AEServerConfig; import dev.zontreck.essentials.events.TeleportEvent; import dev.zontreck.essentials.gui.HeartsRenderer; import dev.zontreck.essentials.networking.ModMessages; +import dev.zontreck.essentials.rtp.RTPCaches; import dev.zontreck.essentials.rtp.RTPCachesEventHandlers; import dev.zontreck.essentials.util.BackPositionCaches; import dev.zontreck.libzontreck.vectors.WorldPosition; @@ -50,6 +52,9 @@ public class AriasEssentials { public static final Logger LOGGER = LogUtils.getLogger(); public static boolean ALIVE; public static Map player_homes = new HashMap<>(); + public static boolean DEBUG = true; + + public AriasEssentials() { @@ -71,6 +76,7 @@ public class AriasEssentials { MinecraftForge.EVENT_BUS.register(new CommandRegister()); MinecraftForge.EVENT_BUS.register(new ForgeEventsHandler()); MinecraftForge.EVENT_BUS.register(new RTPCachesEventHandlers()); + MinecraftForge.EVENT_BUS.register(RTPCachesEventHandlers.class); } @SubscribeEvent @@ -104,6 +110,11 @@ public class AriasEssentials { public void onServerStop(final ServerStoppingEvent ev) { ALIVE=false; + LOGGER.info("Tearing down Aria's Essentials functions and tasks"); + DelayedExecutorService.stop(); + + DelayedExecutorService.getInstance().EXECUTORS.clear(); + RTPCaches.Locations.clear(); } @SubscribeEvent (priority = EventPriority.HIGHEST) diff --git a/src/main/java/dev/zontreck/essentials/commands/gui/HeartsCommand.java b/src/main/java/dev/zontreck/essentials/commands/gui/HeartsCommand.java index 23d91ec..9d24057 100644 --- a/src/main/java/dev/zontreck/essentials/commands/gui/HeartsCommand.java +++ b/src/main/java/dev/zontreck/essentials/commands/gui/HeartsCommand.java @@ -4,7 +4,7 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.BoolArgumentType; import dev.zontreck.essentials.Messages; import dev.zontreck.essentials.networking.ModMessages; -import dev.zontreck.essentials.networking.S2CUpdateHearts; +import dev.zontreck.essentials.networking.packets.s2c.S2CUpdateHearts; import dev.zontreck.libzontreck.util.ChatHelpers; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; diff --git a/src/main/java/dev/zontreck/essentials/commands/homes/HomeCommand.java b/src/main/java/dev/zontreck/essentials/commands/homes/HomeCommand.java index 25d42e7..cdfa6a9 100644 --- a/src/main/java/dev/zontreck/essentials/commands/homes/HomeCommand.java +++ b/src/main/java/dev/zontreck/essentials/commands/homes/HomeCommand.java @@ -9,19 +9,13 @@ import dev.zontreck.essentials.Messages; import dev.zontreck.essentials.commands.teleport.TeleportActioner; import dev.zontreck.essentials.commands.teleport.TeleportContainer; import dev.zontreck.essentials.commands.teleport.TeleportDestination; -import dev.zontreck.essentials.configs.AEServerConfig; import dev.zontreck.essentials.homes.Home; -import dev.zontreck.essentials.homes.NoSuchHomeException; -import dev.zontreck.libzontreck.currency.Bank; -import dev.zontreck.libzontreck.currency.Transaction; -import dev.zontreck.libzontreck.exceptions.InvalidSideException; +import dev.zontreck.essentials.exceptions.NoSuchHomeException; import dev.zontreck.libzontreck.util.ChatHelpers; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.server.level.ServerPlayer; -import java.time.Instant; - public class HomeCommand { public static void register(CommandDispatcher dispatcher) { diff --git a/src/main/java/dev/zontreck/essentials/commands/homes/HomesCommand.java b/src/main/java/dev/zontreck/essentials/commands/homes/HomesCommand.java index 928abb2..578a805 100644 --- a/src/main/java/dev/zontreck/essentials/commands/homes/HomesCommand.java +++ b/src/main/java/dev/zontreck/essentials/commands/homes/HomesCommand.java @@ -16,6 +16,7 @@ import dev.zontreck.libzontreck.chat.Clickable; import dev.zontreck.libzontreck.chat.HoverTip; import dev.zontreck.libzontreck.chestgui.ChestGUI; import dev.zontreck.libzontreck.chestgui.ChestGUIButton; +import dev.zontreck.libzontreck.chestgui.ChestGUIIdentifier; import dev.zontreck.libzontreck.lore.LoreEntry; import dev.zontreck.libzontreck.util.ChatHelpers; import dev.zontreck.libzontreck.vectors.Vector2i; @@ -29,7 +30,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; public class HomesCommand { - private static final ResourceLocation HOMES_GUI_ID = new ResourceLocation("ariasmods", "homes-gui"); + private static final ChestGUIIdentifier HOMES_GUI_ID = new ChestGUIIdentifier("homes-gui"); public static void register(CommandDispatcher dispatcher) { @@ -54,7 +55,11 @@ public class HomesCommand { for (Home string : homes.getList()) { Style st = Style.EMPTY.withFont(Style.DEFAULT_FONT).withHoverEvent(HoverTip.get(ChatHelpers.macroize(Messages.HOME_HOVER_TEXT))).withClickEvent(Clickable.command("/home "+string.homeName)); - ItemStack stack = new ItemStack(Items.BLUE_BED); + ItemStack stack = string.homeIcon.copy(); + if(stack.is(Items.AIR)) + { + stack = new ItemStack(Items.GRASS_BLOCK, 1); + } stack.setHoverName(Component.literal(string.homeName)); ChestGUIButton button = new ChestGUIButton(stack, ()-> { @@ -69,16 +74,20 @@ public class HomesCommand { .withInfo(new LoreEntry.Builder().text(ChatHelpers.macro("!Dark_Purple!This home is in the dimension [0]", string.destination.Dimension).getString()).bold(true).build()); iconY++; - gui.withButton(button); if(iconY>=9) { iconY=0; iconX++; } - //ChatHelpers.broadcastTo(player.getUUID(), ChatHelpers.macro(Messages.HOME_FORMAT, string.homeName).setStyle(st), ctx.getSource().getServer()); + if(homes.count() > 27) + ChatHelpers.broadcastTo(player.getUUID(), ChatHelpers.macro(Messages.HOME_FORMAT, string.homeName).setStyle(st), ctx.getSource().getServer()); + else + gui.withButton(button); // Put this in the else case, to prevent a error when exceeding inventory slots } - gui.open(); + + if(homes.count()<=27) + gui.open(); }catch(CommandSyntaxException ex) { ex.printStackTrace(); diff --git a/src/main/java/dev/zontreck/essentials/commands/homes/SetHomeCommand.java b/src/main/java/dev/zontreck/essentials/commands/homes/SetHomeCommand.java index 8f77ee4..feb356a 100644 --- a/src/main/java/dev/zontreck/essentials/commands/homes/SetHomeCommand.java +++ b/src/main/java/dev/zontreck/essentials/commands/homes/SetHomeCommand.java @@ -22,6 +22,7 @@ import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; @@ -61,7 +62,7 @@ public class SetHomeCommand { TeleportDestination dest = new TeleportDestination(new Vector3(position), new Vector2(rot), p.serverLevel()); - Home newhome = new Home(p, homeName, dest); + Home newhome = new Home(p, homeName, dest, new ItemStack(p.getBlockStateOn().getBlock().asItem())); AriasEssentials.player_homes.get(p.getUUID()).add(newhome); diff --git a/src/main/java/dev/zontreck/essentials/commands/teleport/TeleportRunnable.java b/src/main/java/dev/zontreck/essentials/commands/teleport/TeleportRunnable.java index f953765..f720dd5 100644 --- a/src/main/java/dev/zontreck/essentials/commands/teleport/TeleportRunnable.java +++ b/src/main/java/dev/zontreck/essentials/commands/teleport/TeleportRunnable.java @@ -27,6 +27,8 @@ public class TeleportRunnable extends Task Action.PlayerInst.teleportTo(Action.Dimension, Action.Position.x, Action.Position.y, Action.Position.z, Action.Rotation.y, Action.Rotation.x); + Action.PlayerInst.onUpdateAbilities(); + DelayedExecutorService.getInstance().schedule(new Task("tp_action",true){ public TeleportContainer container=Action; @Override diff --git a/src/main/java/dev/zontreck/essentials/commands/warps/RTPWarpCommand.java b/src/main/java/dev/zontreck/essentials/commands/warps/RTPWarpCommand.java index 4c235e0..3e3dfc4 100644 --- a/src/main/java/dev/zontreck/essentials/commands/warps/RTPWarpCommand.java +++ b/src/main/java/dev/zontreck/essentials/commands/warps/RTPWarpCommand.java @@ -20,6 +20,7 @@ import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; import net.minecraftforge.common.MinecraftForge; @@ -44,7 +45,7 @@ public class RTPWarpCommand { Vec2 rot = p.getRotationVector(); TeleportDestination dest = new TeleportDestination(new Vector3(position), new Vector2(rot), p.serverLevel()); - Warp warp = new Warp(p.getUUID(), string, true, true, dest); + Warp warp = new Warp(p.getUUID(), string, true, true, dest, new ItemStack(p.getBlockStateOn().getBlock().asItem())); WarpCreatedEvent event = new WarpCreatedEvent(warp); if(MinecraftForge.EVENT_BUS.post(event)) { diff --git a/src/main/java/dev/zontreck/essentials/commands/warps/SetWarpCommand.java b/src/main/java/dev/zontreck/essentials/commands/warps/SetWarpCommand.java index 2ffd93c..ed92379 100644 --- a/src/main/java/dev/zontreck/essentials/commands/warps/SetWarpCommand.java +++ b/src/main/java/dev/zontreck/essentials/commands/warps/SetWarpCommand.java @@ -20,6 +20,7 @@ import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; import net.minecraftforge.common.MinecraftForge; @@ -45,7 +46,7 @@ public class SetWarpCommand { Vec2 rot = p.getRotationVector(); TeleportDestination dest = new TeleportDestination(new Vector3(position), new Vector2(rot), p.serverLevel()); - Warp w = new Warp(p.getUUID(), string, false, true, dest); + Warp w = new Warp(p.getUUID(), string, false, true, dest, new ItemStack(p.getBlockStateOn().getBlock().asItem())); WarpCreatedEvent event = new WarpCreatedEvent(w); if(MinecraftForge.EVENT_BUS.post(event)){ ChatHelpers.broadcastTo(p.getUUID(), ChatHelpers.macro(Messages.WARP_CREATE_ERROR, event.denyReason), p.server); diff --git a/src/main/java/dev/zontreck/essentials/commands/warps/WarpsCommand.java b/src/main/java/dev/zontreck/essentials/commands/warps/WarpsCommand.java index 30e044a..9c7e3f8 100644 --- a/src/main/java/dev/zontreck/essentials/commands/warps/WarpsCommand.java +++ b/src/main/java/dev/zontreck/essentials/commands/warps/WarpsCommand.java @@ -22,6 +22,7 @@ import dev.zontreck.libzontreck.chat.Clickable; import dev.zontreck.libzontreck.chat.HoverTip; import dev.zontreck.libzontreck.chestgui.ChestGUI; import dev.zontreck.libzontreck.chestgui.ChestGUIButton; +import dev.zontreck.libzontreck.chestgui.ChestGUIIdentifier; import dev.zontreck.libzontreck.lore.LoreEntry; import dev.zontreck.libzontreck.profiles.Profile; import dev.zontreck.libzontreck.profiles.UserProfileNotYetExistsException; @@ -39,7 +40,7 @@ import net.minecraft.server.level.ServerPlayer; import org.spongepowered.asm.mixin.Mutable; public class WarpsCommand { - private static final ResourceLocation WARPS_GUI_ID = new ResourceLocation("ariasmods", "ess_warps"); + private static final ChestGUIIdentifier WARPS_GUI_ID = new ChestGUIIdentifier("ess_warps"); public static void register(CommandDispatcher dispatcher) { dispatcher.register(Commands.literal("warps").executes(c-> warps(c.getSource()))); @@ -89,6 +90,30 @@ public class WarpsCommand { String appendType = (warpType == 0) ? Messages.WARP_STANDARD : Messages.WARP_RTP; + HoverEvent hover = HoverTip.get(ChatHelpers.macroize(appendType, warp.destination.Dimension)); + ClickEvent click = Clickable.command("/warp "+warpName); + + MutableComponent warpMsg = ChatHelpers.macro(ChatColor.GREEN + warpName + ChatColor.resetChat()); + + warpMsg = ChatHelpers.applyHoverEvent(warpMsg, hover); + // Now, display the warp name, along with the warp's owner information + HoverEvent h2 = HoverTip.get( + ChatHelpers.macroize(Messages.WARP_HOVER_FORMAT, + ChatHelpers.macroize(Messages.WARP_OWNER, prof.name_color, prof.nickname), + ChatHelpers.macroize(Messages.WARP_ACCESS_FORMAT, + (warp.isPublic ? ChatHelpers.macroize(Messages.PUBLIC) : ChatHelpers.macroize(Messages.PRIVATE)) + ) + ) + + ); + Component ownerInfo = ChatHelpers.applyHoverEvent(ChatHelpers.macro(Messages.HOVER_WARP_INFO), h2); + + + // Combine the two + warpMsg = warpMsg.copy().append(ownerInfo); + warpMsg = ChatHelpers.applyClickEvent(warpMsg, click); + + ChestGUIButton button = new ChestGUIButton(HeadUtilities.get(prof.username, warpName), ()->{ TeleportDestination dest = warp.destination; if(warpType == 1) @@ -114,7 +139,13 @@ public class WarpsCommand { .build() ) .withInfo(new LoreEntry.Builder().text(ChatHelpers.macro(appendType, warp.destination.Dimension).getString()).build()); - chestGui.withButton(button); + + if(warps.size() > 27) + { + // Say to person + ChatHelpers.broadcastTo(p, warpMsg, p.server); + }else + chestGui.withButton(button); iconY++; if(iconY>=9){ @@ -124,7 +155,8 @@ public class WarpsCommand { } - chestGui.open(); + if(warps.size() < 27) + chestGui.open(); return 0; } diff --git a/src/main/java/dev/zontreck/essentials/configs/AEServerConfig.java b/src/main/java/dev/zontreck/essentials/configs/AEServerConfig.java index 89eb943..48504d2 100644 --- a/src/main/java/dev/zontreck/essentials/configs/AEServerConfig.java +++ b/src/main/java/dev/zontreck/essentials/configs/AEServerConfig.java @@ -25,6 +25,8 @@ public class AEServerConfig { public static final ForgeConfigSpec.ConfigValue COST_TO_MAKE_WARP; public static final ForgeConfigSpec.ConfigValue MAX_HOMES; + public static final ForgeConfigSpec.ConfigValue MAX_WARPS; + public static final ForgeConfigSpec.ConfigValue> DIMENSION_BLACKLIST; public static final ForgeConfigSpec.ConfigValue BLACKLISTED_DIMENSION_ERROR; @@ -44,7 +46,8 @@ public class AEServerConfig { BUILDER.pop(); BUILDER.push("permissions").comment("This section defines permissions, such as total number of homes, and who can make warps"); - MAX_HOMES = BUILDER.comment("Maximum number of homes that are allowed per player (-1 disables the limit entirely)").define("max_homes", -1); + MAX_HOMES = BUILDER.comment("Maximum number of homes that are allowed per player (-1 disables the limit entirely). 27 is the current default as that is the max that can fit in the current /homes gui").define("max_homes", 27); + MAX_WARPS = BUILDER.comment("Maximum number of warps that are allowed to exist. Default is 27, the max number that can fit in the /warps gui").define("max_warps", 27); BUILDER.pop(); BUILDER.push("teleport"); diff --git a/src/main/java/dev/zontreck/essentials/homes/NoSuchHomeException.java b/src/main/java/dev/zontreck/essentials/exceptions/NoSuchHomeException.java similarity index 57% rename from src/main/java/dev/zontreck/essentials/homes/NoSuchHomeException.java rename to src/main/java/dev/zontreck/essentials/exceptions/NoSuchHomeException.java index 1c1821c..1a032c5 100644 --- a/src/main/java/dev/zontreck/essentials/homes/NoSuchHomeException.java +++ b/src/main/java/dev/zontreck/essentials/exceptions/NoSuchHomeException.java @@ -1,4 +1,4 @@ -package dev.zontreck.essentials.homes; +package dev.zontreck.essentials.exceptions; public class NoSuchHomeException extends Exception{ diff --git a/src/main/java/dev/zontreck/essentials/homes/Home.java b/src/main/java/dev/zontreck/essentials/homes/Home.java index f2b134b..ce832c4 100644 --- a/src/main/java/dev/zontreck/essentials/homes/Home.java +++ b/src/main/java/dev/zontreck/essentials/homes/Home.java @@ -6,18 +6,23 @@ import dev.zontreck.essentials.commands.teleport.TeleportDestination; import dev.zontreck.libzontreck.exceptions.InvalidDeserialization; import net.minecraft.nbt.CompoundTag; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; public class Home { public UUID owner; public String homeName; public TeleportDestination destination; + public ItemStack homeIcon; - public Home(ServerPlayer player, String name, TeleportDestination dest) + public Home(ServerPlayer player, String name, TeleportDestination dest, ItemStack homeIcon) { owner=player.getUUID(); homeName=name; destination=dest; + this.homeIcon = homeIcon; } public Home(CompoundTag tag) @@ -29,6 +34,10 @@ public class Home { } catch (InvalidDeserialization e) { e.printStackTrace(); } + if(tag.contains("icon")) + { + homeIcon = ItemStack.of(tag.getCompound("icon")); + } else homeIcon = new ItemStack(Items.BLUE_BED); } public CompoundTag serialize() @@ -37,6 +46,7 @@ public class Home { tag.putUUID("owner", owner); tag.putString("name", homeName); tag.put("dest", destination.serialize()); + tag.put("icon", homeIcon.serializeNBT()); return tag; } diff --git a/src/main/java/dev/zontreck/essentials/homes/Homes.java b/src/main/java/dev/zontreck/essentials/homes/Homes.java index 66a71af..08c3a72 100644 --- a/src/main/java/dev/zontreck/essentials/homes/Homes.java +++ b/src/main/java/dev/zontreck/essentials/homes/Homes.java @@ -7,6 +7,7 @@ import java.util.Map; import dev.zontreck.essentials.events.HomeCreatedEvent; import dev.zontreck.essentials.events.HomeDeletedEvent; +import dev.zontreck.essentials.exceptions.NoSuchHomeException; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; import net.minecraft.nbt.Tag; diff --git a/src/main/java/dev/zontreck/essentials/networking/ModMessages.java b/src/main/java/dev/zontreck/essentials/networking/ModMessages.java index 2a4fad2..410904f 100644 --- a/src/main/java/dev/zontreck/essentials/networking/ModMessages.java +++ b/src/main/java/dev/zontreck/essentials/networking/ModMessages.java @@ -1,6 +1,7 @@ package dev.zontreck.essentials.networking; import dev.zontreck.essentials.AriasEssentials; +import dev.zontreck.essentials.networking.packets.s2c.S2CUpdateHearts; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; import net.minecraftforge.network.NetworkDirection; diff --git a/src/main/java/dev/zontreck/essentials/networking/S2CUpdateHearts.java b/src/main/java/dev/zontreck/essentials/networking/packets/s2c/S2CUpdateHearts.java similarity index 90% rename from src/main/java/dev/zontreck/essentials/networking/S2CUpdateHearts.java rename to src/main/java/dev/zontreck/essentials/networking/packets/s2c/S2CUpdateHearts.java index 6d79147..744802a 100644 --- a/src/main/java/dev/zontreck/essentials/networking/S2CUpdateHearts.java +++ b/src/main/java/dev/zontreck/essentials/networking/packets/s2c/S2CUpdateHearts.java @@ -1,4 +1,4 @@ -package dev.zontreck.essentials.networking; +package dev.zontreck.essentials.networking.packets.s2c; import dev.zontreck.essentials.configs.AEClientConfig; import dev.zontreck.libzontreck.networking.packets.IPacket; @@ -36,6 +36,7 @@ public class S2CUpdateHearts ctx.enqueueWork(()->{ AEClientConfig.ENABLE_HEARTS_RENDER.set(current); + AEClientConfig.ENABLE_HEARTS_RENDER.save(); }); return true; diff --git a/src/main/java/dev/zontreck/essentials/rtp/RTP.java b/src/main/java/dev/zontreck/essentials/rtp/RTP.java index 5239080..71cabe8 100644 --- a/src/main/java/dev/zontreck/essentials/rtp/RTP.java +++ b/src/main/java/dev/zontreck/essentials/rtp/RTP.java @@ -52,7 +52,7 @@ public class RTP public WorldPosition position; private List BLACKLIST = Lists.of(Blocks.LAVA, Blocks.WATER, Blocks.BEDROCK); protected int tries; - protected int lastThreadDelay = 1; + protected int lastThreadDelay = 15; protected RTP withThreadDelay(int delay) { @@ -136,53 +136,62 @@ public class RTP moveUp(); } } - public void newPosition() { - if(!AriasEssentials.ALIVE)return; - containingThread=Thread.currentThread(); - if(tries>=30)return; + if (!AriasEssentials.ALIVE || tries >= 25) return; + + containingThread = Thread.currentThread(); AriasEssentials.LOGGER.info("RTP starts looking for new position"); + Random rng = new Random(Instant.now().getEpochSecond()); - Vector3 pos = new Vector3(rng.nextDouble(0xFFFF), 0, rng.nextDouble(0xFFFF)); - BlockPos bpos = pos.asBlockPos(); - position.getActualDimension().getChunk(bpos.getX() >> 4, bpos.getZ() >> 4, ChunkStatus.SPAWN); - pos = new Vector3( - position.getActualDimension().getHeightmapPos(heightMapType, pos.asBlockPos())); - while (!position.getActualDimension().getWorldBorder().isWithinBounds(pos.asBlockPos())) { - pos = new Vector3(rng.nextDouble(0xffff), 0, rng.nextDouble(0xffff)); + + Vector3 pos; + BlockPos bpos; + + do { + pos = new Vector3(rng.nextDouble(0xFFFF), 150, rng.nextDouble(0xFFFF)); + + pos = spiralPositions(pos); + position.Position = pos; bpos = pos.asBlockPos(); - position.getActualDimension().getChunk(bpos.getX() >> 4, bpos.getZ() >> 4, ChunkStatus.SPAWN); - pos = new Vector3( - position.getActualDimension().getHeightmapPos(heightMapType, pos.asBlockPos())); - } + } while (!isValidPosition(bpos)); - position.Position = pos; - if (pos.y < -60) { + if (pos.y < -30 || pos.y >= position.getActualDimension().getLogicalHeight()) { newPosition(); return; } - if (pos.y >= position.getActualDimension().getLogicalHeight()) { - spiralPositions(pos); - } - tries++; AriasEssentials.LOGGER.info("RTP returns new position"); } - private void spiralPositions(Vector3 position) - { + private boolean isValidPosition(BlockPos bpos) { + ServerLevel dimension = position.getActualDimension(); + ChunkStatus status = ChunkStatus.SPAWN; + + dimension.getChunk(bpos.getX() >> 4, bpos.getZ() >> 4, status); + + Vector3 pos = new Vector3(dimension.getHeightmapPos(heightMapType, bpos)); + return dimension.getWorldBorder().isWithinBounds(pos.asBlockPos()); + } + + + private Vector3 spiralPositions(Vector3 position) { Vec3i posi = position.asMinecraftVec3i(); - for(BlockPos pos : BlockPos.spiralAround(new BlockPos(posi.getX(), this.position.getActualDimension().getSeaLevel(), posi.getZ()), 16, Direction.WEST, Direction.NORTH)){ - if(isSafe(pos)){ + ServerLevel dimension = this.position.getActualDimension(); + BlockPos startBlockPos = new BlockPos(posi.getX(), dimension.getSeaLevel(), posi.getZ()); + + for (BlockPos pos : BlockPos.spiralAround(startBlockPos, 16, Direction.WEST, Direction.NORTH)) { + if (isSafe(pos)) { // Set the new position - this.position.Position = new Vector3(pos); - return; + return new Vector3(pos); } } + + return position; } + private boolean safe(BlockPos blockPos) { containingThread=Thread.currentThread(); diff --git a/src/main/java/dev/zontreck/essentials/rtp/RTPCachesEventHandlers.java b/src/main/java/dev/zontreck/essentials/rtp/RTPCachesEventHandlers.java index b7e059d..68a5260 100644 --- a/src/main/java/dev/zontreck/essentials/rtp/RTPCachesEventHandlers.java +++ b/src/main/java/dev/zontreck/essentials/rtp/RTPCachesEventHandlers.java @@ -1,10 +1,12 @@ package dev.zontreck.essentials.rtp; +import dev.zontreck.essentials.AriasEssentials; import dev.zontreck.essentials.Messages; import dev.zontreck.essentials.commands.teleport.TeleportActioner; import dev.zontreck.essentials.events.RTPFoundEvent; import dev.zontreck.libzontreck.util.ChatHelpers; import net.minecraft.server.level.ServerLevel; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; @@ -15,8 +17,9 @@ public class RTPCachesEventHandlers @SubscribeEvent public void onTick(TickEvent.ServerTickEvent event) { + if(!AriasEssentials.ALIVE) return; lastTick++; - if(lastTick>=40) + if(lastTick>=400) { lastTick=0; @@ -24,16 +27,25 @@ public class RTPCachesEventHandlers { try { + MinecraftForge.EVENT_BUS.unregister(this); + firstRun=false; + AriasEssentials.LOGGER.info("Aria's Essentials startup is running. Scanning for initial RTP locations"); for(ServerLevel level : event.getServer().getAllLevels()) { + if(AriasEssentials.DEBUG) + { + AriasEssentials.LOGGER.info("Scanning a level"); + } if(TeleportActioner.isBlacklistedDimension(level)) { continue; } - RandomPositionFactory.beginRTPSearch(level); // Populate 10 RTP points + RandomPositionFactory.beginRTPSearch(level); } + + AriasEssentials.LOGGER.info("Startup done. RTP searching will continue in a separate thread"); }catch (Exception e) { e.printStackTrace(); @@ -43,7 +55,7 @@ public class RTPCachesEventHandlers } @SubscribeEvent - public void onRTPFound(RTPFoundEvent event) + public static void onRTPFound(final RTPFoundEvent event) { RTPCaches.Locations.add(event.rtp); ChatHelpers.broadcast(ChatHelpers.macro(Messages.RTP_CACHED, event.rtp.position.Dimension), event.rtp.position.getActualDimension().getServer()); diff --git a/src/main/java/dev/zontreck/essentials/rtp/RTPContainer.java b/src/main/java/dev/zontreck/essentials/rtp/RTPContainer.java deleted file mode 100644 index 109b3a8..0000000 --- a/src/main/java/dev/zontreck/essentials/rtp/RTPContainer.java +++ /dev/null @@ -1,37 +0,0 @@ -package dev.zontreck.essentials.rtp; - -import java.time.Instant; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.stream.Stream; - -import dev.zontreck.essentials.AriasEssentials; -import dev.zontreck.essentials.commands.teleport.TeleportContainer; -import dev.zontreck.libzontreck.vectors.Vector3; -import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; -import net.minecraft.core.BlockPos.MutableBlockPos; -import net.minecraft.core.Vec3i; -import net.minecraft.server.level.ServerLevel; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.level.BlockGetter; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.Blocks; -import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.level.chunk.ChunkStatus; -import net.minecraft.world.level.levelgen.Heightmap; -import net.minecraft.world.level.levelgen.Heightmap.Types; -import net.minecraft.world.phys.AABB; -import net.minecraft.world.phys.Vec2; -import net.minecraft.world.phys.Vec3; - -@Deprecated -/** - * @see RTP instead - */ -public class RTPContainer { -} diff --git a/src/main/java/dev/zontreck/essentials/rtp/RandomPositionLocator.java b/src/main/java/dev/zontreck/essentials/rtp/RandomPositionLocator.java index 8e35d7b..e61f6a8 100644 --- a/src/main/java/dev/zontreck/essentials/rtp/RandomPositionLocator.java +++ b/src/main/java/dev/zontreck/essentials/rtp/RandomPositionLocator.java @@ -8,73 +8,44 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.chunk.ChunkAccess; import net.minecraftforge.common.MinecraftForge; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; /** - * This class aims to serve as the Random Position Locate system - * It aims to be as non-thread blocking as possible to avoid server lag - * - * To utilize, initialize a RTPContainer from the RandomPositionFactory and execute from there. + * This class serves as the Random Position Locate system, aiming to be non-thread blocking for improved server performance. + * To utilize, initialize an RTP from the RandomPositionFactory and execute from there. */ -public class RandomPositionLocator extends Task -{ +public class RandomPositionLocator extends Task { + private static final Logger log = LogManager.getLogger("RPL-"+Thread.currentThread().getName()); private final RTP contain; - public RandomPositionLocator(RTP rtp) - { - super("RPL",true); - contain=rtp; + /** + * Constructs a RandomPositionLocator with the specified RTP instance. + * + * @param rtp The RTP instance to use. + */ + public RandomPositionLocator(RTP rtp) { + super("RPL", true); + contain = rtp; } @Override public void run() { - if(!AriasEssentials.ALIVE)return; - - //ChatHelpers.broadcastTo(contain.container.PlayerInst.getUUID(), ChatHelpers.macro(Messages.RTP_SEARCHING, String.valueOf(contain.tries), "30"), contain.container.PlayerInst.server); + if (!AriasEssentials.ALIVE) return; - ServerLevel levl = contain.position.getActualDimension(); - ChunkAccess chunk = levl.getChunk(contain.position.Position.asBlockPos()); - ChunkPos cpos = chunk.getPos(); - boolean needsLoading = false; - needsLoading = !(levl.getForcedChunks().contains(cpos.toLong())); - - - if(needsLoading) - levl.setChunkForced(cpos.x, cpos.z, true); - - int curChecks=0; - while(curChecks<3) - { - if(contain.isSafe(contain.position.Position.asBlockPos())) - { - if(needsLoading) - levl.setChunkForced(cpos.x, cpos.z, false); - - MinecraftForge.EVENT_BUS.post(new RTPFoundEvent(contain)); - - return; - } else { - curChecks++; - contain.move(); - //AriasEssentials.LOGGER.info("[DEBUG] "+ChatColor.doColors("!Dark_Red!Checking position: "+contain.container.world_pos.Position.toString()+"; "+contain.container.Dimension.getBlockState(contain.container.world_pos.Position.asBlockPos()).getBlock().toString()+"; "+contain.container.Dimension.getBlockState(contain.container.world_pos.Position.asBlockPos().below()).getBlock().toString())); - } + if (AriasEssentials.DEBUG) { + log.debug("RTP Search thread"); } - if(needsLoading) - levl.setChunkForced(cpos.x, cpos.z, false); - + contain.newPosition(); - if(contain.tries > 30) + if(AriasEssentials.DEBUG) { - // Abort - return; - }else { - // Schedule the task to execute - //run(); - - RandomPositionLocator next = new RandomPositionLocator(contain.withThreadDelay(contain.lastThreadDelay+1)); - DelayedExecutorService.getInstance().schedule(next, contain.lastThreadDelay+1); - AriasEssentials.LOGGER.info("Giving up current RTP search. Scheduling another search in 1 second"); + log.debug("Dispatching RTPFoundEvent - " + contain.position.Dimension); } + + contain.position.getActualDimension().getServer().execute(()->{ + MinecraftForge.EVENT_BUS.post(new RTPFoundEvent(contain)); + }); } - } diff --git a/src/main/java/dev/zontreck/essentials/warps/Warp.java b/src/main/java/dev/zontreck/essentials/warps/Warp.java index 224bc23..61225e1 100644 --- a/src/main/java/dev/zontreck/essentials/warps/Warp.java +++ b/src/main/java/dev/zontreck/essentials/warps/Warp.java @@ -8,12 +8,18 @@ import dev.zontreck.essentials.commands.teleport.TeleportDestination; import dev.zontreck.essentials.events.WarpAccessControlListUpdatedEvent; import dev.zontreck.essentials.warps.AccessControlList.ACLEntry; import dev.zontreck.libzontreck.exceptions.InvalidDeserialization; +import dev.zontreck.libzontreck.profiles.Profile; +import dev.zontreck.libzontreck.profiles.UserProfileNotYetExistsException; +import dev.zontreck.libzontreck.util.ServerUtilities; +import dev.zontreck.libzontreck.util.heads.HeadUtilities; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.IntArrayTag; import net.minecraft.nbt.ListTag; import net.minecraft.nbt.NbtUtils; import net.minecraft.nbt.Tag; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; import net.minecraftforge.common.MinecraftForge; public class Warp { @@ -24,7 +30,9 @@ public class Warp { public TeleportDestination destination; private AccessControlList ACL; - public Warp(UUID owner, String name, boolean rtp, boolean publicWarp, TeleportDestination destination) + public ItemStack warpIcon; + + public Warp(UUID owner, String name, boolean rtp, boolean publicWarp, TeleportDestination destination, ItemStack warpIcon) { this.owner=owner; WarpName=name; @@ -32,11 +40,23 @@ public class Warp { isPublic=publicWarp; this.destination=destination; this.ACL = new AccessControlList(); + + if(warpIcon==null) + { + try { + Profile prof = Profile.get_profile_of(owner.toString()); + this.warpIcon = HeadUtilities.get(prof.username, name); + + } catch (UserProfileNotYetExistsException e) { + throw new RuntimeException(e); + } + + }else this.warpIcon=warpIcon; } public static Warp deserialize(CompoundTag tag) throws InvalidDeserialization { - Warp warp = new Warp(tag.getUUID("owner"), tag.getString("name"), tag.getBoolean("rtp"), tag.getBoolean("public"), new TeleportDestination(tag.getCompound("destination"))); + Warp warp = new Warp(tag.getUUID("owner"), tag.getString("name"), tag.getBoolean("rtp"), tag.getBoolean("public"), new TeleportDestination(tag.getCompound("destination")), ItemStack.of(tag.getCompound("icon"))); if(!warp.isPublic) { @@ -54,6 +74,7 @@ public class Warp { tag.putBoolean("rtp", RTP); tag.putBoolean("public", isPublic); tag.put("destination", destination.serialize()); + tag.put("icon", warpIcon.serializeNBT()); if(!isPublic) { tag.put("acl", ACL.serialize());