From 19fff5778dc6ece3cd58a49e71e088d2276de1ac Mon Sep 17 00:00:00 2001 From: zontreck Date: Mon, 18 Dec 2023 17:28:04 -0700 Subject: [PATCH] Get cached RTPs to work properly --- .../zontreck/essentials/AriasEssentials.java | 4 ++ .../dev/zontreck/essentials/Messages.java | 3 + .../java/dev/zontreck/essentials/rtp/RTP.java | 72 ++----------------- .../rtp/RTPCachesEventHandlers.java | 27 ++++++- .../essentials/rtp/RandomPositionLocator.java | 3 +- 5 files changed, 38 insertions(+), 71 deletions(-) diff --git a/src/main/java/dev/zontreck/essentials/AriasEssentials.java b/src/main/java/dev/zontreck/essentials/AriasEssentials.java index 83cab70..ce622a0 100644 --- a/src/main/java/dev/zontreck/essentials/AriasEssentials.java +++ b/src/main/java/dev/zontreck/essentials/AriasEssentials.java @@ -18,7 +18,9 @@ import dev.zontreck.essentials.gui.HeartsRenderer; import dev.zontreck.essentials.networking.ModMessages; import dev.zontreck.essentials.networking.S2CUpdateHearts; import dev.zontreck.essentials.rtp.RTPCachesEventHandlers; +import dev.zontreck.essentials.rtp.RandomPositionFactory; import dev.zontreck.libzontreck.events.RegisterPacketsEvent; +import net.minecraft.server.level.ServerLevel; import net.minecraftforge.fml.config.ModConfig; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import org.slf4j.Logger; @@ -92,6 +94,8 @@ public class AriasEssentials { public void onServerStart(final ServerStartedEvent ev) { ALIVE=true; + + } diff --git a/src/main/java/dev/zontreck/essentials/Messages.java b/src/main/java/dev/zontreck/essentials/Messages.java index fdd7857..4e8f96e 100644 --- a/src/main/java/dev/zontreck/essentials/Messages.java +++ b/src/main/java/dev/zontreck/essentials/Messages.java @@ -69,6 +69,7 @@ public class Messages { public static final String HEARTS_USAGE; public static final String HEARTS_UPDATED; public static final String RESPAWNING; + public static final String RTP_CACHED; static{ @@ -146,5 +147,7 @@ public class Messages { HEARTS_UPDATED = ESSENTIALS_PREFIX + "!Dark_Red!Your hearts preferences have been updated"; RESPAWNING = ESSENTIALS_PREFIX + "!Dark_Green!Respawning at World Spawn"; + + RTP_CACHED = ESSENTIALS_PREFIX + "!Dark_Green!A new RTP location has been cached for [0]"; } } diff --git a/src/main/java/dev/zontreck/essentials/rtp/RTP.java b/src/main/java/dev/zontreck/essentials/rtp/RTP.java index 8a1e5c6..6656de9 100644 --- a/src/main/java/dev/zontreck/essentials/rtp/RTP.java +++ b/src/main/java/dev/zontreck/essentials/rtp/RTP.java @@ -35,7 +35,6 @@ public class RTP { public RTP(ServerLevel level) { - Age = Instant.now().getEpochSecond(); position = new WorldPosition(new Vector3(0,500,0), WorldPosition.getDim(level)); if(position.getActualDimension().dimensionType().hasCeiling()) @@ -53,10 +52,6 @@ public class RTP public WorldPosition position; private List BLACKLIST = Lists.of(Blocks.LAVA, Blocks.WATER); protected int tries; - /** - * This is a unix timestamp, that is checked for being Stale - */ - public long Age; public boolean isDimension(ServerLevel level) { @@ -67,23 +62,6 @@ public class RTP }else return false; } - /** - * Checks if the RTP Cached position is stale. This means over 2 hours old - * @return True if stale - */ - public boolean isStale() - { - if((Age+(2*60*60)) < Instant.now().getEpochSecond()) - { - return true; - } else return false; - } - - public boolean readyForNewer() - { - return ((Age + (10*60)) < Instant.now().getEpochSecond()); - } - /** * Searches for, and finds, a valid RTP location * @param level The level to scan @@ -116,7 +94,10 @@ public class RTP List slice = slicedByDimension(level); if(slice.size()>0) { - return slice.get(AriasEssentials.random.nextInt(0, slice.size())); + RTP ret = slice.get(AriasEssentials.random.nextInt(0, slice.size())); + RTPCaches.Locations.remove(ret); + RandomPositionFactory.beginRTPSearch(ret.position.getActualDimension()); + return ret; } else return null; } @@ -222,49 +203,4 @@ public class RTP return s; } - - public void putAge() - { - Age = Instant.now().getEpochSecond(); - } - - public static void checkStale() - { - Iterator it = RTPCaches.Locations.iterator(); - List uniqueDims = new ArrayList<>(); - while(it.hasNext()) - { - RTP loc = it.next(); - if(loc.isStale()){ - it.remove(); - } - - if(!uniqueDims.contains(loc.position.getActualDimension())) - { - uniqueDims.add(loc.position.getActualDimension()); - } - } - - checkNeedsNewer(uniqueDims); - } - - public static void checkNeedsNewer(List dims) - { - Iterator it = dims.iterator(); - while(it.hasNext()) - { - ServerLevel lvl = it.next(); - List slice = slicedByDimension(lvl); - boolean needsNewer = true; - for(var X : slice) - { - if(!X.readyForNewer()) needsNewer=false; - } - - if(needsNewer) - { - RandomPositionFactory.beginRTPSearch(lvl); - } - } - } } diff --git a/src/main/java/dev/zontreck/essentials/rtp/RTPCachesEventHandlers.java b/src/main/java/dev/zontreck/essentials/rtp/RTPCachesEventHandlers.java index d39f888..b7e059d 100644 --- a/src/main/java/dev/zontreck/essentials/rtp/RTPCachesEventHandlers.java +++ b/src/main/java/dev/zontreck/essentials/rtp/RTPCachesEventHandlers.java @@ -1,12 +1,17 @@ package dev.zontreck.essentials.rtp; +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.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; public class RTPCachesEventHandlers { public int lastTick; + public boolean firstRun=true; @SubscribeEvent public void onTick(TickEvent.ServerTickEvent event) { @@ -14,7 +19,26 @@ public class RTPCachesEventHandlers if(lastTick>=40) { lastTick=0; - RTP.checkStale(); + + if(firstRun) + { + try { + + firstRun=false; + for(ServerLevel level : event.getServer().getAllLevels()) + { + if(TeleportActioner.isBlacklistedDimension(level)) + { + continue; + } + + RandomPositionFactory.beginRTPSearch(level); // Populate 10 RTP points + } + }catch (Exception e) + { + e.printStackTrace(); + } + } } } @@ -22,5 +46,6 @@ public class RTPCachesEventHandlers public void onRTPFound(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/RandomPositionLocator.java b/src/main/java/dev/zontreck/essentials/rtp/RandomPositionLocator.java index a9d4a9a..4f2b88b 100644 --- a/src/main/java/dev/zontreck/essentials/rtp/RandomPositionLocator.java +++ b/src/main/java/dev/zontreck/essentials/rtp/RandomPositionLocator.java @@ -42,11 +42,10 @@ public class RandomPositionLocator extends Task levl.setChunkForced(cpos.x, cpos.z, true); int curChecks=0; - while(curChecks<10) + while(curChecks<3) { if(contain.isSafe(contain.position.Position.asBlockPos())) { - contain.putAge(); if(needsLoading) levl.setChunkForced(cpos.x, cpos.z, false);