Merge updates from 1.19.2 to 1.20.1

This commit is contained in:
Zontreck 2024-02-14 00:44:04 -07:00
parent ed20b4f5fb
commit fa67381990
13 changed files with 118 additions and 197 deletions

View file

@ -8,6 +8,7 @@ import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
@ -20,124 +21,87 @@ import java.util.Iterator;
import java.util.List;
import java.util.Random;
/**
* As of v1.1.121823.x, this is now used for RTP
* <p>
* This ensures that RTP is only scanned once every so often. This is because RTP lags the server when it is having to check block positions.
* <p>
* If the RTP system scans each dimension (Not blacklisted), then it can build a list of safe locations that will be rotated out every 2 hours.
* <p>
* Every 10 minutes, a new RTP location is scanned. This ensures sufficiently semi-random locations. Eventually old locations will be removed from the list.
* <p>
* At server start, it will scan 10 RTP locations per dimension while there are no players.
*/
public class RTP
{
public RTP(ServerLevel level)
{
position = new WorldPosition(new Vector3(0,500,0), WorldPosition.getDim(level));
if(position.getActualDimension().dimensionType().hasCeiling())
{
heightMapType = Heightmap.Types.MOTION_BLOCKING_NO_LEAVES;
SearchDirection=-1;
}else {
heightMapType = Heightmap.Types.MOTION_BLOCKING_NO_LEAVES;
SearchDirection=1;
}
}
private final int SearchDirection;
private Thread containingThread;
public class RTP {
private static final List<Block> BLACKLIST = Lists.of(Blocks.LAVA, Blocks.WATER, Blocks.BEDROCK);
private final int SEARCH_DIRECTION;
private final Heightmap.Types heightMapType;
public WorldPosition position;
private final List<Block> BLACKLIST = Lists.of(Blocks.LAVA, Blocks.WATER, Blocks.BEDROCK);
protected int tries;
protected int lastThreadDelay = 15;
private final ServerLevel dimension;
private int tries;
protected RTP withThreadDelay(int delay)
{
lastThreadDelay=delay;
if(lastThreadDelay >= 60) lastThreadDelay = 60;
return this;
public RTP(ServerLevel level) {
position = new WorldPosition(new Vector3(0, -60, 0), WorldPosition.getDim(level));
dimension = position.getActualDimension();
if (position.getActualDimension().dimensionType().hasCeiling()) {
heightMapType = Heightmap.Types.MOTION_BLOCKING_NO_LEAVES;
SEARCH_DIRECTION = -1;
} else {
heightMapType = Heightmap.Types.MOTION_BLOCKING_NO_LEAVES;
SEARCH_DIRECTION = 1;
}
}
public boolean isDimension(ServerLevel level)
{
public boolean isDimension(ServerLevel level) {
String dim = WorldPosition.getDim(level);
return dim.equals(position.Dimension);
}
/**
* Searches for, and finds, a valid RTP location
* @param level The level to scan
* @return RTP data
*/
public static void find(ServerLevel level)
{
RandomPositionFactory.beginRTPSearch(level);
}
public BlockPos findSafeLandingLocation() {
BlockPos targetPos = position.Position.asBlockPos();
public static List<RTP> slicedByDimension(ServerLevel lvl)
{
List<RTP> slice = new ArrayList<>();
Iterator<RTP> it = RTPCaches.Locations.iterator();
while(it.hasNext())
{
RTP nxt = it.next();
if(nxt.isDimension(lvl))
{
slice.add(nxt);
}
// Search upward for a safe landing location
while (!isSafe(targetPos) || !isSafe(targetPos.above())) {
targetPos = targetPos.above();
}
return slice;
return targetPos;
}
public static RTP getRTP(ServerLevel level)
{
private boolean isSafe(BlockPos blockPos) {
BlockState blockState = dimension.getBlockState(blockPos);
BlockState blockStateAbove = dimension.getBlockState(blockPos.above());
BlockState blockStateBelow = dimension.getBlockState(blockPos.below());
if (blockState.isAir() && blockStateAbove.isAir()) {
if (!blockStateBelow.isAir()) {
return !BLACKLIST.contains(blockStateBelow.getBlock());
} else {
return false;
}
} else {
return false;
}
}
public static RTP getRTP(ServerLevel level) {
List<RTP> slice = slicedByDimension(level);
if(slice.size()>0)
{
RTP ret = slice.get(AriasEssentials.random.nextInt(0, slice.size()));
if (!slice.isEmpty()) {
RTP ret = slice.get(AriasEssentials.random.nextInt(slice.size()));
RTPCaches.Locations.remove(ret);
RandomPositionFactory.beginRTPSearch(ret.position.getActualDimension());
return ret;
} else return null;
}
public void moveDown() {
position.Position = position.Position.moveDown();
}
public void moveUp() {
position.Position = position.Position.moveUp();
}
public void move()
{
if(SearchDirection==1){
moveUp();
}else if(SearchDirection==0)
{
moveDown();
} else {
return null;
}
}
public void moveOpposite()
{
if(SearchDirection==1){
moveDown();
}else if(SearchDirection==0)
{
moveUp();
public void move() {
if (SEARCH_DIRECTION == 1) {
position.Position = position.Position.moveUp();
} else if (SEARCH_DIRECTION == -1) {
position.Position = position.Position.moveDown();
}
}
public void moveOpposite() {
move();
}
public void newPosition() {
if (!AriasEssentials.ALIVE || tries >= 25) return;
if (!AriasEssentials.ALIVE || tries >= 5) return;
containingThread = Thread.currentThread();
AriasEssentials.LOGGER.info("RTP starts looking for new position");
AriasEssentials.LOGGER.info("RTP starts looking for a new position");
Random rng = new Random(Instant.now().getEpochSecond());
@ -145,37 +109,27 @@ public class RTP
BlockPos bpos;
do {
pos = new Vector3(rng.nextDouble(0xFFFF), 150, rng.nextDouble(0xFFFF));
pos = new Vector3(rng.nextDouble(0xFFFF), -60, rng.nextDouble(0xFFFF));
pos = spiralPositions(pos);
position.Position = pos;
bpos = pos.asBlockPos();
} while (!isValidPosition(bpos));
if (pos.y < -30 || pos.y >= position.getActualDimension().getLogicalHeight()) {
newPosition();
return;
}
tries++;
AriasEssentials.LOGGER.info("RTP returns new position");
AriasEssentials.LOGGER.info("RTP returns a new 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();
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)) {
@ -188,32 +142,17 @@ public class RTP
return position;
}
public static List<RTP> slicedByDimension(ServerLevel lvl) {
List<RTP> slice = new ArrayList<>();
private boolean safe(BlockPos blockPos)
{
containingThread=Thread.currentThread();
BlockState b = position.getActualDimension().getBlockState(blockPos);
BlockState b2 = position.getActualDimension().getBlockState(blockPos.above());
BlockState b3 = position.getActualDimension().getBlockState(blockPos.below());
Iterator<RTP> it = RTPCaches.Locations.iterator();
while (it.hasNext()) {
RTP nxt = it.next();
if (nxt.isDimension(lvl)) {
slice.add(nxt);
}
}
if (b.isAir() && b2.isAir()) {
if (!b3.isAir()) {
return !BLACKLIST.contains(b3.getBlock());
} else
return false;
} else
return false;
}
public boolean isSafe(BlockPos blockPos) {
return safe(blockPos);
/*
boolean s = safe(blockPos);
if(s)
{
AriasEssentials.LOGGER.info("/!\\ SAFE /!\\");
}else AriasEssentials.LOGGER.info("/!\\ NOT SAFE /!\\");
return s;*/
return slice;
}
}