Move stuff over from OTEMod

This commit is contained in:
Aria 2023-02-27 15:16:46 -07:00
parent fb71db187d
commit f27c76f334
40 changed files with 2036 additions and 5 deletions

View file

@ -0,0 +1,154 @@
package dev.zontreck.essentials.util;
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.libzontreck.vectors.Vector3;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos.MutableBlockPos;
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;
public class RTPContainer {
public TeleportContainer container;
public int tries = -1;
public boolean complete = false;
public boolean aborted=false;
public Thread containingThread;
private Types heightMapType;
private int SearchDirection;
public static final List<Block> BLACKLIST;
static {
List<Block> tmp = new ArrayList<>();
tmp.add(Blocks.LAVA);
tmp.add(Blocks.BEDROCK);
tmp.add(Blocks.WATER);
BLACKLIST = tmp;
}
public void moveDown() {
container.world_pos.Position = container.world_pos.Position.moveDown();
container.Position = container.world_pos.Position.asMinecraftVector();
}
public void moveUp() {
container.world_pos.Position = container.world_pos.Position.moveUp();
container.Position = container.world_pos.Position.asMinecraftVector();
}
public void move()
{
if(SearchDirection==1){
moveUp();
}else if(SearchDirection==0)
{
moveDown();
}
}
public void moveOpposite()
{
if(SearchDirection==1){
moveDown();
}else if(SearchDirection==0)
{
moveUp();
}
}
public void newPosition() {
if(!AriasEssentials.ALIVE)return;
containingThread=Thread.currentThread();
Random rng = new Random(Instant.now().getEpochSecond());
Vector3 pos = new Vector3(rng.nextDouble(0xFFFF), 0, rng.nextDouble(0xFFFF));
BlockPos bpos = pos.asBlockPos();
container.Dimension.getChunk(bpos.getX() >> 4, bpos.getZ() >> 4, ChunkStatus.HEIGHTMAPS);
pos = new Vector3(
container.Dimension.getHeightmapPos(heightMapType, pos.asBlockPos()));
while (!container.Dimension.getWorldBorder().isWithinBounds(pos.asBlockPos())) {
pos = new Vector3(rng.nextDouble(0xffff), 0, rng.nextDouble(0xffff));
bpos = pos.asBlockPos();
container.Dimension.getChunk(bpos.getX() >> 4, bpos.getZ() >> 4, ChunkStatus.HEIGHTMAPS);
pos = new Vector3(
container.Dimension.getHeightmapPos(heightMapType, pos.asBlockPos()));
}
container.world_pos.Position = pos;
container.Position = container.world_pos.Position.asMinecraftVector();
if (pos.y < -60) {
newPosition();
return;
}
if (pos.y >= container.Dimension.getLogicalHeight()) {
spiralPositions(pos);
}
tries++;
}
private void spiralPositions(Vector3 position)
{
for(BlockPos pos : BlockPos.spiralAround(new BlockPos(position.x, container.Dimension.getSeaLevel(), position.z), 16, Direction.WEST, Direction.NORTH)){
if(isSafe(pos)){
// Set the new position
container.world_pos.Position = new Vector3(pos);
container.Position = container.world_pos.Position.asMinecraftVector();
return;
}
}
}
protected RTPContainer(ServerPlayer player, Vec3 pos, Vec2 rot, ServerLevel level) {
container = new TeleportContainer(player, pos, rot, level);
if(container.Dimension.dimensionType().hasCeiling())
{
heightMapType = Heightmap.Types.MOTION_BLOCKING_NO_LEAVES;
SearchDirection=-1;
}else {
heightMapType = Types.MOTION_BLOCKING_NO_LEAVES;
SearchDirection=1;
}
newPosition();
}
public boolean isSafe(BlockPos blockPos) {
containingThread=Thread.currentThread();
BlockState b = container.Dimension.getBlockState(blockPos);
BlockState b2 = container.Dimension.getBlockState(blockPos.above());
BlockState b3 = container.Dimension.getBlockState(blockPos.below());
if (b.isAir() && b2.isAir()) {
if (!b3.isAir()) {
if (BLACKLIST.contains(b3.getBlock())) {
return false;
} else
return true;
} else
return false;
} else
return false;
}
}

View file

@ -0,0 +1,23 @@
package dev.zontreck.essentials.commands.teleport;
import dev.zontreck.essentials.implementation.DelayedExecutorService;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
/**
* The factory system used to start searching for a random teleport position
*/
public class RandomPositionFactory {
public static RTPContainer beginRTPSearch(ServerPlayer player, Vec3 pos, Vec2 rot, ServerLevel level)
{
RTPContainer contain= new RTPContainer(player, pos, rot, level);
Thread tx = new Thread(new RandomPositionLocator(contain));
tx.setName("RTPTask-"+String.valueOf(DelayedExecutorService.getNext()));
tx.start();
return contain;
}
}

View file

@ -0,0 +1,92 @@
package dev.zontreck.otemod.commands.teleport;
import java.util.Iterator;
import java.util.concurrent.ScheduledExecutorService;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.chat.ChatServerOverride;
import dev.zontreck.otemod.events.RTPEvent;
import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraftforge.common.MinecraftForge;
/**
* 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.
*/
public class RandomPositionLocator implements Runnable
{
private final RTPContainer contain;
public RandomPositionLocator(RTPContainer rtp)
{
contain=rtp;
}
@Override
public void run() {
if(!OTEMod.ALIVE)return;
ChatServerOverride.broadcastTo(contain.container.PlayerInst.getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors(" !Dark_Purple!Searching... Attempt "+String.valueOf(contain.tries)+"/30")), OTEMod.THE_SERVER);
ServerLevel levl = contain.container.Dimension;
ChunkAccess chunk = levl.getChunk(contain.container.world_pos.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<30)
{
if(contain.isSafe(contain.container.world_pos.Position.asBlockPos()))
{
contain.complete=true;
if(needsLoading)
levl.setChunkForced(cpos.x, cpos.z, false);
if(MinecraftForge.EVENT_BUS.post(new RTPEvent(contain.container.PlayerInst, contain.container.world_pos)))
{
contain.complete=false;
ChatServerOverride.broadcastTo(contain.container.PlayerInst.getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors(" !Dark_Red!Last position checked was probably claimed. Another mod has asked us not to send you to that location, continuing the search")), OTEMod.THE_SERVER);
break;
}
return;
}else {
curChecks++;
contain.move();
OTEMod.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(needsLoading)
levl.setChunkForced(cpos.x, cpos.z, false);
contain.newPosition();
if(contain.tries > 30)
{
// Abort
ChatServerOverride.broadcastTo(contain.container.PlayerInst.getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors(" !Dark_Red!Could not find a suitable location in 30 attempts")), OTEMod.THE_SERVER);
contain.aborted=true;
return;
}else {
// Schedule the task to execute
//run();
RandomPositionLocator next = new RandomPositionLocator(contain);
OTEMod.delayedExecutor.schedule(next, 2);
}
}
}