Get cached RTPs to work properly
This commit is contained in:
parent
3a8ce9b989
commit
19fff5778d
5 changed files with 38 additions and 71 deletions
|
@ -18,7 +18,9 @@ import dev.zontreck.essentials.gui.HeartsRenderer;
|
||||||
import dev.zontreck.essentials.networking.ModMessages;
|
import dev.zontreck.essentials.networking.ModMessages;
|
||||||
import dev.zontreck.essentials.networking.S2CUpdateHearts;
|
import dev.zontreck.essentials.networking.S2CUpdateHearts;
|
||||||
import dev.zontreck.essentials.rtp.RTPCachesEventHandlers;
|
import dev.zontreck.essentials.rtp.RTPCachesEventHandlers;
|
||||||
|
import dev.zontreck.essentials.rtp.RandomPositionFactory;
|
||||||
import dev.zontreck.libzontreck.events.RegisterPacketsEvent;
|
import dev.zontreck.libzontreck.events.RegisterPacketsEvent;
|
||||||
|
import net.minecraft.server.level.ServerLevel;
|
||||||
import net.minecraftforge.fml.config.ModConfig;
|
import net.minecraftforge.fml.config.ModConfig;
|
||||||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -92,6 +94,8 @@ public class AriasEssentials {
|
||||||
public void onServerStart(final ServerStartedEvent ev)
|
public void onServerStart(final ServerStartedEvent ev)
|
||||||
{
|
{
|
||||||
ALIVE=true;
|
ALIVE=true;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -69,6 +69,7 @@ public class Messages {
|
||||||
public static final String HEARTS_USAGE;
|
public static final String HEARTS_USAGE;
|
||||||
public static final String HEARTS_UPDATED;
|
public static final String HEARTS_UPDATED;
|
||||||
public static final String RESPAWNING;
|
public static final String RESPAWNING;
|
||||||
|
public static final String RTP_CACHED;
|
||||||
|
|
||||||
|
|
||||||
static{
|
static{
|
||||||
|
@ -146,5 +147,7 @@ public class Messages {
|
||||||
HEARTS_UPDATED = ESSENTIALS_PREFIX + "!Dark_Red!Your hearts preferences have been updated";
|
HEARTS_UPDATED = ESSENTIALS_PREFIX + "!Dark_Red!Your hearts preferences have been updated";
|
||||||
|
|
||||||
RESPAWNING = ESSENTIALS_PREFIX + "!Dark_Green!Respawning at World Spawn";
|
RESPAWNING = ESSENTIALS_PREFIX + "!Dark_Green!Respawning at World Spawn";
|
||||||
|
|
||||||
|
RTP_CACHED = ESSENTIALS_PREFIX + "!Dark_Green!A new RTP location has been cached for [0]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,6 @@ public class RTP
|
||||||
{
|
{
|
||||||
public RTP(ServerLevel level)
|
public RTP(ServerLevel level)
|
||||||
{
|
{
|
||||||
Age = Instant.now().getEpochSecond();
|
|
||||||
position = new WorldPosition(new Vector3(0,500,0), WorldPosition.getDim(level));
|
position = new WorldPosition(new Vector3(0,500,0), WorldPosition.getDim(level));
|
||||||
|
|
||||||
if(position.getActualDimension().dimensionType().hasCeiling())
|
if(position.getActualDimension().dimensionType().hasCeiling())
|
||||||
|
@ -53,10 +52,6 @@ public class RTP
|
||||||
public WorldPosition position;
|
public WorldPosition position;
|
||||||
private List<Block> BLACKLIST = Lists.of(Blocks.LAVA, Blocks.WATER);
|
private List<Block> BLACKLIST = Lists.of(Blocks.LAVA, Blocks.WATER);
|
||||||
protected int tries;
|
protected int tries;
|
||||||
/**
|
|
||||||
* This is a unix timestamp, that is checked for being Stale
|
|
||||||
*/
|
|
||||||
public long Age;
|
|
||||||
|
|
||||||
public boolean isDimension(ServerLevel level)
|
public boolean isDimension(ServerLevel level)
|
||||||
{
|
{
|
||||||
|
@ -67,23 +62,6 @@ public class RTP
|
||||||
}else return false;
|
}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
|
* Searches for, and finds, a valid RTP location
|
||||||
* @param level The level to scan
|
* @param level The level to scan
|
||||||
|
@ -116,7 +94,10 @@ public class RTP
|
||||||
List<RTP> slice = slicedByDimension(level);
|
List<RTP> slice = slicedByDimension(level);
|
||||||
if(slice.size()>0)
|
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;
|
} else return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,49 +203,4 @@ public class RTP
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void putAge()
|
|
||||||
{
|
|
||||||
Age = Instant.now().getEpochSecond();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void checkStale()
|
|
||||||
{
|
|
||||||
Iterator<RTP> it = RTPCaches.Locations.iterator();
|
|
||||||
List<ServerLevel> 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<ServerLevel> dims)
|
|
||||||
{
|
|
||||||
Iterator<ServerLevel> it = dims.iterator();
|
|
||||||
while(it.hasNext())
|
|
||||||
{
|
|
||||||
ServerLevel lvl = it.next();
|
|
||||||
List<RTP> slice = slicedByDimension(lvl);
|
|
||||||
boolean needsNewer = true;
|
|
||||||
for(var X : slice)
|
|
||||||
{
|
|
||||||
if(!X.readyForNewer()) needsNewer=false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(needsNewer)
|
|
||||||
{
|
|
||||||
RandomPositionFactory.beginRTPSearch(lvl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,17 @@
|
||||||
package dev.zontreck.essentials.rtp;
|
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.essentials.events.RTPFoundEvent;
|
||||||
|
import dev.zontreck.libzontreck.util.ChatHelpers;
|
||||||
|
import net.minecraft.server.level.ServerLevel;
|
||||||
import net.minecraftforge.event.TickEvent;
|
import net.minecraftforge.event.TickEvent;
|
||||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
|
|
||||||
public class RTPCachesEventHandlers
|
public class RTPCachesEventHandlers
|
||||||
{
|
{
|
||||||
public int lastTick;
|
public int lastTick;
|
||||||
|
public boolean firstRun=true;
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public void onTick(TickEvent.ServerTickEvent event)
|
public void onTick(TickEvent.ServerTickEvent event)
|
||||||
{
|
{
|
||||||
|
@ -14,7 +19,26 @@ public class RTPCachesEventHandlers
|
||||||
if(lastTick>=40)
|
if(lastTick>=40)
|
||||||
{
|
{
|
||||||
lastTick=0;
|
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)
|
public void onRTPFound(RTPFoundEvent event)
|
||||||
{
|
{
|
||||||
RTPCaches.Locations.add(event.rtp);
|
RTPCaches.Locations.add(event.rtp);
|
||||||
|
ChatHelpers.broadcast(ChatHelpers.macro(Messages.RTP_CACHED, event.rtp.position.Dimension), event.rtp.position.getActualDimension().getServer());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,11 +42,10 @@ public class RandomPositionLocator extends Task
|
||||||
levl.setChunkForced(cpos.x, cpos.z, true);
|
levl.setChunkForced(cpos.x, cpos.z, true);
|
||||||
|
|
||||||
int curChecks=0;
|
int curChecks=0;
|
||||||
while(curChecks<10)
|
while(curChecks<3)
|
||||||
{
|
{
|
||||||
if(contain.isSafe(contain.position.Position.asBlockPos()))
|
if(contain.isSafe(contain.position.Position.asBlockPos()))
|
||||||
{
|
{
|
||||||
contain.putAge();
|
|
||||||
if(needsLoading)
|
if(needsLoading)
|
||||||
levl.setChunkForced(cpos.x, cpos.z, false);
|
levl.setChunkForced(cpos.x, cpos.z, false);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue