From 26f112aeb6f9ada0a4a4c68b68c30a8e559c768e Mon Sep 17 00:00:00 2001 From: FirstMegaGame4 <84094287+FirstMegaGame4@users.noreply.github.com> Date: Sun, 13 Aug 2023 02:34:28 +0200 Subject: [PATCH] Add NonOriented, OrientedBlockPos And TerrainConsumers Math Classes Tweak CustomSquaredPortalBlock --- .../library/math/NonOriented.java | 23 ++ .../library/math/OrientedBlockPos.java | 285 ++++++++++++++++++ .../library/math/TerrainConsumers.java | 41 +++ .../squared/CustomSquaredPortalBlock.java | 11 +- 4 files changed, 353 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/mmodding/mmodding_lib/library/math/NonOriented.java create mode 100644 src/main/java/com/mmodding/mmodding_lib/library/math/OrientedBlockPos.java create mode 100644 src/main/java/com/mmodding/mmodding_lib/library/math/TerrainConsumers.java diff --git a/src/main/java/com/mmodding/mmodding_lib/library/math/NonOriented.java b/src/main/java/com/mmodding/mmodding_lib/library/math/NonOriented.java new file mode 100644 index 0000000..2712d2d --- /dev/null +++ b/src/main/java/com/mmodding/mmodding_lib/library/math/NonOriented.java @@ -0,0 +1,23 @@ +package com.mmodding.mmodding_lib.library.math; + +import net.minecraft.util.math.Direction; + +public enum NonOriented { + TOP, + BOTTOM, + FRONT, + BEHIND, + LEFT, + RIGHT; + + public Direction transform(Direction direction) { + return switch (this) { + case TOP -> direction.rotateCounterclockwise(Direction.Axis.X); + case BOTTOM -> direction.rotateClockwise(Direction.Axis.X); + case FRONT -> direction; + case BEHIND -> direction.getOpposite(); + case LEFT -> direction.rotateCounterclockwise(Direction.Axis.Y); + case RIGHT -> direction.rotateClockwise(Direction.Axis.Y); + }; + } +} diff --git a/src/main/java/com/mmodding/mmodding_lib/library/math/OrientedBlockPos.java b/src/main/java/com/mmodding/mmodding_lib/library/math/OrientedBlockPos.java new file mode 100644 index 0000000..0612007 --- /dev/null +++ b/src/main/java/com/mmodding/mmodding_lib/library/math/OrientedBlockPos.java @@ -0,0 +1,285 @@ +package com.mmodding.mmodding_lib.library.math; + +import net.minecraft.util.BlockRotation; +import net.minecraft.util.math.*; + +public class OrientedBlockPos extends BlockPos { + + private Direction direction; + + private OrientedBlockPos(Direction direction, int i, int j, int k) { + super(i, j, k); + this.direction = direction; + } + + private OrientedBlockPos(Direction direction, double d, double e, double f) { + super(d, e, f); + this.direction = direction; + } + + private OrientedBlockPos(Direction direction, Vec3d pos) { + super(pos); + this.direction = direction; + } + + private OrientedBlockPos(Direction direction, Position pos) { + super(pos); + this.direction = direction; + } + + private OrientedBlockPos(Direction direction, Vec3i pos) { + super(pos); + this.direction = direction; + } + + public static WithoutOrientation of(int i, int j, int k) { + return new WithoutOrientation(i, j, k); + } + + public static WithoutOrientation of(double d, double e, double f) { + return new WithoutOrientation(d, e, f); + } + + public static WithoutOrientation of(Vec3d vec3d) { + return new WithoutOrientation(vec3d); + } + + public static WithoutOrientation of(Position position) { + return new WithoutOrientation(position); + } + + public static WithoutOrientation of(Vec3i vec3i) { + return new WithoutOrientation(vec3i); + } + + public OrientedBlockPos top() { + super.offset(NonOriented.TOP.transform(this.direction)); + return this; + } + + public OrientedBlockPos top(int i) { + super.offset(NonOriented.TOP.transform(this.direction), i); + return this; + } + + public OrientedBlockPos bottom() { + super.offset(NonOriented.BOTTOM.transform(this.direction)); + return this; + } + + public OrientedBlockPos bottom(int i) { + super.offset(NonOriented.BOTTOM.transform(this.direction), i); + return this; + } + + public OrientedBlockPos front() { + super.offset(NonOriented.FRONT.transform(this.direction)); + return this; + } + + public OrientedBlockPos front(int i) { + super.offset(NonOriented.FRONT.transform(this.direction), i); + return this; + } + + public OrientedBlockPos behind() { + super.offset(NonOriented.BEHIND.transform(this.direction)); + return this; + } + + public OrientedBlockPos behind(int i) { + super.offset(NonOriented.BEHIND.transform(this.direction), i); + return this; + } + + public OrientedBlockPos left() { + super.offset(NonOriented.LEFT.transform(this.direction)); + return this; + } + + public OrientedBlockPos left(int i) { + super.offset(NonOriented.LEFT.transform(this.direction), i); + return this; + } + + public OrientedBlockPos right() { + super.offset(NonOriented.RIGHT.transform(this.direction)); + return this; + } + + public OrientedBlockPos right(int i) { + super.offset(NonOriented.RIGHT.transform(this.direction), i); + return this; + } + + public OrientedBlockPos rotateClockwise(Direction.Axis axis) { + this.direction = this.direction.rotateClockwise(axis); + return this; + } + + public OrientedBlockPos rotateCounterClockWise(Direction.Axis axis) { + this.direction = this.direction.rotateCounterclockwise(axis); + return this; + } + + @Override + @Deprecated + public BlockPos up() { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos up(int i) { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos north() { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos north(int i) { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos down() { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos down(int i) { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos south() { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos south(int i) { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos west() { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos west(int i) { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos east() { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos east(int i) { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos offset(Direction direction, int i) { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos offset(Direction.Axis axis, int i) { + throw new UnsupportedOperationException(); + } + + @Override + @Deprecated + public BlockPos offset(Direction direction) { + throw new UnsupportedOperationException(); + } + + @Override + public OrientedBlockPos rotate(BlockRotation rotation) { + throw new UnsupportedOperationException(); + } + + public static class WithoutOrientation { + + private final Status status; + + private int i; + private int j; + private int k; + + private double d; + private double e; + private double f; + + private Vec3d vec3d; + + private Position position; + + private Vec3i vec3i; + + private WithoutOrientation(int i, int j, int k) { + this.status = Status.INTEGERS; + this.i = i; + this.j = j; + this.k = k; + } + + private WithoutOrientation(double d, double e, double f) { + this.status = Status.DOUBLES; + this.d = d; + this.e = e; + this.f = f; + } + + private WithoutOrientation(Vec3d vec3d) { + this.status = Status.VEC3D; + this.vec3d = vec3d; + } + + private WithoutOrientation(Position position) { + this.status = Status.POSITION; + this.position = position; + } + + private WithoutOrientation(Vec3i vec3i) { + this.status = Status.VEC3I; + this.vec3i = vec3i; + } + + public OrientedBlockPos apply(Direction direction) { + return switch (this.status) { + case INTEGERS -> new OrientedBlockPos(direction, this.i, this.j, this.k); + case DOUBLES -> new OrientedBlockPos(direction, this.d, this.e, this.f); + case VEC3D -> new OrientedBlockPos(direction, this.vec3d); + case POSITION -> new OrientedBlockPos(direction, this.position); + case VEC3I -> new OrientedBlockPos(direction, this.vec3i); + }; + } + + private enum Status { + INTEGERS, + DOUBLES, + VEC3D, + POSITION, + VEC3I + } + } +} diff --git a/src/main/java/com/mmodding/mmodding_lib/library/math/TerrainConsumers.java b/src/main/java/com/mmodding/mmodding_lib/library/math/TerrainConsumers.java new file mode 100644 index 0000000..725ed6b --- /dev/null +++ b/src/main/java/com/mmodding/mmodding_lib/library/math/TerrainConsumers.java @@ -0,0 +1,41 @@ +package com.mmodding.mmodding_lib.library.math; + +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; + +import java.util.function.Consumer; + +public class TerrainConsumers { + + public static class Oriented { + + private final BlockPos basePos; + private final Consumer consumer; + + public Oriented(BlockPos basePos, Consumer consumer) { + this.basePos = basePos; + this.consumer = consumer; + } + + public void apply() { + this.consumer.accept(this.basePos); + } + } + + public static class UnOriented { + + private final BlockPos basePos; + private final Direction direction; + private final Consumer consumer; + + public UnOriented(BlockPos basePos, Direction direction, Consumer consumer) { + this.basePos = basePos; + this.direction = direction; + this.consumer = consumer; + } + + public void apply() { + this.consumer.accept(OrientedBlockPos.of(this.basePos).apply(this.direction)); + } + } +} diff --git a/src/main/java/com/mmodding/mmodding_lib/library/portals/squared/CustomSquaredPortalBlock.java b/src/main/java/com/mmodding/mmodding_lib/library/portals/squared/CustomSquaredPortalBlock.java index 7d5559d..fadc394 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/portals/squared/CustomSquaredPortalBlock.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/portals/squared/CustomSquaredPortalBlock.java @@ -69,14 +69,11 @@ public BlockState getStateForNeighborUpdate(BlockState state, Direction directio @Override public void onEntityCollision(BlockState state, World world, BlockPos pos, Entity entity) { if (!entity.hasVehicle() && !entity.hasPassengers() && entity.canUsePortals()) { - for (Identifier identifier : MModdingGlobalMaps.getAllCustomSquaredPortalKeys()) { - AbstractSquaredPortal abstractPortal = MModdingGlobalMaps.getAbstractSquaredPortal(identifier); + for (Identifier identifier : MModdingGlobalMaps.getCustomSquaredPortalKeys()) { + CustomSquaredPortal squaredPortal = MModdingGlobalMaps.getCustomSquaredPortal(identifier); - if (abstractPortal.getPortalBlock() == this) { - - if (abstractPortal instanceof CustomSquaredPortal squaredPortal) { - ((EntityDuckInterface) entity).mmodding_lib$setInCustomPortal(squaredPortal, world, pos); - } + if (squaredPortal.getPortalBlock() == this) { + ((EntityDuckInterface) entity).mmodding_lib$setInCustomPortal(squaredPortal, world, pos); } } }