11package net .ludocrypt .limlib .api .world ;
22
33import java .io .IOException ;
4+ import java .util .Arrays ;
45import java .util .Comparator ;
56import java .util .HashMap ;
67import java .util .List ;
1415import net .minecraft .block .Blocks ;
1516import net .minecraft .entity .Entity ;
1617import net .minecraft .entity .EntityType ;
18+ import net .minecraft .entity .decoration .AbstractDecorationEntity ;
1719import net .minecraft .nbt .NbtCompound ;
1820import net .minecraft .nbt .NbtDouble ;
1921import net .minecraft .nbt .NbtFloat ;
@@ -79,7 +81,7 @@ public NbtPlacerUtil manipulate(BlockRotation rotation, BlockMirror mirror) {
7981 .sorted (Comparator .comparing ((pair ) -> pair .getFirst ().getZ ())).toList ();
8082 positionsPairList .forEach ((pair ) -> positions .put (pair .getFirst ().subtract (positionsPairList .get (0 ).getFirst ()), pair .getSecond ()));
8183
82- return new NbtPlacerUtil (storedNbt , positions , storedNbt .getList ("entities" , 10 ), positionsPairList . get ( 0 ). getFirst ( ), sizeVector );
84+ return new NbtPlacerUtil (storedNbt , positions , storedNbt .getList ("entities" , 10 ), transformSize ( sizeVector , rotation , mirror ), sizeVector );
8385 }
8486
8587 public static Optional <NbtPlacerUtil > load (ResourceManager manager , Identifier id ) {
@@ -151,9 +153,8 @@ public NbtPlacerUtil generateNbt(ChunkRegion region, BlockPos at, TriConsumer<Bl
151153 public NbtPlacerUtil spawnEntities (ChunkRegion region , BlockPos pos , BlockRotation rotation , BlockMirror mirror ) {
152154 this .entities .forEach ((nbtElement ) -> {
153155 NbtCompound entityCompound = (NbtCompound ) nbtElement ;
154- NbtList nbtPos = entityCompound .getList ("blockPos" , 3 );
155- Vec3d realPosition = mirror (rotate (new Vec3d (nbtPos .getInt (0 ), nbtPos .getInt (1 ), nbtPos .getInt (2 )), rotation ), mirror ).subtract (Vec3d .of (lowestPos )).add (pos .getX (), pos .getY (),
156- pos .getZ ());
156+ NbtList nbtPos = entityCompound .getList ("pos" , 6 );
157+ Vec3d realPosition = mirror (rotate (new Vec3d (nbtPos .getDouble (0 ), nbtPos .getDouble (1 ), nbtPos .getDouble (2 )), rotation ), mirror ).subtract (Vec3d .of (lowestPos )).add (Vec3d .of (pos ));
157158
158159 NbtCompound nbt = entityCompound .getCompound ("nbt" ).copy ();
159160 nbt .remove ("Pos" );
@@ -165,15 +166,6 @@ public NbtPlacerUtil spawnEntities(ChunkRegion region, BlockPos pos, BlockRotati
165166 posList .add (NbtDouble .of (realPosition .z ));
166167 nbt .put ("Pos" , posList );
167168
168- if (nbt .contains ("TileX" , 99 ) && nbt .contains ("TileY" , 99 ) && nbt .contains ("TileZ" , 99 )) {
169- nbt .remove ("TileX" );
170- nbt .remove ("TileY" );
171- nbt .remove ("TileZ" );
172- nbt .putInt ("TileX" , (int ) Math .floor (realPosition .x ));
173- nbt .putInt ("TileY" , (int ) Math .floor (realPosition .y ));
174- nbt .putInt ("TileZ" , (int ) Math .floor (realPosition .z ));
175- }
176-
177169 NbtList rotationList = new NbtList ();
178170 NbtList entityRotationList = nbt .getList ("Rotation" , 5 );
179171 float yawRotation = applyMirror (applyRotation (entityRotationList .getFloat (0 ), rotation ), mirror );
@@ -188,10 +180,39 @@ public NbtPlacerUtil spawnEntities(ChunkRegion region, BlockPos pos, BlockRotati
188180 nbt .putByte ("facing" , (byte ) dir .getHorizontal ());
189181 }
190182
191- getEntity (region , nbt ).ifPresent ((entity ) -> {
183+ if (nbt .contains ("TileX" , 3 ) && nbt .contains ("TileY" , 3 ) && nbt .contains ("TileZ" , 3 )) {
184+ nbt .remove ("TileX" );
185+ nbt .remove ("TileY" );
186+ nbt .remove ("TileZ" );
187+ nbt .putInt ("TileX" , MathHelper .floor (realPosition .x ));
188+ nbt .putInt ("TileY" , MathHelper .floor (realPosition .y ));
189+ nbt .putInt ("TileZ" , MathHelper .floor (realPosition .z ));
190+ }
191+
192+ Optional <Entity > optionalEntity = getEntity (region , nbt );
193+
194+ if (optionalEntity .isPresent ()) {
195+ Entity entity = optionalEntity .get ();
192196 entity .refreshPositionAndAngles (realPosition .x , realPosition .y , realPosition .z , yawRotation , entity .getPitch ());
197+
198+ if (entity instanceof AbstractDecorationEntity deco ) {
199+ double newX = realPosition .getX () - (deco .getWidthPixels () % 32 == 0 ? 0.5 : 0.0 ) * deco .getHorizontalFacing ().rotateYCounterclockwise ().getOffsetX ();
200+ double newY = realPosition .getY () - (deco .getHeightPixels () % 32 == 0 ? 0.5 : 0.0 );
201+ double newZ = realPosition .getZ () - (deco .getWidthPixels () % 32 == 0 ? 0.5 : 0.0 ) * deco .getHorizontalFacing ().rotateYCounterclockwise ().getOffsetZ ();
202+
203+ newX += deco .getHorizontalFacing ().getOffsetX () * 0.46875D ;
204+ newZ += deco .getHorizontalFacing ().getOffsetZ () * 0.46875D ;
205+
206+ newX -= 0.5 ;
207+ newY -= 0.5 ;
208+ newZ -= 0.5 ;
209+
210+ deco .setPosition (newX , newY , newZ );
211+ }
212+
193213 region .spawnEntity (entity );
194- });
214+ }
215+
195216 });
196217 return this ;
197218 }
@@ -257,6 +278,22 @@ public static BlockPos mirror(BlockPos in, BlockMirror mirror) {
257278 }
258279 }
259280
281+ public static BlockPos transformSize (BlockPos in , BlockRotation rotation , BlockMirror mirror ) {
282+ BlockPos origin = BlockPos .ORIGIN ;
283+ BlockPos xPin = mirror (rotate (new BlockPos (in .getX (), 0 , 0 ), rotation ), mirror );
284+ BlockPos zPin = mirror (rotate (new BlockPos (0 , 0 , in .getZ ()), rotation ), mirror );
285+ BlockPos pin = mirror (rotate (new BlockPos (in .getX (), 0 , in .getZ ()), rotation ), mirror );
286+
287+ return findBottomLeftVertex (origin , xPin , zPin , pin );
288+ }
289+
290+ public static BlockPos findBottomLeftVertex (BlockPos v1 , BlockPos v2 , BlockPos v3 , BlockPos v4 ) {
291+ BlockPos [] vertices = { v1 , v2 , v3 , v4 };
292+ Arrays .sort (vertices , Comparator .comparingInt (BlockPos ::getX ));
293+ Arrays .sort (vertices , Comparator .comparingInt (BlockPos ::getZ ));
294+ return vertices [0 ];
295+ }
296+
260297 public Direction mirror (Direction in , BlockMirror mirror ) {
261298 switch (mirror ) {
262299 case LEFT_RIGHT :
0 commit comments