Attempt to fix rtp

This commit is contained in:
zontreck 2024-01-21 23:57:03 -07:00
parent 8eb93ff176
commit 11864385e4
9 changed files with 93 additions and 184 deletions

View file

@ -3,7 +3,7 @@
org.gradle.jvmargs=-Xmx3G org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false org.gradle.daemon=false
libzontreck=1.10.011624.1712 libzontreck=1.10.012124.1709
## Environment Properties ## 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. # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPLv3 mod_license=GPLv3
# The mod version. See https://semver.org/ # The mod version. See https://semver.org/
mod_version=1.2.011624.1834 mod_version=1.2.012124.2356
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # 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. # This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html # See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View file

@ -24,6 +24,7 @@ import net.minecraft.commands.Commands;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
@ -74,8 +75,10 @@ public class SetHomeCommand {
Vec2 rot = p.getRotationVector(); Vec2 rot = p.getRotationVector();
TeleportDestination dest = new TeleportDestination(new Vector3(position), new Vector2(rot), p.getLevel()); TeleportDestination dest = new TeleportDestination(new Vector3(position), new Vector2(rot), p.getLevel());
BlockState bs = p.getLevel().getBlockState(dest.Position.moveDown().asBlockPos());
Home newhome = new Home(p, homeName, dest, new ItemStack(p.getFeetBlockState().getBlock().asItem())); Home newhome = new Home(p, homeName, dest, new ItemStack(bs.getBlock().asItem()));
AriasEssentials.player_homes.get(p.getUUID()).add(newhome); AriasEssentials.player_homes.get(p.getUUID()).add(newhome);

View file

@ -3,11 +3,13 @@ package dev.zontreck.essentials.commands.teleport;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType; import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.zontreck.essentials.Messages;
import dev.zontreck.libzontreck.chestgui.ChestGUI; import dev.zontreck.libzontreck.chestgui.ChestGUI;
import dev.zontreck.libzontreck.chestgui.ChestGUIButton; import dev.zontreck.libzontreck.chestgui.ChestGUIButton;
import dev.zontreck.libzontreck.chestgui.ChestGUIIdentifier; import dev.zontreck.libzontreck.chestgui.ChestGUIIdentifier;
import dev.zontreck.libzontreck.profiles.Profile; import dev.zontreck.libzontreck.profiles.Profile;
import dev.zontreck.libzontreck.profiles.UserProfileNotYetExistsException; import dev.zontreck.libzontreck.profiles.UserProfileNotYetExistsException;
import dev.zontreck.libzontreck.util.ChatHelpers;
import dev.zontreck.libzontreck.util.ServerUtilities; import dev.zontreck.libzontreck.util.ServerUtilities;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands; import net.minecraft.commands.Commands;
@ -21,10 +23,10 @@ public class TPEffectsCommand
{ {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{ {
dispatcher.register(Commands.literal("tpeffects").then(Commands.argument("enable", BoolArgumentType.bool()).executes(x->tpeffects(x.getSource(), BoolArgumentType.getBool(x, "enable"))))); dispatcher.register(Commands.literal("tpeffects_disable").then(Commands.argument("disabled", BoolArgumentType.bool()).executes(x->tpeffects(x.getSource(), BoolArgumentType.getBool(x, "disabled")))));
} }
public static int tpeffects(CommandSourceStack source, boolean enabled) public static int tpeffects(CommandSourceStack source, boolean disabled)
{ {
ServerPlayer player = null; ServerPlayer player = null;
try { try {
@ -35,7 +37,9 @@ public class TPEffectsCommand
try { try {
Profile prof = Profile.get_profile_of(player.getStringUUID()); Profile prof = Profile.get_profile_of(player.getStringUUID());
prof.NBT.putBoolean("tpeffects", enabled); prof.NBT.putBoolean("tpeffects", disabled);
ChatHelpers.broadcastTo(player.getUUID(), ChatHelpers.macro(Messages.TP_EFFECTS_TOGGLED, disabled ? "disabled" : "enabled"), player.server);
return 0; return 0;
} catch (UserProfileNotYetExistsException e) { } catch (UserProfileNotYetExistsException e) {

View file

@ -33,7 +33,7 @@ public class TeleportActioner
public static void ApplyTeleportEffect(ServerPlayer player){ public static void ApplyTeleportEffect(ServerPlayer player){
try { try {
Profile prof = Profile.get_profile_of(player.getStringUUID()); Profile prof = Profile.get_profile_of(player.getStringUUID());
if(!prof.NBT.getBoolean("tpeffects")) if(prof.NBT.getBoolean("tpeffects"))
{ {
return; return;
} }

View file

@ -23,6 +23,7 @@ import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
@ -58,7 +59,10 @@ public class SetWarpCommand {
Vec2 rot = p.getRotationVector(); Vec2 rot = p.getRotationVector();
TeleportDestination dest = new TeleportDestination(new Vector3(position), new Vector2(rot), p.getLevel()); TeleportDestination dest = new TeleportDestination(new Vector3(position), new Vector2(rot), p.getLevel());
Warp w = new Warp(p.getUUID(), string, false, true, dest, new ItemStack(p.getFeetBlockState().getBlock().asItem()));
BlockState bs = p.getLevel().getBlockState(dest.Position.moveDown().asBlockPos());
Warp w = new Warp(p.getUUID(), string, false, true, dest, new ItemStack(bs.getBlock().asItem()));
WarpCreatedEvent event = new WarpCreatedEvent(w); WarpCreatedEvent event = new WarpCreatedEvent(w);
if(MinecraftForge.EVENT_BUS.post(event)){ if(MinecraftForge.EVENT_BUS.post(event)){
ChatHelpers.broadcastTo(p.getUUID(), ChatHelpers.macro(Messages.WARP_CREATE_ERROR, event.denyReason), p.server); ChatHelpers.broadcastTo(p.getUUID(), ChatHelpers.macro(Messages.WARP_CREATE_ERROR, event.denyReason), p.server);

View file

@ -1,8 +1,9 @@
package dev.zontreck.essentials.configs.client; package dev.zontreck.essentials.configs.client;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.zontreck.ariaslib.util.FileIO;
import dev.zontreck.essentials.util.EssentialsDatastore; import dev.zontreck.essentials.util.EssentialsDatastore;
import dev.zontreck.essentials.util.FileHandler; import dev.zontreck.libzontreck.util.SNbtIo;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils; import net.minecraft.nbt.NbtUtils;
@ -30,16 +31,7 @@ public class AEClientConfig
Path serverConfig = EssentialsDatastore.of("client.snbt"); Path serverConfig = EssentialsDatastore.of("client.snbt");
if(serverConfig.toFile().exists()) if(serverConfig.toFile().exists())
{ {
inst = deserialize(SNbtIo.loadSnbt(serverConfig));
try {
String snbt = FileHandler.readFile(serverConfig.toFile().getAbsolutePath());
inst = deserialize(NbtUtils.snbtToStructure(snbt));
} catch (CommandSyntaxException e) {
throw new RuntimeException(e);
}
}else { }else {
initNewConfig(); initNewConfig();
} }
@ -68,9 +60,8 @@ public class AEClientConfig
CompoundTag tag = inst.serialize(); CompoundTag tag = inst.serialize();
var snbt = NbtUtils.structureToSnbt(tag);
FileHandler.writeFile(serverConfig.toFile().getAbsolutePath(), snbt); SNbtIo.writeSnbt(serverConfig, tag);
} }
public CompoundTag serialize() public CompoundTag serialize()

View file

@ -1,17 +1,14 @@
package dev.zontreck.essentials.configs.server; package dev.zontreck.essentials.configs.server;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.zontreck.ariaslib.util.Lists; import dev.zontreck.ariaslib.util.Lists;
import dev.zontreck.essentials.configs.server.sections.*; import dev.zontreck.essentials.configs.server.sections.*;
import dev.zontreck.essentials.util.EssentialsDatastore; import dev.zontreck.essentials.util.EssentialsDatastore;
import dev.zontreck.essentials.util.FileHandler;
import dev.zontreck.essentials.util.Maps; import dev.zontreck.essentials.util.Maps;
import dev.zontreck.libzontreck.util.SNbtIo;
import net.minecraft.nbt.*; import net.minecraft.nbt.*;
import java.io.*;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public class AEServerConfig public class AEServerConfig
@ -52,15 +49,7 @@ public class AEServerConfig
if(serverConfig.toFile().exists()) if(serverConfig.toFile().exists())
{ {
try { inst = deserialize(SNbtIo.loadSnbt(serverConfig));
String snbt = FileHandler.readFile(serverConfig.toFile().getAbsolutePath());
inst = deserialize(NbtUtils.snbtToStructure(snbt));
} catch (CommandSyntaxException e) {
throw new RuntimeException(e);
}
}else { }else {
initNewConfig(); initNewConfig();
} }
@ -112,9 +101,7 @@ public class AEServerConfig
CompoundTag tag = inst.serialize(); CompoundTag tag = inst.serialize();
var snbt = NbtUtils.structureToSnbt(tag); SNbtIo.writeSnbt(serverConfig, tag);
FileHandler.writeFile(serverConfig.toFile().getAbsolutePath(), snbt);
} }
public CompoundTag serialize() public CompoundTag serialize()

View file

@ -8,6 +8,7 @@ import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i; import net.minecraft.core.Vec3i;
import net.minecraft.server.level.ServerLevel; 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.Block;
import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
@ -20,124 +21,87 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
/** public class RTP {
* As of v1.1.121823.x, this is now used for RTP private static final List<Block> BLACKLIST = Lists.of(Blocks.LAVA, Blocks.WATER, Blocks.BEDROCK);
* <p> private final int SEARCH_DIRECTION;
* 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. private final Heightmap.Types heightMapType;
* <p> private final WorldPosition position;
* 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. private final ServerLevel dimension;
* <p> private int tries;
* 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()) 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; heightMapType = Heightmap.Types.MOTION_BLOCKING_NO_LEAVES;
SearchDirection=-1; SEARCH_DIRECTION = -1;
}else { } else {
heightMapType = Heightmap.Types.MOTION_BLOCKING_NO_LEAVES; heightMapType = Heightmap.Types.MOTION_BLOCKING_NO_LEAVES;
SearchDirection=1; SEARCH_DIRECTION = 1;
} }
} }
private final int SearchDirection;
private Thread containingThread;
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;
protected RTP withThreadDelay(int delay) public boolean isDimension(ServerLevel level) {
{
lastThreadDelay=delay;
if(lastThreadDelay >= 60) lastThreadDelay = 60;
return this;
}
public boolean isDimension(ServerLevel level)
{
String dim = WorldPosition.getDim(level); String dim = WorldPosition.getDim(level);
return dim.equals(position.Dimension); return dim.equals(position.Dimension);
} }
/** public BlockPos findSafeLandingLocation() {
* Searches for, and finds, a valid RTP location BlockPos targetPos = position.Position.asBlockPos();
* @param level The level to scan
* @return RTP data
*/
public static void find(ServerLevel level)
{
RandomPositionFactory.beginRTPSearch(level);
}
public static List<RTP> slicedByDimension(ServerLevel lvl) // Search upward for a safe landing location
{ while (!isSafe(targetPos) || !isSafe(targetPos.above())) {
List<RTP> slice = new ArrayList<>(); targetPos = targetPos.above();
Iterator<RTP> it = RTPCaches.Locations.iterator();
while(it.hasNext())
{
RTP nxt = it.next();
if(nxt.isDimension(lvl))
{
slice.add(nxt);
}
} }
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); List<RTP> slice = slicedByDimension(level);
if(slice.size()>0) if (!slice.isEmpty()) {
{ RTP ret = slice.get(AriasEssentials.random.nextInt(slice.size()));
RTP ret = slice.get(AriasEssentials.random.nextInt(0, slice.size()));
RTPCaches.Locations.remove(ret); RTPCaches.Locations.remove(ret);
RandomPositionFactory.beginRTPSearch(ret.position.getActualDimension()); RandomPositionFactory.beginRTPSearch(ret.position.getActualDimension());
return ret; return ret;
} else return null; } 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();
} }
} }
public void moveOpposite()
{ public void move() {
if(SearchDirection==1){ if (SEARCH_DIRECTION == 1) {
moveDown(); position.Position = position.Position.moveUp();
}else if(SearchDirection==0) } else if (SEARCH_DIRECTION == -1) {
{ position.Position = position.Position.moveDown();
moveUp();
} }
} }
public void moveOpposite() {
move();
}
public void newPosition() { public void newPosition() {
if (!AriasEssentials.ALIVE || tries >= 25) return; if (!AriasEssentials.ALIVE || tries >= 25) return;
containingThread = Thread.currentThread(); AriasEssentials.LOGGER.info("RTP starts looking for a new position");
AriasEssentials.LOGGER.info("RTP starts looking for new position");
Random rng = new Random(Instant.now().getEpochSecond()); Random rng = new Random(Instant.now().getEpochSecond());
@ -152,30 +116,25 @@ public class RTP
bpos = pos.asBlockPos(); bpos = pos.asBlockPos();
} while (!isValidPosition(bpos)); } while (!isValidPosition(bpos));
if (pos.y < -30 || pos.y >= position.getActualDimension().getLogicalHeight()) { if (pos.y < -30 || pos.y >= position.getActualDimension().getLogicalHeight()) {
newPosition(); newPosition();
return; return;
} }
tries++; tries++;
AriasEssentials.LOGGER.info("RTP returns new position"); AriasEssentials.LOGGER.info("RTP returns a new position");
} }
private boolean isValidPosition(BlockPos bpos) { private boolean isValidPosition(BlockPos bpos) {
ServerLevel dimension = position.getActualDimension();
ChunkStatus status = ChunkStatus.SPAWN; ChunkStatus status = ChunkStatus.SPAWN;
dimension.getChunk(bpos.getX() >> 4, bpos.getZ() >> 4, status); dimension.getChunk(bpos.getX() >> 4, bpos.getZ() >> 4, status);
Vector3 pos = new Vector3(dimension.getHeightmapPos(heightMapType, bpos)); Vector3 pos = new Vector3(dimension.getHeightmapPos(heightMapType, bpos));
return dimension.getWorldBorder().isWithinBounds(pos.asBlockPos()); return dimension.getWorldBorder().isWithinBounds(pos.asBlockPos());
} }
private Vector3 spiralPositions(Vector3 position) { private Vector3 spiralPositions(Vector3 position) {
Vec3i posi = position.asMinecraftVec3i(); Vec3i posi = position.asMinecraftVec3i();
ServerLevel dimension = this.position.getActualDimension();
BlockPos startBlockPos = new BlockPos(posi.getX(), dimension.getSeaLevel(), posi.getZ()); BlockPos startBlockPos = new BlockPos(posi.getX(), dimension.getSeaLevel(), posi.getZ());
for (BlockPos pos : BlockPos.spiralAround(startBlockPos, 16, Direction.WEST, Direction.NORTH)) { for (BlockPos pos : BlockPos.spiralAround(startBlockPos, 16, Direction.WEST, Direction.NORTH)) {
@ -188,32 +147,17 @@ public class RTP
return position; return position;
} }
public static List<RTP> slicedByDimension(ServerLevel lvl) {
List<RTP> slice = new ArrayList<>();
private boolean safe(BlockPos blockPos) Iterator<RTP> it = RTPCaches.Locations.iterator();
{ while (it.hasNext()) {
containingThread=Thread.currentThread(); RTP nxt = it.next();
BlockState b = position.getActualDimension().getBlockState(blockPos); if (nxt.isDimension(lvl)) {
BlockState b2 = position.getActualDimension().getBlockState(blockPos.above()); slice.add(nxt);
BlockState b3 = position.getActualDimension().getBlockState(blockPos.below()); }
}
if (b.isAir() && b2.isAir()) { return slice;
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;*/
} }
} }

View file

@ -1,24 +0,0 @@
package dev.zontreck.essentials.util;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class FileHandler
{
public static String readFile(String filePath) {
try {
byte[] fileBytes = Files.readAllBytes(Paths.get(filePath));
return new String(fileBytes);
} catch (IOException e) {
return "An error occurred: " + e.getMessage();
}
}
public static void writeFile(String filePath, String newContent) {
try {
Files.write(Paths.get(filePath), newContent.getBytes());
} catch (IOException e) {
}
}
}