Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

## #43: Prevent players from swimming up waterfalls #46

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove these changes, as I am making this change in a separate PR.

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) {
Nixotica marked this conversation as resolved.
Show resolved Hide resolved
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure if an event is strictly necessary right now. It may be useful in the future, but I'm uncertain if it needs to be included here.

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);
}
}
}