Move floatAt logic to AbstractPlayer, add isAllowedToFly and setFlying.

This commit is contained in:
wizjany 2019-07-11 20:27:05 -04:00
parent 8545417b3a
commit 2cc6a367c6
6 changed files with 54 additions and 36 deletions

View File

@ -185,14 +185,13 @@ public class BukkitPlayer extends AbstractPlayerActor {
} }
@Override @Override
public void floatAt(int x, int y, int z, boolean alwaysGlass) { public boolean isAllowedToFly() {
if (alwaysGlass || !player.getAllowFlight()) { return player.getAllowFlight();
super.floatAt(x, y, z, alwaysGlass); }
return;
}
setPosition(Vector3.at(x + 0.5, y, z + 0.5)); @Override
player.setFlying(true); public void setFlying(boolean flying) {
player.setFlying(flying);
} }
@Override @Override

View File

@ -187,6 +187,24 @@ public interface Player extends Entity, Actor {
*/ */
void floatAt(int x, int y, int z, boolean alwaysGlass); 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. * Get the point of the block that is being stood in.
* *

View File

@ -313,13 +313,17 @@ public abstract class AbstractPlayerActor implements Actor, Player, Cloneable {
@Override @Override
public void floatAt(int x, int y, int z, boolean alwaysGlass) { public void floatAt(int x, int y, int z, boolean alwaysGlass) {
BlockVector3 spot = BlockVector3.at(x, y - 1, z); if (alwaysGlass || !isAllowedToFly()) {
final World world = (World) getLocation().getExtent(); BlockVector3 spot = BlockVector3.at(x, y - 1, z);
if (!world.getBlock(spot).getBlockType().getMaterial().isMovementBlocker()) { final World world = getWorld();
try (EditSession session = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, 1, this)) { if (!world.getBlock(spot).getBlockType().getMaterial().isMovementBlocker()) {
session.setBlock(spot, BlockTypes.GLASS.getDefaultState()); try (EditSession session = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, 1, this)) {
} catch (MaxChangedBlocksException ignored) { session.setBlock(spot, BlockTypes.GLASS.getDefaultState());
} catch (MaxChangedBlocksException ignored) {
}
} }
} else {
setFlying(true);
} }
setPosition(Vector3.at(x + 0.5, y, z + 0.5)); setPosition(Vector3.at(x + 0.5, y, z + 0.5));
} }

View File

@ -189,15 +189,14 @@ public class FabricPlayer extends AbstractPlayerActor {
} }
@Override @Override
public void floatAt(int x, int y, int z, boolean alwaysGlass) { public boolean isAllowedToFly() {
if (alwaysGlass || !player.abilities.allowFlying) { return player.abilities.allowFlying;
super.floatAt(x, y, z, alwaysGlass); }
return;
}
setPosition(Vector3.at(x + 0.5, y, z + 0.5)); @Override
if (!player.abilities.flying) { public void setFlying(boolean flying) {
player.abilities.flying = true; if (player.abilities.flying != flying) {
player.abilities.flying = flying;
player.sendAbilitiesUpdate(); player.sendAbilitiesUpdate();
} }
} }

View File

@ -190,15 +190,14 @@ public class ForgePlayer extends AbstractPlayerActor {
} }
@Override @Override
public void floatAt(int x, int y, int z, boolean alwaysGlass) { public boolean isAllowedToFly() {
if (alwaysGlass || !player.abilities.allowFlying) { return player.abilities.allowFlying;
super.floatAt(x, y, z, alwaysGlass); }
return;
}
setPosition(Vector3.at(x + 0.5, y, z + 0.5)); @Override
if (!player.abilities.isFlying) { public void setFlying(boolean flying) {
player.abilities.isFlying = true; if (player.abilities.isFlying != flying) {
player.abilities.isFlying = flying;
player.sendPlayerAbilities(); player.sendPlayerAbilities();
} }
} }

View File

@ -204,14 +204,13 @@ public class SpongePlayer extends AbstractPlayerActor {
} }
@Override @Override
public void floatAt(int x, int y, int z, boolean alwaysGlass) { public boolean isAllowedToFly() {
if (alwaysGlass || !player.get(Keys.CAN_FLY).orElse(false)) { return player.get(Keys.CAN_FLY).orElse(super.isAllowedToFly());
super.floatAt(x, y, z, alwaysGlass); }
return;
}
setPosition(Vector3.at(x + 0.5, y, z + 0.5)); @Override
player.offer(Keys.IS_FLYING, true); public void setFlying(boolean flying) {
player.offer(Keys.IS_FLYING, flying);
} }
@Override @Override