From 2cc6a367c623183200ee59b3e6e337315ce67290 Mon Sep 17 00:00:00 2001 From: wizjany Date: Thu, 11 Jul 2019 20:27:05 -0400 Subject: [PATCH] Move floatAt logic to AbstractPlayer, add isAllowedToFly and setFlying. --- .../sk89q/worldedit/bukkit/BukkitPlayer.java | 13 ++++++------- .../com/sk89q/worldedit/entity/Player.java | 18 ++++++++++++++++++ .../platform/AbstractPlayerActor.java | 16 ++++++++++------ .../sk89q/worldedit/fabric/FabricPlayer.java | 15 +++++++-------- .../com/sk89q/worldedit/forge/ForgePlayer.java | 15 +++++++-------- .../sk89q/worldedit/sponge/SpongePlayer.java | 13 ++++++------- 6 files changed, 54 insertions(+), 36 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java index b5cd6bd37..3e12279ea 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java @@ -185,14 +185,13 @@ public class BukkitPlayer extends AbstractPlayerActor { } @Override - public void floatAt(int x, int y, int z, boolean alwaysGlass) { - if (alwaysGlass || !player.getAllowFlight()) { - super.floatAt(x, y, z, alwaysGlass); - return; - } + public boolean isAllowedToFly() { + return player.getAllowFlight(); + } - setPosition(Vector3.at(x + 0.5, y, z + 0.5)); - player.setFlying(true); + @Override + public void setFlying(boolean flying) { + player.setFlying(flying); } @Override diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/entity/Player.java b/worldedit-core/src/main/java/com/sk89q/worldedit/entity/Player.java index 3673d8842..a51864ee4 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/entity/Player.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/entity/Player.java @@ -187,6 +187,24 @@ public interface Player extends Entity, Actor { */ void floatAt(int x, int y, int z, boolean alwaysGlass); + /** + * Check whether the player is allowed to fly. + * + * @return true if allowed flight + */ + default boolean isAllowedToFly() { + return false; + } + + /** + * Set whether the player is currently flying. + * + * @param flying true to fly + */ + default void setFlying(boolean flying) { + throw new UnsupportedOperationException("setFlying unimplemented but isAllowedToFly was true (or unchecked)"); + } + /** * Get the point of the block that is being stood in. * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/AbstractPlayerActor.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/AbstractPlayerActor.java index cea7d8d47..fb70c77ba 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/AbstractPlayerActor.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/AbstractPlayerActor.java @@ -313,13 +313,17 @@ public abstract class AbstractPlayerActor implements Actor, Player, Cloneable { @Override public void floatAt(int x, int y, int z, boolean alwaysGlass) { - BlockVector3 spot = BlockVector3.at(x, y - 1, z); - final World world = (World) getLocation().getExtent(); - if (!world.getBlock(spot).getBlockType().getMaterial().isMovementBlocker()) { - try (EditSession session = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, 1, this)) { - session.setBlock(spot, BlockTypes.GLASS.getDefaultState()); - } catch (MaxChangedBlocksException ignored) { + if (alwaysGlass || !isAllowedToFly()) { + BlockVector3 spot = BlockVector3.at(x, y - 1, z); + final World world = getWorld(); + if (!world.getBlock(spot).getBlockType().getMaterial().isMovementBlocker()) { + try (EditSession session = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, 1, this)) { + session.setBlock(spot, BlockTypes.GLASS.getDefaultState()); + } catch (MaxChangedBlocksException ignored) { + } } + } else { + setFlying(true); } setPosition(Vector3.at(x + 0.5, y, z + 0.5)); } diff --git a/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricPlayer.java b/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricPlayer.java index 2d0681c1b..6bd1171d7 100644 --- a/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricPlayer.java +++ b/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricPlayer.java @@ -189,15 +189,14 @@ public class FabricPlayer extends AbstractPlayerActor { } @Override - public void floatAt(int x, int y, int z, boolean alwaysGlass) { - if (alwaysGlass || !player.abilities.allowFlying) { - super.floatAt(x, y, z, alwaysGlass); - return; - } + public boolean isAllowedToFly() { + return player.abilities.allowFlying; + } - setPosition(Vector3.at(x + 0.5, y, z + 0.5)); - if (!player.abilities.flying) { - player.abilities.flying = true; + @Override + public void setFlying(boolean flying) { + if (player.abilities.flying != flying) { + player.abilities.flying = flying; player.sendAbilitiesUpdate(); } } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java index e55761da3..94dfb8c10 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java @@ -190,15 +190,14 @@ public class ForgePlayer extends AbstractPlayerActor { } @Override - public void floatAt(int x, int y, int z, boolean alwaysGlass) { - if (alwaysGlass || !player.abilities.allowFlying) { - super.floatAt(x, y, z, alwaysGlass); - return; - } + public boolean isAllowedToFly() { + return player.abilities.allowFlying; + } - setPosition(Vector3.at(x + 0.5, y, z + 0.5)); - if (!player.abilities.isFlying) { - player.abilities.isFlying = true; + @Override + public void setFlying(boolean flying) { + if (player.abilities.isFlying != flying) { + player.abilities.isFlying = flying; player.sendPlayerAbilities(); } } diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePlayer.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePlayer.java index 45fc43000..3a5f94933 100644 --- a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePlayer.java +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePlayer.java @@ -204,14 +204,13 @@ public class SpongePlayer extends AbstractPlayerActor { } @Override - public void floatAt(int x, int y, int z, boolean alwaysGlass) { - if (alwaysGlass || !player.get(Keys.CAN_FLY).orElse(false)) { - super.floatAt(x, y, z, alwaysGlass); - return; - } + public boolean isAllowedToFly() { + return player.get(Keys.CAN_FLY).orElse(super.isAllowedToFly()); + } - setPosition(Vector3.at(x + 0.5, y, z + 0.5)); - player.offer(Keys.IS_FLYING, true); + @Override + public void setFlying(boolean flying) { + player.offer(Keys.IS_FLYING, flying); } @Override