Skip to content

Commit

Permalink
Move floatAt logic to AbstractPlayer, add isAllowedToFly and setFlying.
Browse files Browse the repository at this point in the history
  • Loading branch information
wizjany committed Jul 12, 2019
1 parent 04bb677 commit 567174b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 36 deletions.
Expand Up @@ -185,14 +185,13 @@ public Player getPlayer() {
}

@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
Expand Down
Expand Up @@ -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.
*
Expand Down
Expand Up @@ -313,13 +313,17 @@ public boolean ascendUpwards(int distance, boolean alwaysGlass) {

@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 = (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) {
}
}
} else {
setFlying(true);
}
setPosition(Vector3.at(x + 0.5, y, z + 0.5));
}
Expand Down
Expand Up @@ -189,15 +189,14 @@ public <T> T getFacet(Class<? extends T> cls) {
}

@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();
}
}
Expand Down
Expand Up @@ -190,15 +190,14 @@ public <T> T getFacet(Class<? extends T> cls) {
}

@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();
}
}
Expand Down
Expand Up @@ -204,14 +204,13 @@ public void setGameMode(GameMode gameMode) {
}

@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
Expand Down

0 comments on commit 567174b

Please sign in to comment.