Fishing !

This commit is contained in:
2026-09-28 13:33:40 -03:00
parent 2488c6f8dc
commit 84a622c3f1
27 changed files with 367 additions and 21 deletions
+2 -1
View File
@@ -10,7 +10,8 @@
"item": false,
"replaceable": true,
"components": {
"bworld:water_source": {}
"bworld:water_source": {},
"bworld:fishable": {}
}
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"format_version": 1,
"item": {
"id": "bworld:cod",
"texture": "bworld:cod",
"food": {
"nutrition": 2,
"saturation_modifier": 0.1
}
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"format_version": 1,
"item": {
"id": "bworld:cooked_cod",
"texture": "bworld:cooked_cod",
"food": {
"nutrition": 5,
"saturation_modifier": 0.6
}
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"format_version": 1,
"item": {
"id": "bworld:cooked_salmon",
"texture": "bworld:cooked_salmon",
"food": {
"nutrition": 6,
"saturation_modifier": 0.8
}
}
}
+22
View File
@@ -0,0 +1,22 @@
{
"format_version": 1,
"item": {
"id": "bworld:fishing_rod",
"texture": "bworld:fishing_rod",
"data_textures": {
"cast": "bworld:fishing_rod_cast"
},
"max_stack": 1,
"lore": "Right click water to cast, and again when something bites.",
"components": {
"bworld:fishing_rod": {
"wait": [100, 600],
"bite": [20, 40],
"loot": [
{ "item": "bworld:cod", "weight": 60 },
{ "item": "bworld:salmon", "weight": 25 }
]
}
}
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"format_version": 1,
"item": {
"id": "bworld:salmon",
"texture": "bworld:salmon",
"food": {
"nutrition": 2,
"saturation_modifier": 0.1
}
}
}
@@ -0,0 +1,19 @@
{
"format_version": 1,
"recipe": {
"type": "shaped",
"pattern": [
" S",
" SW",
"S W"
],
"key": {
"S": "bworld:stick",
"W": "bworld:wheat"
},
"result": {
"id": "bworld:fishing_rod",
"count": 1
}
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"recipe": {
"type": "furnace",
"input": "bworld:cod",
"output": {
"id": "bworld:cooked_cod",
"count": 1
},
"cook_time": 200
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"recipe": {
"type": "furnace",
"input": "bworld:salmon",
"output": {
"id": "bworld:cooked_salmon",
"count": 1
},
"cook_time": 200
}
}
+123
View File
@@ -7,6 +7,7 @@ export function setup(ctx: ServerContext) {
register_furnace(ctx);
register_smithing_table(ctx);
register_farming(ctx);
register_fishing(ctx);
}
// right clicking with a type of tool turns the block into another, like any hoe on grass
@@ -506,3 +507,125 @@ function register_farming(ctx: ServerContext) {
},
});
}
// fishing, like minecraft's without the bobber: right click water with a rod to cast, wait for a bite, and right click
// again before it gets away to reel it in. there's no hook entity yet, so a chat message says when something bites
const FISHING_ROD = "bworld:fishing_rod";
// further than this from where they cast, the line snaps
const LINE_LENGTH = 16;
interface FishingRodParams {
// ticks until something bites, the shortest and longest, like minecraft's 5 to 30 seconds
wait: [number, number];
// ticks it stays on the hook before it gets away
bite: [number, number];
loot: { item: string; weight: number }[];
}
interface Cast {
x: number;
y: number;
z: number;
// the block they cast into, the line comes out when it's gone
block: string;
// the hotbar slot the rod is in, switching away reels it in
slot: number;
params: FishingRodParams;
// ticks, when something bites and when it gets away
bite_at: number;
escape_at?: number;
}
function register_fishing(ctx: ServerContext) {
// by player id
const casts = new Map<string, Cast>();
// the water each player right clicked this tick, for their rod's on_use, which doesn't get the block
const aimed = new Map<string, { x: number; y: number; z: number; block: string; tick: number }>();
const between = ([min, max]: [number, number]) => min + Math.floor(Math.random() * (max - min + 1));
const pick = (loot: FishingRodParams["loot"]) => {
const total = loot.reduce((sum, entry) => sum + entry.weight, 0);
let roll = Math.random() * total;
for (const entry of loot) {
roll -= entry.weight;
if (roll < 0) return entry.item;
}
return loot.at(-1)?.item;
};
// takes the line out of the water, the rod in the slot looks uncast again
const stop = (player: Player | undefined, id: string) => {
const cast = casts.get(id);
casts.delete(id);
const rod = cast && player?.inventory.get(cast.slot);
if (rod?.id === FISHING_ROD) rod.data = undefined;
};
ctx.components.register_block("bworld:fishable", {
on_interact(block, _params, player) {
aimed.set(player.id, {
x: block.x,
y: block.y,
z: block.z,
block: block.id,
tick: ctx.system.current_tick,
});
return false;
},
});
ctx.components.register_item<FishingRodParams>(FISHING_ROD, {
on_use(rod, params, player) {
const cast = casts.get(player.id);
if (cast) {
if (cast.escape_at !== undefined) {
const item = pick(params.loot);
if (item) player.give_item(item);
}
stop(player, player.id);
return;
}
const water = aimed.get(player.id);
aimed.delete(player.id);
if (water?.tick !== ctx.system.current_tick) {
// cast before the server restarted
rod.data = undefined;
return;
}
casts.set(player.id, {
x: water.x,
y: water.y,
z: water.z,
block: water.block,
slot: player.selected_slot,
params,
bite_at: ctx.system.current_tick + between(params.wait),
});
rod.data = { cast: true };
},
});
ctx.events.after.tick.subscribe(() => {
const now = ctx.system.current_tick;
for (const [id, cast] of casts) {
const player = ctx.players.get(id);
const { x, y, z } = player?.position ?? cast;
const distance = Math.hypot(x - cast.x, y - cast.y, z - cast.z);
if (
!player || player.selected_slot !== cast.slot || player.held_item?.id !== FISHING_ROD ||
distance > LINE_LENGTH || ctx.world.get_block(cast.x, cast.y, cast.z) !== cast.block
) {
stop(player, id);
} else if (cast.escape_at === undefined && now >= cast.bite_at) {
cast.escape_at = now + between(cast.params.bite);
player.send_message("Something's biting! Right click to reel it in");
} else if (cast.escape_at !== undefined && now >= cast.escape_at) {
// it got away, wait for another
cast.escape_at = undefined;
cast.bite_at = now + between(cast.params.wait);
}
}
});
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 541 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 393 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 421 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 175 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 575 B