From 1aba85b09fb3d676bfdd789e2718b621c97df9c3 Mon Sep 17 00:00:00 2001 From: MeowMC <70984351+MeowMC@users.noreply.github.com> Date: Mon, 12 Apr 2021 16:41:54 -0400 Subject: [PATCH] fix portal space checker --- .../meowmc/portalgun/items/PortalGunItem.java | 312 +++++++++++------- .../meowmc/portalgun/misc/PortalMethods.java | 27 +- 2 files changed, 201 insertions(+), 138 deletions(-) diff --git a/src/main/java/tk/meowmc/portalgun/items/PortalGunItem.java b/src/main/java/tk/meowmc/portalgun/items/PortalGunItem.java index 1d169c4..213a0f9 100644 --- a/src/main/java/tk/meowmc/portalgun/items/PortalGunItem.java +++ b/src/main/java/tk/meowmc/portalgun/items/PortalGunItem.java @@ -29,6 +29,7 @@ import tk.meowmc.portalgun.misc.PortalMethods; import tk.meowmc.portalgun.misc.TaskList; +import static net.minecraft.state.property.Properties.LAYERS; import static net.minecraft.util.hit.HitResult.Type.BLOCK; import static net.minecraft.util.hit.HitResult.Type.MISS; import static tk.meowmc.portalgun.misc.PortalMethods.*; @@ -38,10 +39,11 @@ public class PortalGunItem extends Item { public static HitResult hit; public static BlockHitResult blockHit; public static BlockPos blockPos; + public static BlockState blockState; public static Portal newPortal1; public static Portal newPortal2; - Entity portal1; - Entity portal2; + public static Entity portal1; + public static Entity portal2; public static boolean waitPortal = false; public static BlockState space1BlockState; public static BlockState space2BlockState; @@ -67,8 +69,6 @@ public static void removeOldPortals(CompoundTag tag, CompoundTag portalsTag, Ent } portalsTag.remove("Left" + "Portal"); portalsTag.remove("Right" + "Portal"); - tag.remove("Left" + "Portal"); - tag.remove("Right" + "Portal"); newPortal1.removed = false; newPortal2.removed = false; } @@ -78,6 +78,16 @@ public boolean canMine(BlockState state, World world, BlockPos pos, PlayerEntity return false; } + public static boolean isSnowUp(Direction direction) { + return blockState.getBlock().is(Blocks.SNOW) && blockState.get(LAYERS) == 1 && + space2BlockState.getBlock().is(Blocks.SNOW) && space2BlockState.get(LAYERS) == 1 && + direction == Direction.UP; + } + + public static boolean notSnowUp(Direction direction) { + return !blockState.getBlock().is(Blocks.SNOW); + } + public void portal1Spawn(World world, PlayerEntity user, Hand hand) { ItemStack itemStack = user.getStackInHand(hand); tag = itemStack.getOrCreateTag(); @@ -87,6 +97,7 @@ public void portal1Spawn(World world, PlayerEntity user, Hand hand) { hit = entity.raycast(50.0D, 0.0F, false); blockHit = (BlockHitResult) hit; blockPos = blockHit.getBlockPos(); + blockState = world.getBlockState(blockPos); if (hit.getType() == BLOCK && PortalgunClient.delay) { direction = blockHit.getSide(); @@ -134,9 +145,27 @@ public void portal1Spawn(World world, PlayerEntity user, Hand hand) { dirRight1 = new Vec3i(-dirRight1.getX(), -dirRight1.getY(), -dirRight1.getZ()); - if (dirUp1.getZ() == -1) { - space2BlockPos = blockPos.add(0, 0, 1); - space3BlockPos = blockPos.add(0, 1, 1); + if (dirUp1.getY() != 1) { + if (dirUp1.getZ() == -1) { + space2BlockPos = blockPos.add(0, 0, 1); + space3BlockPos = blockPos.add(0, 1, 1); + } + + if (dirRight1.getX() == -1) { + space2BlockPos = blockPos.add(0, 0, -1); + space3BlockPos = blockPos.add(0, 1, -1); + } + + switch (dirRight1.getZ()) { + case 1: + space2BlockPos = blockPos.add(-1, 0, 0); + space3BlockPos = blockPos.add(-1, 1, 0); + break; + case -1: + space2BlockPos = blockPos.add(1, 0, 0); + space3BlockPos = blockPos.add(1, 1, 0); + break; + } } space1BlockState = world.getBlockState(space1BlockPos); @@ -159,73 +188,78 @@ public void portal1Spawn(World world, PlayerEntity user, Hand hand) { if (!user.getItemCooldownManager().isCoolingDown(this)) user.getItemCooldownManager().set(this, 4); - if (!world.isClient && !waitPortal && space2BlockState.isOpaque() && !space1BlockState.isOpaque() && !space3BlockState.isOpaque() || space2BlockState.getBlock().is(Blocks.SNOW)) { - world.playSound(null, - user.getX(), - user.getY(), - user.getZ(), - Portalgun.PORTAL1_SHOOT_EVENT, - SoundCategory.NEUTRAL, - 1.0F, - 1F); - - waitPortal = true; - - if (portalsTag.contains("Left" + "Portal")) { - newPortal1 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Left" + "Portal")); - if (newPortal1 != null) { - portal1Exists = true; - } - } + if (!world.isClient) { + if (!waitPortal && !space1BlockState.isOpaque() && space2BlockState.isOpaque() && !space3BlockState.isOpaque()) { + world.playSound(null, + user.getX(), + user.getY(), + user.getZ(), + Portalgun.PORTAL1_SHOOT_EVENT, + SoundCategory.NEUTRAL, + 1.0F, + 1F); - if (portalsTag.contains("Right" + "Portal")) { - newPortal2 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Right" + "Portal")); - if (newPortal2 != null) { - portal2Exists = true; - } - } - PortalMethods.portal1Methods(user, hit, world); + waitPortal = true; - if (PortalGunItem.space2BlockState.getBlock().is(Blocks.SNOW) && direction == Direction.UP) { - newPortal1.updatePosition(newPortal1.getX(), newPortal1.getY() - 0.875, newPortal1.getZ()); - } - if (newPortal2 != null) - newPortal1.setDestinationDimension(newPortal2.world.getRegistryKey()); + if (portalsTag.contains("Left" + "Portal")) { + newPortal1 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Left" + "Portal")); + if (newPortal1 != null) { + portal1Exists = true; + } + } + if (portalsTag.contains("Right" + "Portal")) { + newPortal2 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Right" + "Portal")); + if (newPortal2 != null) { + portal2Exists = true; + } + } - PortalManipulation.adjustRotationToConnect(newPortal1, newPortal2); - PortalManipulation.adjustRotationToConnect(newPortal2, newPortal1); + if (notSnowUp(direction) || isSnowUp(direction)) { + PortalMethods.portal1Methods(user, hit, world); - ModMain.serverTaskList.addTask(TaskList.withDelay(delay, TaskList.oneShotTask(() -> { - if (McHelper.getServer().getThread() == Thread.currentThread()) { - if (portalsTag.contains("Left" + "Portal") && portalsTag.contains("Right" + "Portal")) { - portal1 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Left" + "Portal")); - portal2 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Right" + "Portal")); + if (isSnowUp(direction)) { + newPortal1.updatePosition(newPortal1.getX(), newPortal1.getY() - 0.875, newPortal1.getZ()); } - world.playSound(null, - newPortal1.getX(), - newPortal1.getY(), - newPortal1.getZ(), - Portalgun.PORTAL_OPEN_EVENT, - SoundCategory.NEUTRAL, - 1.0F, - 1F); - removeOldPortals(tag, portalsTag, portal1, portal2); - McHelper.spawnServerEntity(newPortal1); - McHelper.spawnServerEntity(newPortal2); - } - waitPortal = false; - if (newPortal2 != null) { - portalsTag.putUuid("Right" + "Portal", newPortal2.getUuid()); - tag.put(world.getRegistryKey().toString(), portalsTag); - } - if (newPortal1 != null) { - portalsTag.putUuid("Left" + "Portal", newPortal1.getUuid()); - tag.put(world.getRegistryKey().toString(), portalsTag); + + if (newPortal2 != null && notSnowUp(direction)) + newPortal1.setDestinationDimension(newPortal2.world.getRegistryKey()); + + + PortalManipulation.adjustRotationToConnect(newPortal1, newPortal2); + + ModMain.serverTaskList.addTask(TaskList.withDelay(delay, TaskList.oneShotTask(() -> { + if (McHelper.getServer().getThread() == Thread.currentThread()) { + if (portalsTag.contains("Left" + "Portal") && portalsTag.contains("Right" + "Portal")) { + portal1 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Left" + "Portal")); + portal2 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Right" + "Portal")); + } + world.playSound(null, + newPortal1.getX(), + newPortal1.getY(), + newPortal1.getZ(), + Portalgun.PORTAL_OPEN_EVENT, + SoundCategory.NEUTRAL, + 1.0F, + 1F); + removeOldPortals(tag, portalsTag, portal1, portal2); + McHelper.spawnServerEntity(newPortal1); + McHelper.spawnServerEntity(newPortal2); + } + waitPortal = false; + if (newPortal2 != null) { + portalsTag.putUuid("Right" + "Portal", newPortal2.getUuid()); + tag.put(world.getRegistryKey().toString(), portalsTag); + } + if (newPortal1 != null) { + portalsTag.putUuid("Left" + "Portal", newPortal1.getUuid()); + tag.put(world.getRegistryKey().toString(), portalsTag); + } + }))); } - }))); + } } user.incrementStat(Stats.USED.getOrCreateStat(this)); } @@ -239,17 +273,18 @@ public TypedActionResult use(World world, PlayerEntity user, Hand han user.getItemCooldownManager().set(this, 4); Entity entity = client.getCameraEntity(); hit = entity.raycast(50.0D, 0.0F, false); + blockHit = (BlockHitResult) hit; + blockPos = blockHit.getBlockPos(); + blockState = world.getBlockState(blockPos); if (hit.getType() == MISS) return TypedActionResult.fail(itemStack); else if (hit.getType() == BLOCK) { - Direction direction = ((BlockHitResult) hit).getSide(); + direction = blockHit.getSide(); - BlockHitResult blockHit = (BlockHitResult) hit; - BlockPos blockPos = blockHit.getBlockPos(); - BlockPos space1BlockPos = blockPos.add(-1, 0, 0); - BlockPos space2BlockPos = blockPos.add(0, -1, 0); - BlockPos space3BlockPos = blockPos.add(0, -1, -1); + space1BlockPos = blockPos.add(-1, 0, 0); + space2BlockPos = blockPos.add(0, -1, 0); + space3BlockPos = blockPos.add(0, -1, -1); switch (direction) { case UP: @@ -279,19 +314,37 @@ else if (hit.getType() == BLOCK) { break; } - dirOut2 = ((BlockHitResult) hit).getSide().getOpposite().getVector(); + dirOut2 = blockHit.getSide().getOpposite().getVector(); if (dirOut2.getY() == 0) { dirUp2 = new Vec3i(0, 1, 0); } else { - dirUp2 = client.player.getHorizontalFacing().getVector(); + dirUp2 = user.getHorizontalFacing().getVector(); } dirRight2 = dirUp2.crossProduct(dirOut2); dirRight2 = new Vec3i(-dirRight2.getX(), -dirRight2.getY(), -dirRight2.getZ()); - if (dirUp2.getZ() == -1) { - space2BlockPos = blockPos.add(0, 0, 1); - space3BlockPos = blockPos.add(0, 1, 1); + if (dirUp2.getY() != 1) { + if (dirUp2.getZ() == -1) { + space2BlockPos = blockPos.add(0, 0, 1); + space3BlockPos = blockPos.add(0, 1, 1); + } + + if (dirRight2.getX() == -1) { + space2BlockPos = blockPos.add(0, 0, -1); + space3BlockPos = blockPos.add(0, 1, -1); + } + + switch (dirRight2.getZ()) { + case 1: + space2BlockPos = blockPos.add(-1, 0, 0); + space3BlockPos = blockPos.add(-1, 1, 0); + break; + case -1: + space2BlockPos = blockPos.add(1, 0, 0); + space3BlockPos = blockPos.add(1, 1, 0); + break; + } } space1BlockState = world.getBlockState(space1BlockPos); @@ -311,66 +364,69 @@ else if (hit.getType() == BLOCK) { client.attackCooldown = 10; client.gameRenderer.firstPersonRenderer.resetEquipProgress(user.getActiveHand()); - if (!world.isClient && !waitPortal && space2BlockState.isOpaque() && !space1BlockState.isOpaque() && !space3BlockState.isOpaque() || space2BlockState.getBlock().is(Blocks.SNOW)) { - - world.playSound(null, - user.getX(), - user.getY(), - user.getZ(), - Portalgun.PORTAL2_SHOOT_EVENT, - SoundCategory.NEUTRAL, - 1.0F, - 1F); + if (!world.isClient) { + if (!waitPortal && !space1BlockState.isOpaque() && space2BlockState.isOpaque() && !space3BlockState.isOpaque()) { - waitPortal = true; + world.playSound(null, + user.getX(), + user.getY(), + user.getZ(), + Portalgun.PORTAL2_SHOOT_EVENT, + SoundCategory.NEUTRAL, + 1.0F, + 1F); - if (portalsTag.contains("Left" + "Portal")) { - newPortal1 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Left" + "Portal")); - if (newPortal1 != null) { - portal1Exists = true; - } - } + waitPortal = true; - if (portalsTag.contains("Right" + "Portal")) { - newPortal2 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Right" + "Portal")); - if (newPortal2 != null) { - portal2Exists = true; + if (portalsTag.contains("Left" + "Portal")) { + newPortal1 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Left" + "Portal")); + if (newPortal1 != null) { + portal1Exists = true; + } } - } - PortalMethods.portal2Methods(user, hit, world); - - PortalManipulation.adjustRotationToConnect(newPortal1, newPortal2); - PortalManipulation.adjustRotationToConnect(newPortal2, newPortal1); - - ModMain.serverTaskList.addTask(TaskList.withDelay(delay, TaskList.oneShotTask(() -> { - if (McHelper.getServer().getThread() == Thread.currentThread()) { - if (portalsTag.contains("Left" + "Portal") && portalsTag.contains("Right" + "Portal")) { - portal1 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Left" + "Portal")); - portal2 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Right" + "Portal")); + if (portalsTag.contains("Right" + "Portal")) { + newPortal2 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Right" + "Portal")); + if (newPortal2 != null) { + portal2Exists = true; } - world.playSound(null, - newPortal2.getX(), - newPortal2.getY(), - newPortal2.getZ(), - Portalgun.PORTAL_OPEN_EVENT, - SoundCategory.NEUTRAL, - 1.0F, - 1F); - removeOldPortals(tag, portalsTag, portal1, portal2); - McHelper.spawnServerEntity(newPortal1); - McHelper.spawnServerEntity(newPortal2); - } - waitPortal = false; - if (newPortal2 != null) { - portalsTag.putUuid("Right" + "Portal", newPortal2.getUuid()); - tag.put(world.getRegistryKey().toString(), portalsTag); } - if (newPortal1 != null) { - portalsTag.putUuid("Left" + "Portal", newPortal1.getUuid()); - tag.put(world.getRegistryKey().toString(), portalsTag); + + if (notSnowUp(direction) || isSnowUp(direction)) { + PortalMethods.portal2Methods(user, hit, world); + + PortalManipulation.adjustRotationToConnect(newPortal1, newPortal2); + + ModMain.serverTaskList.addTask(TaskList.withDelay(delay, TaskList.oneShotTask(() -> { + if (McHelper.getServer().getThread() == Thread.currentThread()) { + if (portalsTag.contains("Left" + "Portal") && portalsTag.contains("Right" + "Portal")) { + portal1 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Left" + "Portal")); + portal2 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Right" + "Portal")); + } + world.playSound(null, + newPortal2.getX(), + newPortal2.getY(), + newPortal2.getZ(), + Portalgun.PORTAL_OPEN_EVENT, + SoundCategory.NEUTRAL, + 1.0F, + 1F); + removeOldPortals(tag, portalsTag, portal1, portal2); + McHelper.spawnServerEntity(newPortal1); + McHelper.spawnServerEntity(newPortal2); + } + waitPortal = false; + if (newPortal2 != null) { + portalsTag.putUuid("Right" + "Portal", newPortal2.getUuid()); + tag.put(world.getRegistryKey().toString(), portalsTag); + } + if (newPortal1 != null) { + portalsTag.putUuid("Left" + "Portal", newPortal1.getUuid()); + tag.put(world.getRegistryKey().toString(), portalsTag); + } + }))); } - }))); + } } user.incrementStat(Stats.USED.getOrCreateStat(this)); } diff --git a/src/main/java/tk/meowmc/portalgun/misc/PortalMethods.java b/src/main/java/tk/meowmc/portalgun/misc/PortalMethods.java index 6a81e3c..ee6c48b 100644 --- a/src/main/java/tk/meowmc/portalgun/misc/PortalMethods.java +++ b/src/main/java/tk/meowmc/portalgun/misc/PortalMethods.java @@ -4,7 +4,6 @@ import com.qouteall.immersive_portals.portal.GeometryPortalShape; import com.qouteall.immersive_portals.portal.Portal; import com.qouteall.immersive_portals.portal.PortalExtension; -import com.qouteall.immersive_portals.portal.PortalManipulation; import net.minecraft.block.Blocks; import net.minecraft.entity.LivingEntity; import net.minecraft.server.world.ServerWorld; @@ -15,7 +14,6 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3i; import net.minecraft.world.World; -import tk.meowmc.portalgun.items.PortalGunItem; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -27,12 +25,12 @@ public class PortalMethods { public static final double TAU = Math.PI*2; //mathematical name for 2 * PI public static final int PORTAL_HEIGHT = 2; public static final int PORTAL_WIDTH = 1; - public static Vec3i dirUp1; - public static Vec3i dirUp2; + public static Vec3i dirUp1; //Portal 1 AxisH + public static Vec3i dirUp2; //Portal 2 AxisH public static Vec3i dirOut1; public static Vec3i dirOut2; - public static Vec3i dirRight1; - public static Vec3i dirRight2; + public static Vec3i dirRight1; //Portal 1 AxisW + public static Vec3i dirRight2; //Portal 2 AxisW static Vec3d portal1AxisW; static Vec3d portal1AxisH; static Vec3d portal2AxisW; @@ -98,12 +96,14 @@ public static Portal Settings1(Direction direction, BlockPos blockPos, HitResult public static Portal Settings2(Direction direction, BlockPos blockPos, HitResult hit, LivingEntity user) { Portal portal = Portal.entityType.create(McHelper.getServer().getWorld(user.world.getRegistryKey())); Vec3d portalPosition = new Vec3d(blockPos.getX(), blockPos.getY(), blockPos.getZ()); - Vec3d destpos = newPortal1.getPos(); PortalExtension portalExtension = PortalExtension.get(portal); portal.dimensionTo = newPortal1 != null ? newPortal1.world.getRegistryKey() : user.world.getRegistryKey(); - portal.setDestination(newPortal1.getPos()); + if (newPortal1 != null) + portal.setDestination(newPortal1.getPos()); + else + portal.setDestination(portalPosition); portal.updatePosition(portalPosition.x, portalPosition.y, portalPosition.z); portalExtension.adjustPositionAfterTeleport = direction == Direction.UP || direction == Direction.DOWN; @@ -140,6 +140,7 @@ public static void portal1Methods(LivingEntity user, HitResult hit, World world) portal1Exists = true; } } + newPortal1 = Settings1(direction, blockPos, hit, user); if (newPortal2 != null) @@ -179,6 +180,12 @@ public static void portal2Methods(LivingEntity user, HitResult hit, World world) portal1AxisH = newPortal1.axisH; } + if (portalsTag.contains("Right" + "Portal")) { + newPortal2 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Right" + "Portal")); + if (newPortal2 != null) { + portal2Exists = true; + } + } newPortal2 = Settings2(direction, blockPos, hit, user); if (portalsTag.contains("Left" + "Portal")) { newPortal1 = (Portal) ((ServerWorld) world).getEntity(portalsTag.getUuid("Left" + "Portal")); @@ -188,12 +195,12 @@ public static void portal2Methods(LivingEntity user, HitResult hit, World world) } newPortal1 = Settings1(direction, blockPos, hit, user); - if (PortalGunItem.space2BlockState.getBlock().is(Blocks.SNOW) && direction == Direction.UP) { + if (space2BlockState.getBlock().is(Blocks.SNOW) && direction == Direction.UP) { newPortal2.updatePosition(newPortal2.getX(), newPortal2.getY() - 0.875, newPortal2.getZ()); } newPortal1.updatePosition(newPortal2.getDestPos().getX(), newPortal2.getDestPos().getY(), newPortal2.getDestPos().getZ()); - newPortal1.setDestination(new Vec3d(newPortal2.getX(), newPortal2.getY(), newPortal2.getZ())); + newPortal1.setDestination(newPortal2.getPos()); newPortal1.setWorld(portal1World); newPortal1.axisW = portal1AxisW; newPortal1.axisH = portal1AxisH;