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 all commits
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
2 changes: 2 additions & 0 deletions modules/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ subprojects {
apply(plugin = "java")
apply(plugin = "net.ltgt.errorprone")

group = "net.hollowcube.libmmo"

repositories {
mavenCentral()
maven(url = "https://jitpack.io")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.hollowcube.registry;

import org.jetbrains.annotations.NotNull;

public class MissingEntryException extends RuntimeException{
private final @NotNull Registry<?> registry;
private final @NotNull String key;

public MissingEntryException(@NotNull Registry<?> registry, @NotNull String key) {
super("Missing registry entry: " + key + " in " + registry + "!");
this.registry = registry;
this.key = key;
}

public @NotNull Registry<?> registry() {
return registry;
}

public @NotNull String key() {
return key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ static <T extends Resource> Registry<T> codec(@NotNull String name, @NotNull Cod

default @NotNull T required(String namespace) {
T result = get(namespace);
Check.notNull(result, "Missing required registry entry: " + namespace);
if (result == null) throw new MissingEntryException(this, namespace);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.hollowcube.registry;

import com.google.gson.JsonParser;
import com.mojang.serialization.JsonOps;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class TestMissingEntry {

@Test
public void testMissingRegistryEntry() {
var json = JsonParser.parseString("{\"type\": \"missing\"}");
assertThrows(MissingEntryException.class, () -> JsonOps.INSTANCE.withDecoder(TestResource.CODEC).apply(json));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.hollowcube.registry;

import com.mojang.serialization.Codec;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;

public interface TestResource extends Resource {

Codec<TestResource> CODEC = Factory.CODEC.dispatch(Factory::from, Factory::codec);

abstract class Factory extends ResourceFactory<TestResource> {
static Registry<Factory> REGISTRY = Registry.service("test_resource", Factory.class);
static Registry.Index<Class<?>, Factory> TYPE_REGISTRY = REGISTRY.index(Factory::type);

static Codec<Factory> CODEC = Codec.STRING.xmap(ns -> REGISTRY.required(ns), Factory::name);

public Factory(NamespaceID namespace, Class<? extends TestResource> type, Codec<? extends TestResource> codec) {
super(namespace, type, codec);
}

public static @NotNull Factory from(@NotNull TestResource resource) {
return TYPE_REGISTRY.get(resource.getClass());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,48 @@ 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);

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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface ItemComponentHandler<C extends ItemComponent> extends Resource

// Cannot be a method ref because REGISTRY may be mid-initialization when this is initialized.
@SuppressWarnings("Convert2MethodRef")
Codec<ItemComponentHandler<?>> CODEC = Codec.STRING.xmap(namespace -> ItemComponentRegistry.REGISTRY.get(namespace), ItemComponentHandler::name);
Codec<ItemComponentHandler<?>> CODEC = Codec.STRING.xmap(namespace -> ItemComponentRegistry.REGISTRY.required(namespace), ItemComponentHandler::name);


// Descriptors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ItemManager implements Facet {

@Override
public void hook(@NotNull ServerWrapper server) {
EventNode<Event> eventNode = EventNode.all("unnamed:item/facet");
EventNode<Event> eventNode = EventNode.all("starlight:item/facet");
server.addEventNode(eventNode);

// Component handlers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RarityHandler implements ItemComponentHandler<Rarity> {

@Override
public @NotNull NamespaceID namespace() {
return NamespaceID.from("unnamed:rarity");
return NamespaceID.from("starlight:rarity");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class Factory extends ResourceFactory<LootEntry<?>> {
static Registry<Factory> REGISTRY = Registry.service("loot_entries", Factory.class);
static Registry.Index<Class<?>, Factory> TYPE_REGISTRY = REGISTRY.index(Factory::type);

static Codec<Factory> CODEC = Codec.STRING.xmap(ns -> REGISTRY.get(ns), Factory::name);
static Codec<Factory> CODEC = Codec.STRING.xmap(ns -> REGISTRY.required(ns), Factory::name);

public Factory(NamespaceID namespace, Class<? extends LootEntry<?>> type, Codec<? extends LootEntry<?>> codec) {
super(namespace, type, codec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class Factory extends ResourceFactory<LootModifier> {
static Registry<Factory> REGISTRY = Registry.service("loot_modifiers", LootModifier.Factory.class);
static Registry.Index<Class<?>, Factory> TYPE_REGISTRY = REGISTRY.index(Factory::type);

static Codec<Factory> CODEC = Codec.STRING.xmap(ns -> REGISTRY.get(ns), Factory::name);
static Codec<Factory> CODEC = Codec.STRING.xmap(ns -> REGISTRY.required(ns), Factory::name);

public Factory(NamespaceID namespace, Class<? extends LootModifier> type, Codec<? extends LootModifier> codec) {
super(namespace, type, codec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class Factory extends ResourceFactory<LootPredicate> {
static Registry<Factory> REGISTRY = Registry.service("loot_predicates", LootPredicate.Factory.class);
static Registry.Index<Class<?>, Factory> TYPE_REGISTRY = REGISTRY.index(Factory::type);

public static final Codec<Factory> CODEC = Codec.STRING.xmap(ns -> REGISTRY.get(ns), Factory::name);
public static final Codec<Factory> CODEC = Codec.STRING.xmap(ns -> REGISTRY.required(ns), Factory::name);

public Factory(NamespaceID namespace, Class<? extends LootPredicate> type, Codec<? extends LootPredicate> codec) {
super(namespace, type, codec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

public class WeaponHandler implements ItemComponentHandler<Weapon> {

private final EventNode<Event> eventNode = EventNode.all("unnamed:weapon/item_component_handler");
private final EventNode<Event> eventNode = EventNode.all("starlight:weapon/item_component_handler");

public WeaponHandler() {

}

@Override
public @NotNull NamespaceID namespace() {
return NamespaceID.from("unnamed:weapon");
return NamespaceID.from("starlight:weapon");
}

@Override
Expand Down
34 changes: 34 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 @@ -6,13 +6,15 @@
import net.hollowcube.player.event.PlayerLongDiggingStartEvent;
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Player;
import net.minestom.server.event.player.PlayerPacketEvent;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
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,30 @@ private void handleDiggingPacket(ClientPlayerDiggingPacket packet) {
}
}

private void handlePositionPacket(ClientPlayerPositionPacket packet) {
Pos playerPos = this.getPosition();
Block blockAtPlayerPos = this.getInstance().getBlock(playerPos);

if (packet.onGround()) {

}
else {
if (blockAtPlayerPos == Block.BUBBLE_COLUMN) {
if (!this.wasLastSeenInBubbleColumn) {
this.wasLastSeenInBubbleColumn = true;
this.bubbleColumnSinkEnergyLevel = this.getFood();
this.setFood(6);
}
}
}

if ((this.wasLastSeenInBubbleColumn) && (blockAtPlayerPos != 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 {
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);
}
}
}