From 06185d921a9b7a4fe1ca158fffbf09a0fa834c16 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 7 Aug 2024 10:07:54 +0200 Subject: [PATCH 01/11] New methods in CruiseDirection --- .../movecraft/CruiseDirection.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java b/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java index 251ae2daa..134146b5c 100644 --- a/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java +++ b/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java @@ -53,5 +53,33 @@ else if(direction.getOppositeFace() == BlockFace.DOWN) else return NONE; } + + public CruiseDirection getOpposite() { + return switch (this) { + case NORTH -> SOUTH; + case SOUTH -> NORTH; + case EAST -> WEST; + case WEST -> EAST; + case UP -> DOWN; + case DOWN -> UP; + case NONE -> NONE; + }; + } + + public CruiseDirection getClockwiseRotation() { + return switch (this) { + case NORTH -> EAST; + case SOUTH -> WEST; + case EAST -> SOUTH; + case WEST -> NORTH; + case UP -> UP; + case DOWN -> DOWN; + case NONE -> NONE; + }; + } + + public CruiseDirection getAnticlockwiseRotation() { + return this.getClockwiseRotation().getOpposite(); + } } From a3d7166292674e03ef68d3292cb150dcd40015b7 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 7 Aug 2024 10:10:22 +0200 Subject: [PATCH 02/11] Change RotationTask execute to use new methods --- .../async/rotation/RotationTask.java | 39 ++----------------- 1 file changed, 3 insertions(+), 36 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 9e9453f11..c77f71016 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -251,44 +251,11 @@ protected void execute() { } if (getCraft().getCruising()) { + CruiseDirection direction = getCraft().getCruiseDirection(); if (rotation == MovecraftRotation.ANTICLOCKWISE) { - // ship faces west - switch (getCraft().getCruiseDirection()) { - case WEST: - getCraft().setCruiseDirection(CruiseDirection.SOUTH); - break; - // ship faces east - case EAST: - getCraft().setCruiseDirection(CruiseDirection.NORTH); - break; - // ship faces north - case SOUTH: - getCraft().setCruiseDirection(CruiseDirection.EAST); - break; - // ship faces south - case NORTH: - getCraft().setCruiseDirection(CruiseDirection.WEST); - break; - } + getCraft().setCruiseDirection(direction.getAnticlockwiseRotation()); } else if (rotation == MovecraftRotation.CLOCKWISE) { - // ship faces west - switch (getCraft().getCruiseDirection()) { - case WEST: - getCraft().setCruiseDirection(CruiseDirection.NORTH); - break; - // ship faces east - case EAST: - getCraft().setCruiseDirection(CruiseDirection.SOUTH); - break; - // ship faces north - case SOUTH: - getCraft().setCruiseDirection(CruiseDirection.WEST); - break; - // ship faces south - case NORTH: - getCraft().setCruiseDirection(CruiseDirection.EAST); - break; - } + getCraft().setCruiseDirection(direction.getClockwiseRotation()); } } From c797f1f94aaf250da16c434f119a4f14d82f78a0 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 7 Aug 2024 10:13:18 +0200 Subject: [PATCH 03/11] Early return + rename variables --- .../async/rotation/RotationTask.java | 89 ++++++++++--------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index c77f71016..6486d72e2 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -250,59 +250,62 @@ protected void execute() { } } - if (getCraft().getCruising()) { - CruiseDirection direction = getCraft().getCruiseDirection(); + Craft craft1 = getCraft(); + if (craft1.getCruising()) { + CruiseDirection direction = craft1.getCruiseDirection(); if (rotation == MovecraftRotation.ANTICLOCKWISE) { - getCraft().setCruiseDirection(direction.getAnticlockwiseRotation()); + craft1.setCruiseDirection(direction.getAnticlockwiseRotation()); } else if (rotation == MovecraftRotation.CLOCKWISE) { - getCraft().setCruiseDirection(direction.getClockwiseRotation()); + craft1.setCruiseDirection(direction.getClockwiseRotation()); } } // if you rotated a subcraft, update the parent with the new blocks - if (this.isSubCraft) { - // also find the furthest extent from center and notify the player of the new direction - int farthestX = 0; - int farthestZ = 0; - for (MovecraftLocation loc : newHitBox) { - if (Math.abs(loc.getX() - originPoint.getX()) > Math.abs(farthestX)) - farthestX = loc.getX() - originPoint.getX(); - if (Math.abs(loc.getZ() - originPoint.getZ()) > Math.abs(farthestZ)) - farthestZ = loc.getZ() - originPoint.getZ(); + if (!this.isSubCraft) { + return; + } + // also find the furthest extent from center and notify the player of the new direction + int farthestX = 0; + int farthestZ = 0; + for (MovecraftLocation loc : newHitBox) { + if (Math.abs(loc.getX() - originPoint.getX()) > Math.abs(farthestX)) + farthestX = loc.getX() - originPoint.getX(); + if (Math.abs(loc.getZ() - originPoint.getZ()) > Math.abs(farthestZ)) + farthestZ = loc.getZ() - originPoint.getZ(); + } + Component faceMessage = I18nSupport.getInternationalisedComponent("Rotation - Farthest Extent Facing") + .append(Component.text(" ")); + if (Math.abs(farthestX) > Math.abs(farthestZ)) { + if (farthestX > 0) { + faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - East")); + } else { + faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - West")); } - Component faceMessage = I18nSupport.getInternationalisedComponent("Rotation - Farthest Extent Facing") - .append(Component.text(" ")); - if (Math.abs(farthestX) > Math.abs(farthestZ)) { - if (farthestX > 0) { - faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - East")); - } else { - faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - West")); - } + } else { + if (farthestZ > 0) { + faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - South")); } else { - if (farthestZ > 0) { - faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - South")); - } else { - faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - North")); - } + faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - North")); } - getCraft().getAudience().sendMessage(faceMessage); - - craftsInWorld = CraftManager.getInstance().getCraftsInWorld(getCraft().getWorld()); - for (Craft craft : craftsInWorld) { - if (!newHitBox.intersection(craft.getHitBox()).isEmpty() && craft != getCraft()) { - //newHitBox.addAll(CollectionUtils.filter(craft.getHitBox(),newHitBox)); - //craft.setHitBox(newHitBox); - if (Settings.Debug) { - Bukkit.broadcastMessage(String.format("Size of %s hitbox: %d, Size of %s hitbox: %d", this.craft.getType().getStringProperty(CraftType.NAME), newHitBox.size(), craft.getType().getStringProperty(CraftType.NAME), craft.getHitBox().size())); - } - craft.setHitBox(craft.getHitBox().difference(oldHitBox).union(newHitBox)); - if (Settings.Debug){ - Bukkit.broadcastMessage(String.format("Hitbox of craft %s intersects hitbox of craft %s", this.craft.getType().getStringProperty(CraftType.NAME), craft.getType().getStringProperty(CraftType.NAME))); - Bukkit.broadcastMessage(String.format("Size of %s hitbox: %d, Size of %s hitbox: %d", this.craft.getType().getStringProperty(CraftType.NAME), newHitBox.size(), craft.getType().getStringProperty(CraftType.NAME), craft.getHitBox().size())); - } - break; - } + } + craft1.getAudience().sendMessage(faceMessage); + + craftsInWorld = CraftManager.getInstance().getCraftsInWorld(craft1.getWorld()); + for (Craft craft : craftsInWorld) { + if (newHitBox.intersection(craft.getHitBox()).isEmpty() || craft == craft1) { + continue; + } + //newHitBox.addAll(CollectionUtils.filter(craft.getHitBox(),newHitBox)); + //craft.setHitBox(newHitBox); + if (Settings.Debug) { + Bukkit.broadcastMessage(String.format("Size of %s hitbox: %d, Size of %s hitbox: %d", this.craft.getType().getStringProperty(CraftType.NAME), newHitBox.size(), craft.getType().getStringProperty(CraftType.NAME), craft.getHitBox().size())); + } + craft.setHitBox(craft.getHitBox().difference(oldHitBox).union(newHitBox)); + if (Settings.Debug){ + Bukkit.broadcastMessage(String.format("Hitbox of craft %s intersects hitbox of craft %s", this.craft.getType().getStringProperty(CraftType.NAME), craft.getType().getStringProperty(CraftType.NAME))); + Bukkit.broadcastMessage(String.format("Size of %s hitbox: %d, Size of %s hitbox: %d", this.craft.getType().getStringProperty(CraftType.NAME), newHitBox.size(), craft.getType().getStringProperty(CraftType.NAME), craft.getHitBox().size())); } + break; } From 4369e403e6cc43f1dddfb09448d1ecf7d25fd388 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 7 Aug 2024 11:54:28 +0200 Subject: [PATCH 04/11] Extract Rotate function --- .../async/rotation/RotationTask.java | 122 ++++++++++-------- 1 file changed, 66 insertions(+), 56 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 6486d72e2..3ec49ffc3 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -48,12 +48,11 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.HumanEntity; -import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.InventoryView; import java.util.HashSet; -import java.util.Iterator; +import java.util.List; import java.util.Set; import static net.countercraft.movecraft.util.MathUtils.withinWorldBorder; @@ -195,60 +194,7 @@ protected void execute() { tOP.setX(tOP.getBlockX() + 0.5); tOP.setZ(tOP.getBlockZ() + 0.5); - if (!(craft instanceof SinkingCraft && craft.getType().getBoolProperty(CraftType.ONLY_MOVE_PLAYERS)) - && craft.getType().getBoolProperty(CraftType.MOVE_ENTITIES)) { - Location midpoint = new Location( - craft.getWorld(), - (oldHitBox.getMaxX() + oldHitBox.getMinX())/2.0, - (oldHitBox.getMaxY() + oldHitBox.getMinY())/2.0, - (oldHitBox.getMaxZ() + oldHitBox.getMinZ())/2.0); - for(Entity entity : craft.getWorld().getNearbyEntities(midpoint, - oldHitBox.getXLength() / 2.0 + 1, - oldHitBox.getYLength() / 2.0 + 2, - oldHitBox.getZLength() / 2.0 + 1)) { - - if (entity instanceof HumanEntity) { - InventoryView inventoryView = ((HumanEntity) entity).getOpenInventory(); - if (inventoryView.getType() != InventoryType.CRAFTING) { - Location l = Movecraft.getInstance().getWorldHandler().getAccessLocation(inventoryView); - if (l != null) { - MovecraftLocation location = new MovecraftLocation(l.getBlockX(), l.getBlockY(), l.getBlockZ()); - if (oldHitBox.contains(location)) { - location = MathUtils.rotateVec(rotation, location.subtract(originPoint)).add(originPoint); - updates.add(new AccessLocationUpdateCommand(inventoryView, location.toBukkit(w))); - } - } - } - } - - if (!craft.getType().getBoolProperty(CraftType.ONLY_MOVE_PLAYERS) || ( - (entity.getType() == EntityType.PLAYER || entity.getType() == EntityType.PRIMED_TNT) - && !(craft instanceof SinkingCraft) - )) { - // Player is onboard this craft - - Location adjustedPLoc = entity.getLocation().subtract(tOP); - - double[] rotatedCoords = MathUtils.rotateVecNoRound(rotation, - adjustedPLoc.getX(), adjustedPLoc.getZ()); - float newYaw = rotation == MovecraftRotation.CLOCKWISE ? 90F : -90F; - - CraftTeleportEntityEvent e = new CraftTeleportEntityEvent(craft, entity); - Bukkit.getServer().getPluginManager().callEvent(e); - if (e.isCancelled()) - continue; - - EntityUpdateCommand eUp = new EntityUpdateCommand(entity, - rotatedCoords[0] + tOP.getX() - entity.getLocation().getX(), - 0, - rotatedCoords[1] + tOP.getZ() - entity.getLocation().getZ(), - newYaw, - 0 - ); - updates.add(eUp); - } - } - } + rotateEntitiesOnCraft(tOP); Craft craft1 = getCraft(); if (craft1.getCruising()) { @@ -311,6 +257,70 @@ protected void execute() { } + private void rotateEntitiesOnCraft(Location tOP) { + if (craft instanceof SinkingCraft + && craft.getType().getBoolProperty(CraftType.ONLY_MOVE_PLAYERS) + || !craft.getType().getBoolProperty(CraftType.MOVE_ENTITIES)) { + return; + } + + Location midpoint = new Location( + craft.getWorld(), + (oldHitBox.getMaxX() + oldHitBox.getMinX())/2.0, + (oldHitBox.getMaxY() + oldHitBox.getMinY())/2.0, + (oldHitBox.getMaxZ() + oldHitBox.getMinZ())/2.0); + + for(Entity entity : craft.getWorld().getNearbyEntities(midpoint, + oldHitBox.getXLength() / 2.0 + 1, + oldHitBox.getYLength() / 2.0 + 2, + oldHitBox.getZLength() / 2.0 + 1)) { + + if (entity instanceof HumanEntity) { + InventoryView inventoryView = ((HumanEntity) entity).getOpenInventory(); + if (inventoryView.getType() != InventoryType.CRAFTING) { + Location l = Movecraft.getInstance().getWorldHandler().getAccessLocation(inventoryView); + if (l != null) { + MovecraftLocation location = new MovecraftLocation(l.getBlockX(), l.getBlockY(), l.getBlockZ()); + if (oldHitBox.contains(location)) { + location = MathUtils.rotateVec(rotation, location.subtract(originPoint)).add(originPoint); + updates.add(new AccessLocationUpdateCommand(inventoryView, location.toBukkit(w))); + } + } + } + } + + if (craft.getType().getBoolProperty(CraftType.ONLY_MOVE_PLAYERS) + && ((List.of(EntityType.PLAYER, EntityType.PRIMED_TNT).contains(entity.getType())) + || craft instanceof SinkingCraft)) { + continue; + } + + + // Player is onboard this craft + + Location adjustedPLoc = entity.getLocation().subtract(tOP); + + double[] rotatedCoords = MathUtils.rotateVecNoRound(rotation, + adjustedPLoc.getX(), adjustedPLoc.getZ()); + float newYaw = rotation == MovecraftRotation.CLOCKWISE ? 90F : -90F; + + CraftTeleportEntityEvent e = new CraftTeleportEntityEvent(craft, entity); + Bukkit.getServer().getPluginManager().callEvent(e); + if (e.isCancelled()) + continue; + + EntityUpdateCommand eUp = new EntityUpdateCommand(entity, + rotatedCoords[0] + tOP.getX() - entity.getLocation().getX(), + 0, + rotatedCoords[1] + tOP.getZ() - entity.getLocation().getZ(), + newYaw, + 0 + ); + updates.add(eUp); + } + + } + public MovecraftLocation getOriginPoint() { return originPoint; } From cfa9c1072bb861c1b5ee28bbfaec79d510c99b78 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 7 Aug 2024 11:56:01 +0200 Subject: [PATCH 05/11] Extract Rotate Human Entity --- .../async/rotation/RotationTask.java | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 3ec49ffc3..992e1b910 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -275,19 +275,7 @@ private void rotateEntitiesOnCraft(Location tOP) { oldHitBox.getYLength() / 2.0 + 2, oldHitBox.getZLength() / 2.0 + 1)) { - if (entity instanceof HumanEntity) { - InventoryView inventoryView = ((HumanEntity) entity).getOpenInventory(); - if (inventoryView.getType() != InventoryType.CRAFTING) { - Location l = Movecraft.getInstance().getWorldHandler().getAccessLocation(inventoryView); - if (l != null) { - MovecraftLocation location = new MovecraftLocation(l.getBlockX(), l.getBlockY(), l.getBlockZ()); - if (oldHitBox.contains(location)) { - location = MathUtils.rotateVec(rotation, location.subtract(originPoint)).add(originPoint); - updates.add(new AccessLocationUpdateCommand(inventoryView, location.toBukkit(w))); - } - } - } - } + rotateHumanEntity(entity); if (craft.getType().getBoolProperty(CraftType.ONLY_MOVE_PLAYERS) && ((List.of(EntityType.PLAYER, EntityType.PRIMED_TNT).contains(entity.getType())) @@ -318,7 +306,30 @@ private void rotateEntitiesOnCraft(Location tOP) { ); updates.add(eUp); } + } + + private void rotateHumanEntity(Entity entity) { + if (!(entity instanceof HumanEntity)) { + return; + } + + InventoryView inventoryView = ((HumanEntity) entity).getOpenInventory(); + if (inventoryView.getType() == InventoryType.CRAFTING) { + return; + } + + Location l = Movecraft.getInstance().getWorldHandler().getAccessLocation(inventoryView); + if (l == null) { + return; + } + + MovecraftLocation location = new MovecraftLocation(l.getBlockX(), l.getBlockY(), l.getBlockZ()); + if (!oldHitBox.contains(location)) { + return; + } + location = MathUtils.rotateVec(rotation, location.subtract(originPoint)).add(originPoint); + updates.add(new AccessLocationUpdateCommand(inventoryView, location.toBukkit(w))); } public MovecraftLocation getOriginPoint() { From 155fd5c21fc5b7fb0517364e6aa400ed646991ec Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 7 Aug 2024 12:13:53 +0200 Subject: [PATCH 06/11] checkChests refactor --- .../async/rotation/RotationTask.java | 45 ++++++++----------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 992e1b910..20e16b25f 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -199,10 +199,9 @@ protected void execute() { Craft craft1 = getCraft(); if (craft1.getCruising()) { CruiseDirection direction = craft1.getCruiseDirection(); - if (rotation == MovecraftRotation.ANTICLOCKWISE) { - craft1.setCruiseDirection(direction.getAnticlockwiseRotation()); - } else if (rotation == MovecraftRotation.CLOCKWISE) { - craft1.setCruiseDirection(direction.getClockwiseRotation()); + switch (rotation) { + case ANTICLOCKWISE -> craft1.setCruiseDirection(direction.getAnticlockwiseRotation()); + case CLOCKWISE -> craft1.setCruiseDirection(direction.getClockwiseRotation()); } } @@ -254,7 +253,6 @@ protected void execute() { break; } - } private void rotateEntitiesOnCraft(Location tOP) { @@ -359,37 +357,32 @@ public boolean getIsSubCraft() { private boolean checkChests(Material mBlock, MovecraftLocation newLoc) { Material testMaterial; MovecraftLocation aroundNewLoc; + final World world = craft.getWorld(); aroundNewLoc = newLoc.translate(1, 0, 0); - testMaterial = craft.getWorld().getBlockAt(aroundNewLoc.getX(), aroundNewLoc.getY(), aroundNewLoc.getZ()).getType(); - if (testMaterial.equals(mBlock)) { - if (!oldHitBox.contains(aroundNewLoc)) { - return false; - } - } + testMaterial = world.getBlockAt(aroundNewLoc.getX(), aroundNewLoc.getY(), aroundNewLoc.getZ()).getType(); + if (checkOldHitBox(testMaterial, mBlock, aroundNewLoc)) + return false; aroundNewLoc = newLoc.translate(-1, 0, 0); - testMaterial = craft.getWorld().getBlockAt(aroundNewLoc.getX(), aroundNewLoc.getY(), aroundNewLoc.getZ()).getType(); - if (testMaterial.equals(mBlock)) { - if (!oldHitBox.contains(aroundNewLoc)) { - return false; - } - } + testMaterial = world.getBlockAt(aroundNewLoc.getX(), aroundNewLoc.getY(), aroundNewLoc.getZ()).getType(); + if (checkOldHitBox(testMaterial, mBlock, aroundNewLoc)) + return false; aroundNewLoc = newLoc.translate(0, 0, 1); - testMaterial = craft.getWorld().getBlockAt(aroundNewLoc.getX(), aroundNewLoc.getY(), aroundNewLoc.getZ()).getType(); - if (testMaterial.equals(mBlock)) { - if (!oldHitBox.contains(aroundNewLoc)) { - return false; - } - } + testMaterial = world.getBlockAt(aroundNewLoc.getX(), aroundNewLoc.getY(), aroundNewLoc.getZ()).getType(); + + if (checkOldHitBox(testMaterial, mBlock, aroundNewLoc)) + return false; aroundNewLoc = newLoc.translate(0, 0, -1); - testMaterial = craft.getWorld().getBlockAt(aroundNewLoc.getX(), aroundNewLoc.getY(), aroundNewLoc.getZ()).getType(); - return !testMaterial.equals(mBlock) || oldHitBox.contains(aroundNewLoc); + testMaterial = world.getBlockAt(aroundNewLoc.getX(), aroundNewLoc.getY(), aroundNewLoc.getZ()).getType(); + return !checkOldHitBox(testMaterial, mBlock, aroundNewLoc); } - + private boolean checkOldHitBox(Material testMaterial, Material mBlock, MovecraftLocation aroundNewLoc) { + return testMaterial.equals(mBlock) && !oldHitBox.contains(aroundNewLoc); + } public MutableHitBox getNewHitBox() { return newHitBox; From 347c4e061625ba81b9f9cea9221111f8ee791c3b Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 7 Aug 2024 12:19:21 +0200 Subject: [PATCH 07/11] Extract rotation message --- .../async/rotation/RotationTask.java | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 20e16b25f..2846891df 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -220,19 +220,8 @@ protected void execute() { } Component faceMessage = I18nSupport.getInternationalisedComponent("Rotation - Farthest Extent Facing") .append(Component.text(" ")); - if (Math.abs(farthestX) > Math.abs(farthestZ)) { - if (farthestX > 0) { - faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - East")); - } else { - faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - West")); - } - } else { - if (farthestZ > 0) { - faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - South")); - } else { - faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - North")); - } - } + + faceMessage = faceMessage.append(getRotationMessage(farthestX, farthestZ)); craft1.getAudience().sendMessage(faceMessage); craftsInWorld = CraftManager.getInstance().getCraftsInWorld(craft1.getWorld()); @@ -255,6 +244,22 @@ protected void execute() { } + private Component getRotationMessage(int farthestX, int farthestZ) { + if (Math.abs(farthestX) > Math.abs(farthestZ)) { + if (farthestX > 0) { + return I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - East"); + } else { + return I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - West"); + } + } else { + if (farthestZ > 0) { + return I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - South"); + } else { + return I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - North"); + } + } + } + private void rotateEntitiesOnCraft(Location tOP) { if (craft instanceof SinkingCraft && craft.getType().getBoolProperty(CraftType.ONLY_MOVE_PLAYERS) From 3904a067c16aef8a7f3f9f45380613a3ced88b58 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 7 Aug 2024 14:08:30 +0200 Subject: [PATCH 08/11] Correction Entity List --- .../movecraft/async/rotation/RotationTask.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 2846891df..300c80443 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -273,6 +273,7 @@ private void rotateEntitiesOnCraft(Location tOP) { (oldHitBox.getMaxY() + oldHitBox.getMinY())/2.0, (oldHitBox.getMaxZ() + oldHitBox.getMinZ())/2.0); + List entityList = List.of(EntityType.PLAYER, EntityType.PRIMED_TNT); for(Entity entity : craft.getWorld().getNearbyEntities(midpoint, oldHitBox.getXLength() / 2.0 + 1, oldHitBox.getYLength() / 2.0 + 2, @@ -281,13 +282,10 @@ private void rotateEntitiesOnCraft(Location tOP) { rotateHumanEntity(entity); if (craft.getType().getBoolProperty(CraftType.ONLY_MOVE_PLAYERS) - && ((List.of(EntityType.PLAYER, EntityType.PRIMED_TNT).contains(entity.getType())) + && (!entityList.contains(entity.getType()) || craft instanceof SinkingCraft)) { continue; - } - - - // Player is onboard this craft + }// Player is onboard this craft Location adjustedPLoc = entity.getLocation().subtract(tOP); @@ -308,6 +306,8 @@ private void rotateEntitiesOnCraft(Location tOP) { 0 ); updates.add(eUp); + + } } From 8d198e88fbbba05aed6043a313dd04b98997e25d Mon Sep 17 00:00:00 2001 From: Intybyte Date: Sat, 17 Aug 2024 19:13:50 +0200 Subject: [PATCH 09/11] Fix rotateEntitiesOnCraft --- .../countercraft/movecraft/async/rotation/RotationTask.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 300c80443..9ad8ed35b 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -261,9 +261,9 @@ private Component getRotationMessage(int farthestX, int farthestZ) { } private void rotateEntitiesOnCraft(Location tOP) { - if (craft instanceof SinkingCraft - && craft.getType().getBoolProperty(CraftType.ONLY_MOVE_PLAYERS) - || !craft.getType().getBoolProperty(CraftType.MOVE_ENTITIES)) { + if (!craft.getType().getBoolProperty(CraftType.MOVE_ENTITIES) + || (craft instanceof SinkingCraft + && craft.getType().getBoolProperty(CraftType.ONLY_MOVE_PLAYERS))) { return; } From 4800bfb54e5263a432ef45ab10853ca847d3f3e1 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Sat, 17 Aug 2024 19:20:52 +0200 Subject: [PATCH 10/11] Merge clockwise and anticlockwise rotation --- .../async/rotation/RotationTask.java | 5 +--- .../movecraft/CruiseDirection.java | 24 +++++++++---------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 9ad8ed35b..327e9e86c 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -199,10 +199,7 @@ protected void execute() { Craft craft1 = getCraft(); if (craft1.getCruising()) { CruiseDirection direction = craft1.getCruiseDirection(); - switch (rotation) { - case ANTICLOCKWISE -> craft1.setCruiseDirection(direction.getAnticlockwiseRotation()); - case CLOCKWISE -> craft1.setCruiseDirection(direction.getClockwiseRotation()); - } + craft1.setCruiseDirection(direction.getRotated(rotation)); } // if you rotated a subcraft, update the parent with the new blocks diff --git a/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java b/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java index 134146b5c..a83e0aa7b 100644 --- a/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java +++ b/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java @@ -66,20 +66,18 @@ public CruiseDirection getOpposite() { }; } - public CruiseDirection getClockwiseRotation() { - return switch (this) { - case NORTH -> EAST; - case SOUTH -> WEST; - case EAST -> SOUTH; - case WEST -> NORTH; - case UP -> UP; - case DOWN -> DOWN; - case NONE -> NONE; + public CruiseDirection getRotated(MovecraftRotation rotation) { + return switch(rotation) { + case CLOCKWISE -> switch (this) { + case NORTH -> EAST; + case SOUTH -> WEST; + case EAST -> SOUTH; + case WEST -> NORTH; + default -> this; + }; + case ANTICLOCKWISE -> getRotated(MovecraftRotation.CLOCKWISE).getOpposite(); + case NONE -> this; }; } - - public CruiseDirection getAnticlockwiseRotation() { - return this.getClockwiseRotation().getOpposite(); - } } From 14610e93a1a67b5673c74f10abc00c2117ba9dfb Mon Sep 17 00:00:00 2001 From: Intybyte Date: Sat, 17 Aug 2024 19:23:47 +0200 Subject: [PATCH 11/11] Refactor `fromBlockFace` method --- .../movecraft/CruiseDirection.java | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java b/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java index a83e0aa7b..b569903a3 100644 --- a/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java +++ b/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java @@ -38,20 +38,15 @@ else if(rawDirection == (byte) 0x43) } public static CruiseDirection fromBlockFace(BlockFace direction) { - if(direction.getOppositeFace() == BlockFace.NORTH) - return NORTH; - else if(direction.getOppositeFace() == BlockFace.SOUTH) - return SOUTH; - else if(direction.getOppositeFace() == BlockFace.EAST) - return EAST; - else if(direction.getOppositeFace() == BlockFace.WEST) - return WEST; - else if(direction.getOppositeFace() == BlockFace.UP) - return UP; - else if(direction.getOppositeFace() == BlockFace.DOWN) - return DOWN; - else - return NONE; + return switch (direction.getOppositeFace()) { + case NORTH -> NORTH; + case SOUTH -> SOUTH; + case EAST -> EAST; + case WEST -> WEST; + case UP -> UP; + case DOWN -> DOWN; + default -> NONE; + }; } public CruiseDirection getOpposite() {