11package gollorum .signpost .minecraft .worldgen ;
22
33import com .google .common .collect .ImmutableList ;
4+ import com .google .common .collect .Streams ;
45import com .mojang .datafixers .util .Either ;
56import com .mojang .serialization .Codec ;
67import com .mojang .serialization .codecs .RecordCodecBuilder ;
4546import java .util .function .Function ;
4647import java .util .function .Supplier ;
4748import java .util .stream .Collectors ;
49+ import java .util .stream .Stream ;
4850
4951public class SignpostJigsawPiece extends SinglePoolElement {
5052
@@ -100,7 +102,7 @@ public boolean place(
100102 if (!Config .Server .worldGen .isVillageGenerationEnabled .get ()) return false ;
101103 if (signpostCountForVillage .getOrDefault (villageLocation , 0 ) >= Config .Server .worldGen .maxSignpostsPerVillage .get ())
102104 return false ;
103- Queue <Map . Entry <BlockPos , WaystoneHandle .Vanilla >> possibleTargets = fetchPossibleTargets (pieceLocation , villageLocation , random );
105+ Queue <Tuple <BlockPos , WaystoneHandle .Vanilla >> possibleTargets = fetchPossibleTargets (pieceLocation , villageLocation , random );
104106 if (possibleTargets .isEmpty ()) {
105107 Signpost .LOGGER .debug ("Did not generate signpost because no targets were found." );
106108 return false ;
@@ -129,24 +131,40 @@ public boolean place(
129131 }
130132 }
131133
132- private static Queue <Map .Entry <BlockPos , WaystoneHandle .Vanilla >> fetchPossibleTargets (BlockPos pieceLocation , BlockPos villageLocation , Random random ) {
133- return WaystoneJigsawPiece .getAllEntries ().stream ()
134- .filter (e -> !(e .getKey ().equals (villageLocation ) || (
135- waystonesTargetedByVillage .containsKey (villageLocation )
136- && waystonesTargetedByVillage .get (villageLocation ).contains (e .getValue ()))))
137- .map (e -> new Tuple <>(e , (float ) Math .sqrt (e .getKey ().distSqr (pieceLocation )) * (0.5f + random .nextFloat ())))
134+ private static Queue <Tuple <BlockPos , WaystoneHandle .Vanilla >> fetchPossibleTargets (BlockPos pieceLocation , BlockPos villageLocation , Random random ) {
135+ return allWaystoneTargets (villageLocation )
136+ .map (e -> new Tuple <>(e , (float ) Math .sqrt (e ._1 .distSqr (pieceLocation )) * (0.5f + random .nextFloat ())))
138137 .sorted ((e1 , e2 ) -> Float .compare (e1 ._2 , e2 ._2 ))
139138 .map (Tuple ::getLeft )
140- .filter (e -> WaystoneLibrary .getInstance ().contains (e .getValue () ))
139+ .filter (e -> WaystoneLibrary .getInstance ().contains (e ._2 ))
141140 .collect (Collectors .toCollection (LinkedList ::new ));
142141 }
143142
143+ private static Stream <Tuple <BlockPos , WaystoneHandle .Vanilla >> allWaystoneTargets (BlockPos villageLocation ) {
144+ Stream <Tuple <BlockPos , WaystoneHandle .Vanilla >> villageWaystones = villageWaystonesExceptSelf (villageLocation );
145+ return (Config .Server .worldGen .villagesOnlyTargetVillages .get ()
146+ ? villageWaystones
147+ : Streams .concat (villageWaystones , nonVillageWaystones ())
148+ ).filter (e -> waystonesTargetedByVillage .containsKey (villageLocation )
149+ && waystonesTargetedByVillage .get (villageLocation ).contains (e ._2 ));
150+ }
151+ private static Stream <Tuple <BlockPos , WaystoneHandle .Vanilla >> villageWaystonesExceptSelf (BlockPos villageLocation ) {
152+ return WaystoneJigsawPiece .getAllEntries ().stream ()
153+ .filter (e -> !(e .getKey ().equals (villageLocation )))
154+ .map (Tuple ::from );
155+ }
156+ private static Stream <Tuple <BlockPos , WaystoneHandle .Vanilla >> nonVillageWaystones () {
157+ return WaystoneLibrary .getInstance ().getAllWaystoneInfo ().stream ()
158+ .map (info -> new Tuple <>(info .locationData .block .blockPos , info .handle ))
159+ .filter (t -> WaystoneJigsawPiece .getAllEntries ().stream ().noneMatch (e -> e .getValue ().equals (t ._2 )));
160+ }
161+
144162 private Collection <WaystoneHandle .Vanilla > populateSignPostGeneration (
145163 StructurePlaceSettings placementSettings ,
146164 BlockPos pieceLocation ,
147165 WorldGenLevel world ,
148166 Random random ,
149- Queue <Map . Entry <BlockPos , WaystoneHandle .Vanilla >> possibleTargets
167+ Queue <Tuple <BlockPos , WaystoneHandle .Vanilla >> possibleTargets
150168 ) {
151169 Direction facing = placementSettings .getRotation ().rotate (Direction .WEST );
152170 Direction left = placementSettings .getRotation ().rotate (Direction .SOUTH );
@@ -175,7 +193,7 @@ public Tuple<Collection<WaystoneHandle.Vanilla>, Consumer<PostTile>> makeSign(
175193 Direction facing ,
176194 WorldGenLevel world ,
177195 BlockPos tilePos ,
178- Queue <Map . Entry <BlockPos , WaystoneHandle .Vanilla >> possibleTargets ,
196+ Queue <Tuple <BlockPos , WaystoneHandle .Vanilla >> possibleTargets ,
179197 float y
180198 ) {
181199 if (possibleTargets .isEmpty ()) return new Tuple <>(Collections .emptySet (), x -> {});
@@ -188,13 +206,13 @@ private Tuple<Collection<WaystoneHandle.Vanilla>, Consumer<PostTile>> makeWideSi
188206 Direction facing ,
189207 WorldGenLevel world ,
190208 BlockPos tilePos ,
191- Queue <Map . Entry <BlockPos , WaystoneHandle .Vanilla >> possibleTargets ,
209+ Queue <Tuple <BlockPos , WaystoneHandle .Vanilla >> possibleTargets ,
192210 float y
193211 ) {
194- Map . Entry <BlockPos , WaystoneHandle .Vanilla > target = possibleTargets .poll ();
212+ Tuple <BlockPos , WaystoneHandle .Vanilla > target = possibleTargets .poll ();
195213 if (target == null ) return new Tuple <>(Collections .emptySet (), x -> {});
196- WaystoneData targetData = WaystoneLibrary .getInstance ().getData (target .getValue () );
197- Angle rotation = SignBlockPart .pointingAt (tilePos , target .getKey () );
214+ WaystoneData targetData = WaystoneLibrary .getInstance ().getData (target ._2 );
215+ Angle rotation = SignBlockPart .pointingAt (tilePos , target ._1 );
198216 Consumer <PostTile > onTileFetched = tile -> {
199217 if (tile .getParts ().stream ().anyMatch (instance -> !(instance .blockPart instanceof PostBlockPart ) && isNearly (instance .offset .y , y )))
200218 return ;
@@ -203,7 +221,7 @@ private Tuple<Collection<WaystoneHandle.Vanilla>, Consumer<PostTile>> makeWideSi
203221 new SmallWideSignBlockPart (
204222 rotation , targetData .name , shouldFlip (facing , rotation ),
205223 tile .modelType .mainTexture , tile .modelType .secondaryTexture ,
206- overlayFor (world , tilePos ), Colors .black , Optional .of (target .getValue () ),
224+ overlayFor (world , tilePos ), Colors .black , Optional .of (target ._2 ),
207225 ItemStack .EMPTY , tile .modelType , false
208226 ),
209227 new Vector3 (0 , y , 0 )
@@ -212,7 +230,7 @@ rotation, targetData.name, shouldFlip(facing, rotation),
212230 PlayerHandle .Invalid
213231 );
214232 };
215- return new Tuple <>(Collections .singleton (target .getValue () ), onTileFetched );
233+ return new Tuple <>(Collections .singleton (target ._2 ), onTileFetched );
216234 }
217235
218236 private static boolean isNearly (float a , float b ) { return Math .abs (a - b ) < 1e-5f ; }
@@ -221,13 +239,13 @@ private Tuple<Collection<WaystoneHandle.Vanilla>, Consumer<PostTile>> makeShortS
221239 Direction facing ,
222240 WorldGenLevel world ,
223241 BlockPos tilePos ,
224- Queue <Map . Entry <BlockPos , WaystoneHandle .Vanilla >> possibleTargets ,
242+ Queue <Tuple <BlockPos , WaystoneHandle .Vanilla >> possibleTargets ,
225243 float y
226244 ) {
227- Map . Entry <BlockPos , WaystoneHandle .Vanilla > target = possibleTargets .poll ();
245+ Tuple <BlockPos , WaystoneHandle .Vanilla > target = possibleTargets .poll ();
228246 if (target == null ) return new Tuple <>(Collections .emptySet (), x -> {});
229- WaystoneData targetData = WaystoneLibrary .getInstance ().getData (target .getValue () );
230- Angle rotation = SignBlockPart .pointingAt (tilePos , target .getKey () );
247+ WaystoneData targetData = WaystoneLibrary .getInstance ().getData (target ._2 );
248+ Angle rotation = SignBlockPart .pointingAt (tilePos , target ._1 );
231249 boolean shouldFlip = shouldFlip (facing , rotation );
232250 Optional <Overlay > overlay = overlayFor (world , tilePos );
233251 List <Consumer <PostTile >> onTileFetched = new ArrayList <>();
@@ -236,26 +254,26 @@ private Tuple<Collection<WaystoneHandle.Vanilla>, Consumer<PostTile>> makeShortS
236254 new SmallShortSignBlockPart (
237255 rotation , targetData .name , shouldFlip ,
238256 tile .modelType .mainTexture , tile .modelType .secondaryTexture ,
239- overlay , Colors .black , Optional .of (target .getValue () ),
257+ overlay , Colors .black , Optional .of (target ._2 ),
240258 ItemStack .EMPTY , tile .modelType , false
241259 ),
242260 new Vector3 (0 , y , 0 )
243261 ),
244262 ItemStack .EMPTY ,
245263 PlayerHandle .Invalid
246264 ));
247- Map . Entry <BlockPos , WaystoneHandle .Vanilla > secondTarget = possibleTargets .poll ();
248- List <Map . Entry <BlockPos , WaystoneHandle .Vanilla >> skippedTargets = new ArrayList <>();
265+ Tuple <BlockPos , WaystoneHandle .Vanilla > secondTarget = possibleTargets .poll ();
266+ List <Tuple <BlockPos , WaystoneHandle .Vanilla >> skippedTargets = new ArrayList <>();
249267 while (secondTarget != null ) {
250- WaystoneData secondTargetData = WaystoneLibrary .getInstance ().getData (secondTarget .getValue () );
251- Angle secondRotation = SignBlockPart .pointingAt (tilePos , secondTarget .getKey () );
268+ WaystoneData secondTargetData = WaystoneLibrary .getInstance ().getData (secondTarget ._2 );
269+ Angle secondRotation = SignBlockPart .pointingAt (tilePos , secondTarget ._1 );
252270 boolean shouldSecondFlip = shouldFlip (facing , secondRotation );
253271 if (shouldSecondFlip == shouldFlip ) {
254272 skippedTargets .add (secondTarget );
255273 secondTarget = possibleTargets .poll ();
256274 continue ;
257275 }
258- WaystoneHandle .Vanilla secondTargetHandle = secondTarget .getValue () ;
276+ WaystoneHandle .Vanilla secondTargetHandle = secondTarget ._2 ;
259277 onTileFetched .add (tile -> tile .addPart (
260278 new BlockPartInstance (
261279 new SmallShortSignBlockPart (
@@ -276,10 +294,10 @@ private Tuple<Collection<WaystoneHandle.Vanilla>, Consumer<PostTile>> makeShortS
276294 possibleTargets .addAll (skippedTargets );
277295 return new Tuple <>(
278296 secondTarget == null
279- ? Collections .singleton (target .getValue () )
280- : ImmutableList .of (target .getValue () , secondTarget .getValue () ),
297+ ? Collections .singleton (target ._2 )
298+ : ImmutableList .of (target ._2 , secondTarget ._2 ),
281299 tile -> {
282- if (! tile .getParts ().stream ().anyMatch (instance -> !(instance .blockPart instanceof PostBlockPart ) && isNearly (instance .offset .y , y )))
300+ if (tile .getParts ().stream ().noneMatch (instance -> !(instance .blockPart instanceof PostBlockPart ) && isNearly (instance .offset .y , y )))
283301 for (Consumer <PostTile > now : onTileFetched ) now .accept (tile );
284302 }
285303 );
0 commit comments