Skip to content

Commit

Permalink
add PlayerJumpCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
LCLPYT committed Jan 25, 2025
1 parent ea160ea commit ec64143
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ translations4j_version=2.11.0

# subproject versions
kibu-fabric-hooks-version=0.5.1
kibu-hooks-version=0.55.0
kibu-hooks-version=0.56.0
kibu-command-api-version=0.9.0
kibu-scheduler-api-version=0.9.0
kibu-hook-api-version=1.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import org.jetbrains.annotations.Nullable;
import work.lclpnet.kibu.hook.player.PlayerDeathCallback;
import work.lclpnet.kibu.hook.player.PlayerInventoryHooks;
import work.lclpnet.kibu.hook.player.PlayerJumpCallback;
import work.lclpnet.kibu.hook.player.PlayerMoveCallback;
import work.lclpnet.kibu.hook.util.OnGroundDetector;
import work.lclpnet.kibu.hook.util.PlayerUtils;
import work.lclpnet.kibu.hook.world.BlockModificationHooks;

Expand Down Expand Up @@ -69,6 +72,12 @@ public void onInitialize() {

PlayerInventoryHooks.DROPPED_ITEM.invoker().onDroppedItem(event.player(), event.slot());
});

PlayerMoveCallback.HOOK.register((player, from, to) -> player.getPlayerInput().jump()
&& to.getY() > from.getY()
&& player.isOnGround()
&& OnGroundDetector.isOnGroundServer(player)
&& PlayerJumpCallback.HOOK.invoker().onJump(player));
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package work.lclpnet.kibu.hook.player;

import net.minecraft.server.network.ServerPlayerEntity;
import work.lclpnet.kibu.hook.Hook;
import work.lclpnet.kibu.hook.HookFactory;

public interface PlayerJumpCallback {

Hook<PlayerJumpCallback> HOOK = HookFactory.createArrayBacked(PlayerJumpCallback.class, callbacks -> (player) -> {
boolean cancel = false;

for (var cb : callbacks) {
if (cb.onJump(player)) {
cancel = true;
}
}

return cancel;
});

boolean onJump(ServerPlayerEntity player);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package work.lclpnet.kibu.hook.util;

import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.Box;

public class OnGroundDetector {

public static boolean isOnGroundServer(ServerPlayerEntity player) {
return isOnGroundServer(player, 0.02);
}

public static boolean isOnGroundServer(ServerPlayerEntity player, double tol) {
double y = player.getY();
Box box = player.getBoundingBox().withMinY(y - tol).withMaxY(y + 1e-5);

return player.getWorld().getBlockCollisions(player, box).iterator().hasNext();
}
}
13 changes: 12 additions & 1 deletion src/testmod/java/work/lclpnet/test/KibuTestMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.DyeColor;
import net.minecraft.util.Hand;
Expand Down Expand Up @@ -331,8 +333,17 @@ private void misc() {

PlayerInputCallback.HOOK.register((player, input) -> {
if (player.getMainHandStack().isOf(Items.FEATHER)) {
System.out.println("JUMP");
System.out.println("input " + input);
}
});

PlayerJumpCallback.HOOK.register(player -> {
if (player.getMainHandStack().isOf(Items.FEATHER)) {
player.playSoundToPlayer(SoundEvents.BLOCK_NOTE_BLOCK_PLING.value(), SoundCategory.MASTER, 0.2f, 1f);
return player.getOffHandStack().isOf(STICK);
}

return false;
});
}
}

0 comments on commit ec64143

Please sign in to comment.