3
3
import com .mojang .serialization .Codec ;
4
4
import com .mojang .serialization .MapCodec ;
5
5
import com .mojang .serialization .codecs .RecordCodecBuilder ;
6
+ import com .sk89q .worldedit .math .convolution .HeightMap ;
6
7
import net .minecraft .core .BlockPos ;
7
8
import net .minecraft .core .Rotations ;
8
9
import net .minecraft .network .chat .Component ;
9
10
import net .minecraft .resources .ResourceKey ;
11
+ import net .minecraft .server .level .ServerLevel ;
10
12
import net .minecraft .server .level .ServerPlayer ;
11
13
import net .minecraft .util .RandomSource ;
14
+ import net .minecraft .util .Tuple ;
12
15
import net .minecraft .world .entity .Entity ;
13
16
import net .minecraft .world .level .block .Block ;
14
17
import net .minecraft .world .level .block .Blocks ;
24
27
import org .dimdev .dimdoors .block .ModBlocks ;
25
28
import org .dimdev .dimdoors .block .UnravelUtil ;
26
29
import org .dimdev .dimdoors .world .ModDimensions ;
30
+ import org .dimdev .dimdoors .world .level .registry .DimensionalRegistry ;
31
+ import org .jetbrains .annotations .Nullable ;
32
+ import oshi .util .tuples .Pair ;
27
33
28
34
import java .util .Random ;
29
35
import java .util .UUID ;
@@ -54,43 +60,44 @@ public boolean receiveEntity(Entity entity, Vec3 relativePos, Rotations relative
54
60
// chat(entity, Component.translatable("rifts.destinations.escape.cannot_escape_limbo")); TODO: Decide a proper alternate to spam
55
61
return false ;
56
62
}
63
+
64
+
57
65
if (entity .level ().isClientSide )
58
66
return false ;
59
- UUID uuid = entity .getUUID ();
60
- if (uuid != null ) {
61
- //Location destLoc = DimensionalRegistry.getRiftRegistry().getOverworldRift(uuid);
62
- if (entity .level ().getPlayerByUUID (uuid ) == null ) {
63
- LOGGER .log (Level .ERROR , "Tried to get player for escape target from uuid, but player does not exist, uh oh" );
64
- return false ;
65
- }
66
- Location destLoc ;
67
-
68
- if (((ServerPlayer ) entity .level ().getPlayerByUUID (uuid )).getRespawnPosition () != null && DimensionalDoors .getConfig ().getLimboConfig ().escapeTargetWorld == null && !DimensionalDoors .getConfig ().getLimboConfig ().escapeToWorldSpawn ) {
69
- LOGGER .log (Level .INFO , "Sending player from limbo to their spawnpoint, good luck!" );
70
- destLoc = new Location (((ServerPlayer ) entity .level ().getPlayerByUUID (uuid )).getRespawnDimension (), ((ServerPlayer ) entity .level ().getPlayerByUUID (uuid )).getRespawnPosition ());
71
- } else if (DimensionalDoors .getConfig ().getLimboConfig ().escapeTargetWorld != null && !DimensionalDoors .getConfig ().getLimboConfig ().escapeToWorldSpawn ) {
72
- targetWorldResourceKey = DimensionalDoors .getConfig ().getLimboConfig ().escapeTargetWorld ;
73
- if (DimensionalDoors .getWorld (targetWorldResourceKey ) != null ) {
74
- LOGGER .log (Level .INFO , "Sending player from limbo to the exit dimension, good luck!" );
75
-
76
- var level = DimensionalDoors .getWorld (targetWorldResourceKey );
77
- destLoc = new Location (level , level .getHeightmapPos (Heightmap .Types .WORLD_SURFACE , entity .blockPosition ()));
78
- } else {
79
- LOGGER .log (Level .INFO , "Target dimension defined in config does not exist. Use /forge dimensions for a list!" );
80
- LOGGER .log (Level .INFO , "Sending player from limbo to worldspawn, good luck!" );
67
+ if (entity instanceof ServerPlayer player ) { //TODO: Determine what other entity types should do when escaping.
68
+ // Location destLoc = DimensionalRegistry.getRiftRegistry().get.getOverworldRift(uuid);
69
+
70
+ ServerLevel destLevel = null ;
71
+ BlockPos destPos = null ;
81
72
82
- var overworld = DimensionalDoors .getServer ().overworld ();
73
+ if (DimensionalDoors .getConfig ().getLimboConfig ().tryPlayerBedSpawn ) {
74
+ var level = DimensionalDoors .getWorld (player .getRespawnDimension ());
83
75
84
- destLoc = new Location (overworld , overworld .getHeightmapPos (Heightmap .Types .WORLD_SURFACE , overworld .getSharedSpawnPos ()));
76
+ if (level != null ) {
77
+ destLevel = level ;
78
+ destPos = player .getRespawnPosition ();
85
79
}
86
- } else {
87
- LOGGER .log (Level .INFO , "sending player from limbo to worldspawn, good luck!" );
80
+ }
88
81
89
- var overworld = DimensionalDoors .getServer ().overworld ();
90
82
91
- destLoc = new Location (overworld , overworld .getHeightmapPos (Heightmap .Types .WORLD_SURFACE , overworld .getSharedSpawnPos ()));
92
- }
83
+ if (destLevel == null ) {
84
+ var targetWorld = DimensionalDoors .getConfig ().getLimboConfig ().escapeTargetWorld ;
85
+ destLevel = DimensionalDoors .getServer ().overworld ();
86
+
87
+ if (targetWorld != null ) {
88
+ var level = DimensionalDoors .getWorld (targetWorld );
93
89
90
+ if (level != null ) {
91
+ destLevel = level ;
92
+ }
93
+ }
94
+
95
+ if (DimensionalDoors .getConfig ().getLimboConfig ().defaultToWorldSpawn ) {
96
+ destPos = destLevel .getSharedSpawnPos ();
97
+ } else {
98
+ destPos = player .blockPosition ();
99
+ }
100
+ }
94
101
95
102
/*
96
103
if (destLoc != null && destLoc.getBlockEntity() instanceof RiftBlockEntity || this.canEscapeLimbo) {
@@ -108,26 +115,30 @@ public boolean receiveEntity(Entity entity, Vec3 relativePos, Rotations relative
108
115
}
109
116
*/
110
117
111
- destLoc = randomizeLimboReturn (destLoc , DimensionalDoors .getConfig ().getLimboConfig ().limboReturnDistance ); //todo add minimum radius
118
+ var destLoc = randomizeLimboReturn (destLevel , destPos , DimensionalDoors .getConfig ().getLimboConfig ().limboReturnDistanceMin , DimensionalDoors . getConfig (). getLimboConfig (). limboReturnDistanceMax ); //todo add minimum radius
112
119
113
120
if (destLoc != null && this .canEscapeLimbo ) {
114
121
Location location = destLoc ; //VirtualLocation.fromLocation(new Location((ServerWorld) entity.world, destLoc.pos)).projectToWorld(false); //TODO Fix world projection.
115
- entity = TeleportUtil .teleport (entity , location .getWorld (), location .getBlockPos (), relativeAngle , relativeVelocity );
122
+
123
+ var level = location .getWorld ();
124
+ entity = TeleportUtil .teleport (entity , level , location .getBlockPos (), relativeAngle , relativeVelocity );
116
125
entity .fallDistance = -500 ;
117
- location .getWorld ().setBlockAndUpdate (location .getBlockPos (), Blocks .AIR .defaultBlockState ());
118
- location .getWorld ().setBlockAndUpdate (location .getBlockPos ().offset (0 , 1 , 0 ), Blocks .AIR .defaultBlockState ());
119
-
120
- RandomSource random = RandomSource .create ();
121
- BlockPos .withinManhattan (location .pos .offset (0 , -3 , 0 ), 3 , 2 , 3 ).forEach ((pos1 -> {
122
- if (random .nextFloat () < (1 / ((float ) location .pos .distSqr (pos1 ))) * DimensionalDoors .getConfig ().getLimboConfig ().limboBlocksCorruptingExitWorldAmount ) {
123
- Block block = location .getWorld ().getBlockState (pos1 ).getBlock ();
124
- if (UnravelUtil .unravelBlocksMap .containsKey (block ))
125
- location .getWorld ().setBlockAndUpdate (pos1 , UnravelUtil .unravelBlocksMap .get (block ).defaultBlockState ());
126
- else if (UnravelUtil .whitelistedBlocksForLimboRemoval .contains (block )) {
127
- location .getWorld ().setBlockAndUpdate (pos1 , ModBlocks .UNRAVELLED_FABRIC .get ().defaultBlockState ());
126
+ level .setBlockAndUpdate (location .getBlockPos (), Blocks .AIR .defaultBlockState ());
127
+ level .setBlockAndUpdate (location .getBlockPos ().offset (0 , 1 , 0 ), Blocks .AIR .defaultBlockState ());
128
+
129
+ if (DimensionalDoors .getConfig ().getLimboConfig ().decaySurroundings ) {
130
+ RandomSource random = RandomSource .create ();
131
+ BlockPos .withinManhattan (location .pos .offset (0 , -3 , 0 ), 3 , 2 , 3 ).forEach ((pos1 -> {
132
+ if (random .nextFloat () < (1 / ((float ) location .pos .distSqr (pos1 ))) * DimensionalDoors .getConfig ().getLimboConfig ().limboBlocksCorruptingExitWorldAmount ) {
133
+ Block block = level .getBlockState (pos1 ).getBlock ();
134
+ if (UnravelUtil .unravelBlocksMap .containsKey (block ))
135
+ level .setBlockAndUpdate (pos1 , UnravelUtil .unravelBlocksMap .get (block ).defaultBlockState ());
136
+ else if (UnravelUtil .whitelistedBlocksForLimboRemoval .contains (block )) {
137
+ level .setBlockAndUpdate (pos1 , ModBlocks .UNRAVELLED_FABRIC .get ().defaultBlockState ());
138
+ }
128
139
}
129
- }
130
- }));
140
+ }));
141
+ }
131
142
} else {
132
143
if (destLoc == null ) {
133
144
chat (entity , Component .translatable ("rifts.destinations.escape.did_not_use_rift" ));
@@ -156,16 +167,27 @@ public VirtualTarget copy() {
156
167
return new EscapeTarget (canEscapeLimbo );
157
168
}
158
169
159
- public static Location randomizeLimboReturn (Location playerSpawn , int range ){
160
- return new Location (playerSpawn .getWorld (), randomizeCoord (playerSpawn .getX (), range ), playerSpawn .getY (), randomizeCoord (playerSpawn .getZ (),range ));
170
+ public static Location randomizeLimboReturn (ServerLevel level , BlockPos pos , int minRange , int maxRange ) {
171
+ if (level == null || pos == null ) return null ;
172
+
173
+ if (minRange == 0 && maxRange == 0 ) return new Location (level , pos );
174
+
175
+ return new Location (
176
+ level ,
177
+ Location .getHeightmapPosSafe (level , randomizeCoord (pos .getX (), minRange , maxRange ), randomizeCoord (pos .getZ (), minRange , maxRange ))
178
+ );
161
179
}
162
180
163
- public static int randomizeCoord (int coord , int range ) {
181
+ public static int randomizeCoord (int coord , int minRange , int maxRange ) {
164
182
Random random = new Random ();
165
- int offset = random .nextInt (range + 1 ); // Generate a random offset within the range
166
- boolean isPositive = random .nextBoolean (); // Randomly decide whether the offset should be positive or negative
167
183
168
- // Apply the offset with the direction (positive or negative)
184
+ if (minRange > maxRange ) {
185
+ throw new IllegalArgumentException ("minRange cannot be greater than maxRange" );
186
+ }
187
+
188
+ int offset = minRange + random .nextInt ((maxRange - minRange ) + 1 );
189
+ boolean isPositive = random .nextBoolean ();
190
+
169
191
return isPositive ? coord + offset : coord - offset ;
170
192
}
171
193
}
0 commit comments