Skip to content

Commit

Permalink
## #43: Prevent players from swimming up waterfalls
Browse files Browse the repository at this point in the history
[Problem]

Players can swim up waterfalls in areas where we want waterfalls as decoration but also need to restrict access.

[Solution]

Well there is none so far, since we tested and setting a player's hunger to 6 or lower does not work, since it is not implemented in Minestom to stop players from swimming or sprinting in this state.

Also added some comments about potential structuring of handling these types of events when we have a solution.
  • Loading branch information
Nixotica committed Sep 23, 2022
1 parent 880b03e commit 30ddb62
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,28 @@ public static void main(String[] args) {
player.setAllowFlying(true);

// Testing
event.getSpawnInstance().setBlock(5, 43, 5, Ore.fromNamespaceId("unnamed:gold_ore").asBlock());
event.getSpawnInstance().setBlock(4, 43, 5, Ore.fromNamespaceId("unnamed:diamond_ore").asBlock());
player.getInventory().addItemStack(Item.fromNamespaceId("unnamed:diamond_pickaxe").asItemStack());
event.getSpawnInstance().setBlock(5, 43, 5, Ore.fromNamespaceId("starlight:gold_ore").asBlock());
event.getSpawnInstance().setBlock(4, 43, 5, Ore.fromNamespaceId("starlight:diamond_ore").asBlock());

event.getSpawnInstance().setBlock(10, 40, 5, Block.BUBBLE_COLUMN);
event.getSpawnInstance().setBlock(10, 41, 5, Block.BUBBLE_COLUMN);
event.getSpawnInstance().setBlock(10, 42, 5, Block.BUBBLE_COLUMN);
event.getSpawnInstance().setBlock(10, 43, 5, Block.BUBBLE_COLUMN);
event.getSpawnInstance().setBlock(10, 44, 5, Block.BUBBLE_COLUMN);
event.getSpawnInstance().setBlock(10, 45, 5, Block.BUBBLE_COLUMN);
event.getSpawnInstance().setBlock(10, 46, 5, Block.BUBBLE_COLUMN);
event.getSpawnInstance().setBlock(10, 47, 5, Block.BUBBLE_COLUMN);
event.getSpawnInstance().setBlock(10, 48, 5, Block.BUBBLE_COLUMN);

event.getSpawnInstance().setBlock(11, 40, 5, Block.WATER);
event.getSpawnInstance().setBlock(11, 41, 5, Block.WATER);
event.getSpawnInstance().setBlock(11, 42, 5, Block.WATER);
event.getSpawnInstance().setBlock(11, 43, 5, Block.WATER);
event.getSpawnInstance().setBlock(11, 44, 5, Block.WATER);
event.getSpawnInstance().setBlock(11, 45, 5, Block.WATER);
event.getSpawnInstance().setBlock(11, 46, 5, Block.WATER);
event.getSpawnInstance().setBlock(11, 47, 5, Block.WATER);
event.getSpawnInstance().setBlock(11, 48, 5, Block.WATER);

//todo this needs to be done elsewhere
player.addEffect(new Potion(PotionEffect.MINING_FATIGUE, (byte) -1, Short.MAX_VALUE, (byte) 0x0));
Expand Down
41 changes: 41 additions & 0 deletions modules/player/src/main/java/net/hollowcube/player/PlayerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.hollowcube.modifiers.ModifierList;
import net.hollowcube.modifiers.ModifierOperation;
import net.hollowcube.modifiers.ModifierType;
import net.hollowcube.player.event.PlayerBubbleColumnSinkEvent;
import net.hollowcube.player.event.PlayerLongDiggingStartEvent;
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Point;
Expand All @@ -13,6 +14,7 @@
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.network.packet.client.ClientPacket;
import net.minestom.server.network.packet.client.play.ClientPlayerDiggingPacket;
import net.minestom.server.network.packet.client.play.ClientPlayerPositionPacket;
import net.minestom.server.network.packet.server.play.BlockBreakAnimationPacket;
import net.minestom.server.network.player.PlayerConnection;
import org.jetbrains.annotations.NotNull;
Expand All @@ -34,6 +36,10 @@ public class PlayerImpl extends Player {
private int diggingBlockMaxHealth = 0;
private BlockFace diggingFace = null;

// Bubble column sink event state
private int bubbleColumnSinkEnergyLevel = -1;
private boolean wasLastSeenInBubbleColumn = false;

private final Map<String, ModifierList> currentModifiers = new HashMap<>();

private final Logger logger = LoggerFactory.getLogger(PlayerImpl.class);
Expand All @@ -58,6 +64,10 @@ private void handlePacket(PlayerPacketEvent event) {
if (packet instanceof ClientPlayerDiggingPacket diggingPacket) {
handleDiggingPacket(diggingPacket);
}

if (packet instanceof ClientPlayerPositionPacket positionPacket) {
handlePositionPacket(positionPacket);
}
}

private void handleDiggingPacket(ClientPlayerDiggingPacket packet) {
Expand Down Expand Up @@ -94,6 +104,37 @@ private void handleDiggingPacket(ClientPlayerDiggingPacket packet) {
}
}

private void handlePositionPacket(ClientPlayerPositionPacket packet) {
if (packet.onGround()) {

}
else {
if (this.getInstance().getBlock(this.getPosition()) == Block.BUBBLE_COLUMN) {
//TODO: I think we want to make this part of a whole PlayerInWaterHandler or something, and on top of that,
// we would have to make a BlockComponentHandler or something to encapsulate it. I wonder if @Matt has a
// suggestion on this.

// PlayerBubbleColumnSinkEvent playerBubbleColumnSinkEvent = new PlayerBubbleColumnSinkEvent(this, this.getFood());
// MinecraftServer.getGlobalEventHandler().call(playerBubbleColumnSinkEvent);

if (!this.wasLastSeenInBubbleColumn) {
this.wasLastSeenInBubbleColumn = true;
this.bubbleColumnSinkEnergyLevel = this.getFood();
//TODO: This does not work lol it is not implemented in minestom to get rid of the player's
// ability to sprint when they have hunger below 7
this.setFood(6);
}
}
else if (this.wasLastSeenInBubbleColumn) {
if (this.getInstance().getBlock(this.getPosition()) != Block.BUBBLE_COLUMN) {
this.wasLastSeenInBubbleColumn = false;
this.setFood(this.bubbleColumnSinkEnergyLevel);
this.bubbleColumnSinkEnergyLevel = -1;
}
}
}
}

private void tickLongDigging() {
if (diggingBlock == null) return;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.hollowcube.player.event;

import net.minestom.server.entity.Player;
import net.minestom.server.event.trait.PlayerInstanceEvent;
import net.minestom.server.instance.block.Block;
import org.jetbrains.annotations.NotNull;

public class PlayerBubbleColumnSinkEvent implements PlayerInstanceEvent {
private final Player player;
private final int energyLevel;

public PlayerBubbleColumnSinkEvent(Player player, int energyLevel) {
this.player = player;
this.energyLevel = energyLevel;
}

@Override
public @NotNull Player getPlayer() {
return player;
}

public int getEnergyLevel() {
return energyLevel;
}

public void cancelPlayerSwimming() {
if (player.getInstance().getBlock(player.getPosition()) == Block.BUBBLE_COLUMN) {
player.setFood(6);
}
}

public void restorePlayerEnergy() {
if (player.getInstance().getBlock(player.getPosition()) != Block.BUBBLE_COLUMN) {
player.setFood(energyLevel);
}
}
}

0 comments on commit 30ddb62

Please sign in to comment.