Move curios into the main bworld mod
@@ -719,3 +719,26 @@ Textures by Hugh "XSSheep" Rutland (https://www.minecraftforum.net/members/XSShe
|
||||
https://github.com/NovaWostra/Pixel-Perfection-Chorus-Eddit
|
||||
|
||||
license: [Creative Commons Attribution-Share Alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/)
|
||||
|
||||
# Curios
|
||||
|
||||
The curios slots (`scripts/curios.ts`) are a port of [Curios](https://github.com/TheIllusiveC4/Curios) by C4. Their slot
|
||||
textures (`textures/empty_*_slot.png`) and game design come from the original, version 17.0.0-beta.2 for NeoForge 26.3,
|
||||
under the GNU Lesser General Public License, version 3 or later:
|
||||
|
||||
```
|
||||
Copyright (C) 2018-2024 C4
|
||||
|
||||
Curios is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Curios is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with Curios. If not, see <https://www.gnu.org/licenses/>.
|
||||
```
|
||||
|
||||
@@ -5,3 +5,47 @@ item behavior from `scripts/server.ts`, the same way it loads any other mod.
|
||||
|
||||
Some of the base game is still built into the engine: recipe types, the inventory screen and crafting grid, the HUD,
|
||||
`/give` and `/time`, mining rules, and terrain generation. See "The Engine and the Base Game" in `docs/modding_plan.md`.
|
||||
|
||||
## Curios
|
||||
|
||||
`scripts/curios.ts` is a port of [Curios](https://github.com/TheIllusiveC4/Curios): accessory slots, like rings,
|
||||
necklaces and belts, for any mod's items. They're in 2 columns left of the player's inventory, there whenever it's open.
|
||||
Shift clicking a curio in the inventory puts it in the first slot it fits.
|
||||
|
||||
Like the original, it adds no items, and a slot type only shows up once some mod puts an item in its tag.
|
||||
|
||||
### Making an Item a Curio
|
||||
|
||||
Add the item to the slot type's item tag. For a ring, `tags/items/bworld/curios/ring.json` in your mod:
|
||||
|
||||
```json
|
||||
{ "format_version": 1, "tag": { "values": ["jewelry:gold_ring"] } }
|
||||
```
|
||||
|
||||
| Tag | Slot |
|
||||
| ------------------------- | ---------------------- |
|
||||
| `#bworld:curios/curio` | Any slot, and its own. |
|
||||
| `#bworld:curios/head` | Head |
|
||||
| `#bworld:curios/necklace` | Necklace |
|
||||
| `#bworld:curios/back` | Back |
|
||||
| `#bworld:curios/body` | Body |
|
||||
| `#bworld:curios/bracelet` | Bracelet |
|
||||
| `#bworld:curios/hands` | Hands |
|
||||
| `#bworld:curios/ring` | Ring |
|
||||
| `#bworld:curios/belt` | Belt |
|
||||
| `#bworld:curios/feet` | Feet |
|
||||
| `#bworld:curios/charm` | Charm |
|
||||
|
||||
Every player has one slot of each type. Curios drop when the player dies, like the rest of what they carry.
|
||||
|
||||
### Attribute Modifiers
|
||||
|
||||
An item with the `bworld:curio_modifiers` component changes the wearer's
|
||||
[attributes](../../docs/modding/entities/players.md#attributes) while it's in a curio slot, and says so in its tooltip.
|
||||
The Power Glove from Artifacts:
|
||||
|
||||
```json
|
||||
"components": {
|
||||
"bworld:curio_modifiers": [{ "attribute": "bworld:attack_damage", "amount": 4, "operation": "add_value" }]
|
||||
}
|
||||
```
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
// curios, from C4's Curios mod: accessory slots, in 2 columns left of the player's inventory. each player's curios are
|
||||
// a container of their own, and fall out when they die like the rest of what they carry. items with the
|
||||
// bworld:curio_modifiers component change the wearer's attributes, like curios' attribute modifiers
|
||||
import type { AttributeModifier, Container, Player, ServerContext, TagApi } from "bworld/server";
|
||||
|
||||
// where the left column of slots goes, from the inventory screen's left edge. the panel around them ends just left of it
|
||||
const COLUMNS_X = -45;
|
||||
|
||||
// bworld:curio_modifiers' params, a list of these
|
||||
interface WornModifier {
|
||||
attribute: string;
|
||||
amount: number;
|
||||
operation: AttributeModifier["operation"];
|
||||
}
|
||||
|
||||
export function register_curios(ctx: ServerContext) {
|
||||
const active = active_slots(ctx.tags);
|
||||
|
||||
ctx.components.register_item<WornModifier[]>("bworld:curio_modifiers", {
|
||||
get_lore: (_item, modifiers) => ["When worn:", ...modifiers.map(describe)].join("\n"),
|
||||
});
|
||||
const modifiers_of = (id: string) => (ctx.items.components(id)["bworld:curio_modifiers"] ?? []) as WornModifier[];
|
||||
|
||||
// the modifiers each online player's curios gave them, by player id, to take them off again
|
||||
const applied = new Map<string, { attribute: string; id: string }[]>();
|
||||
const stop_watching = new Map<string, () => void>();
|
||||
|
||||
// by player name, like the players themselves are saved. undefined until they first join with curios
|
||||
function saved_curios(player: Player): Container | undefined {
|
||||
const id = ctx.storage.get<Record<string, string>>("curios")?.[player.name];
|
||||
return id ? ctx.containers.get(id) : undefined;
|
||||
}
|
||||
|
||||
function curios_of(player: Player): Container {
|
||||
const existing = saved_curios(player);
|
||||
if (existing) return existing;
|
||||
const container = ctx.containers.create(SLOTS.length);
|
||||
const ids = ctx.storage.get<Record<string, string>>("curios") ?? {};
|
||||
ctx.storage.set("curios", { ...ids, [player.name]: container.id });
|
||||
watch(player, container);
|
||||
return container;
|
||||
}
|
||||
|
||||
// keeps what their curios give them up to date
|
||||
function watch(player: Player, curios: Container) {
|
||||
stop_watching.get(player.id)?.();
|
||||
stop_watching.set(player.id, curios.on_change(() => update_modifiers(player, curios)));
|
||||
update_modifiers(player, curios);
|
||||
}
|
||||
|
||||
function update_modifiers(player: Player, curios: Container) {
|
||||
for (const { attribute, id } of applied.get(player.id) ?? []) player.remove_modifier(attribute, id);
|
||||
const now: { attribute: string; id: string }[] = [];
|
||||
for (const [index, slot] of SLOTS.entries()) {
|
||||
const item = curios.get(index);
|
||||
if (!item || !active.includes(slot)) continue;
|
||||
for (const [i, { attribute, amount, operation }] of modifiers_of(item.id).entries()) {
|
||||
const id = `bworld:curio/${slot}/${i}`;
|
||||
player.add_modifier(attribute, { id, amount, operation });
|
||||
now.push({ attribute, id });
|
||||
}
|
||||
}
|
||||
applied.set(player.id, now);
|
||||
}
|
||||
|
||||
ctx.events.after.player_join.subscribe(({ player }) => {
|
||||
const curios = saved_curios(player);
|
||||
if (curios) watch(player, curios);
|
||||
});
|
||||
ctx.events.after.player_leave.subscribe(({ player }) => {
|
||||
stop_watching.get(player.id)?.();
|
||||
stop_watching.delete(player.id);
|
||||
applied.delete(player.id);
|
||||
});
|
||||
|
||||
// the slot types in 2 columns left of the inventory, always there while it's open. the engine draws their panel
|
||||
ctx.menus.extend_player_menu((menu, player) => {
|
||||
if (active.length === 0) return;
|
||||
const curios = curios_of(player);
|
||||
// what's in slot types no item goes in anymore, since the mod that put items in them was removed, goes back
|
||||
// to them
|
||||
for (const [index, slot] of SLOTS.entries()) {
|
||||
const item = curios.get(index);
|
||||
if (item && !active.includes(slot)) {
|
||||
player.give_item(item.id, item.count, item.data);
|
||||
curios.set(index, undefined);
|
||||
}
|
||||
}
|
||||
for (const [i, slot] of active.entries()) {
|
||||
menu.add_slot(curios, SLOTS.indexOf(slot), {
|
||||
x: COLUMNS_X + (i % 2) * 18,
|
||||
y: 7 + Math.floor(i / 2) * 18,
|
||||
filter: (item) => fits(ctx.tags, slot, item.id),
|
||||
icon: `bworld:empty_${slot}_slot`,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
ctx.events.after.player_death.subscribe(({ player }) => {
|
||||
const curios = saved_curios(player);
|
||||
if (!curios) return;
|
||||
const { x, y, z } = player.position;
|
||||
for (let i = 0; i < curios.size; i++) {
|
||||
const item = curios.get(i);
|
||||
if (!item) continue;
|
||||
const { id, count, data } = item;
|
||||
curios.set(i, undefined);
|
||||
ctx.world.drop_item(Math.floor(x), Math.floor(Math.max(y, 0)), Math.floor(z), { id, count, data });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// like minecraft's attribute tooltips: "+4 Attack Damage", "+30% Attack Speed"
|
||||
function describe({ attribute, amount, operation }: WornModifier): string {
|
||||
const name = attribute.split(":").at(-1)!.split("_").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
|
||||
const sign = amount < 0 ? "-" : "+";
|
||||
const value = operation === "add_value" ? `${Math.abs(amount)}` : `${Math.round(Math.abs(amount) * 100)}%`;
|
||||
return `${sign}${value} ${name}`;
|
||||
}
|
||||
|
||||
// the slot types, in curios' order. every player has one of each, at its index here in their curios container, so
|
||||
// slots keep their items when mods come and go
|
||||
const SLOTS = [
|
||||
"curio",
|
||||
"head",
|
||||
"necklace",
|
||||
"back",
|
||||
"body",
|
||||
"bracelet",
|
||||
"hands",
|
||||
"ring",
|
||||
"belt",
|
||||
"feet",
|
||||
"charm",
|
||||
];
|
||||
|
||||
// like curios, a slot type is only there once some item goes in it: something is in its item tag, #bworld:curios/ring
|
||||
// for rings
|
||||
function active_slots(tags: TagApi): string[] {
|
||||
return SLOTS.filter((slot) => tags.get("items", `bworld:curios/${slot}`).length > 0);
|
||||
}
|
||||
|
||||
// curios' tag validator: an item goes in the slot types whose tag it's in, and #bworld:curios/curio items go in any
|
||||
function fits(tags: TagApi, slot: string, id: string): boolean {
|
||||
return tags.has("items", `bworld:curios/${slot}`, id) || tags.has("items", "bworld:curios/curio", id);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
// the base game's block and item behavior, written against the same api as any other mod
|
||||
import type { BlockRef, Container, EntityRef, Player, ServerContext } from "bworld/server";
|
||||
import { register_curios } from "./curios.ts";
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
register_hoeable(ctx);
|
||||
@@ -8,6 +9,7 @@ export function setup(ctx: ServerContext) {
|
||||
register_smithing_table(ctx);
|
||||
register_farming(ctx);
|
||||
register_fishing(ctx);
|
||||
register_curios(ctx);
|
||||
}
|
||||
|
||||
// right clicking with a type of tool turns the block into another, like any hoe on grass
|
||||
|
||||
|
After Width: | Height: | Size: 145 B |
|
After Width: | Height: | Size: 144 B |
|
After Width: | Height: | Size: 152 B |
|
After Width: | Height: | Size: 157 B |
|
After Width: | Height: | Size: 139 B |
|
After Width: | Height: | Size: 140 B |
|
After Width: | Height: | Size: 149 B |
|
After Width: | Height: | Size: 158 B |
|
After Width: | Height: | Size: 157 B |
|
After Width: | Height: | Size: 152 B |
|
After Width: | Height: | Size: 144 B |