Skip to content

Commit 04ab456

Browse files
committed
1C - Bugfixes and QoL
1 parent 7c133ca commit 04ab456

11 files changed

Lines changed: 3873 additions & 429 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@ Put your scripts to plugins/pyjavabridge/scripts and they will be ran on boot.
1010
## Docs
1111

1212
See [The documentation](docs/index.md)
13+
14+
## Examples
15+
16+
See [examples](examples/index.md)
17+
18+
## Changelog
19+
20+
See [changelog.md](changelog.md)

changelog.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
# Changelog
3+
4+
## 1C
5+
6+
API cleanup
7+
8+
Changes:
9+
10+
- Added call_sync and field_or_call_sync helpers
11+
- Turned most attribute-like methods into attributes
12+
- Made all attributes synchronous
13+
- Added create classmethods to most classes
14+
- Added optional args to world.spawn_entity
15+
- Allowed spawning of non-living entities
16+
- Fixed player UUID lookups
17+
- Chat event return value is now the chat message format for that event
18+
- Bugfixes
19+
20+
## 1B
21+
22+
API expansion
23+
24+
Changes:
25+
26+
- Implemented most missing APIs
27+
- Added proper command argument parsing
28+
- Added docs
29+
- Bugfixes
30+
31+
## 1A
32+
33+
Initial release
34+
35+
Changes:
36+
37+
- Added most common APIs

docs/index.md

Lines changed: 96 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ PyJavaBridge is a java plugin that manages and exposes APIs to python scripts vi
2929
- [Team](#team)
3030
- [Advancement](#advancement)
3131
- [AdvancementProgress](#advancementprogress)
32-
- [ItemMeta](#itemmeta)
3332
- [Difficulty](#difficulty)
3433
- [Objective](#objective)
34+
- [Chat](#chat)
35+
- [Raycast](#raycast)
36+
- [RaycastResult](#raycastresult)
3537
- [Enums](#enums)
3638
- [EntityType](#entitytype)
3739
- [EffectType](#effecttype)
@@ -53,11 +55,16 @@ Base event proxy. Event payloads may expose additional fields depending on event
5355
- `location`: [`Location`](#location) — The location involved in the event, if any.
5456
- `player`: [`Player`](#player) — The player involved in the event, if any.
5557
- `world`: [`World`](#world) — The world involved in the event, if any.
58+
- `slot`: int — Slot index for inventory click events.
5659

5760
### Methods
5861

5962
- `cancel()` — Cancel the event if it is cancellable. Returns an awaitable that resolves to `None`.
6063

64+
### Chat formatting
65+
66+
For `player_chat` handlers, returning a string will cancel the original chat event and broadcast the returned string instead. Returning `None` (or no return value) leaves chat unchanged.
67+
6168
### Event types
6269

6370
Events are resolved dynamically from handler names using snake_case to PascalCase plus `Event` (for example, `player_join``PlayerJoinEvent`).
@@ -83,19 +90,31 @@ Special cases:
8390

8491
Common examples: `server_boot`, `player_join`, `block_break`, `block_place`, `block_explode`, `entity_explode`, `player_move`, `player_quit`, `player_chat`, `player_interact`, `inventory_click`, `inventory_close`, `entity_damage`, `entity_death`, `world_load`, `world_unload`, `weather_change`.
8592

86-
## Server
93+
## Item
8794

88-
`Server`-level API.
95+
Item (ItemStack) API.
8996

9097
### Attributes
9198

92-
- `max_players`: int — Maximum player count.
93-
- `motd`: str — Server message of the day.
94-
- `name`: str — Server name.
95-
- `version`: str — Server version string.
99+
- `amount`: int — Item amount.
100+
- `type`: [`Material`](#material) — Item material.
101+
- `name`: str | None — Display name.
102+
- `lore`: list[str] — Lore lines.
103+
- `custom_model_data`: int | None — Custom model data.
104+
- `attributes`: list[dict] — Attribute modifiers.
105+
- `nbt`: dict — Serialized NBT map.
96106

97107
### Methods
98108

109+
- `clone()` — Clone this item. Returns an awaitable that resolves to [`Item`](#item).
110+
- `is_similar(other: [`Item`](#item))` — Check if items are similar. Returns an awaitable that resolves to bool.
111+
- `max_stack_size()` — Get max stack size. Returns an awaitable that resolves to int.
112+
- `set_amount(value: int)` — Set item amount. Returns an awaitable that resolves to `None`.
113+
- `set_name(name: str)` — Set display name. Returns an awaitable that resolves to `None`.
114+
- `set_lore(lore: list[str])` — Set lore. Returns an awaitable that resolves to `None`.
115+
- `set_custom_model_data(value: int)` — Set custom model data. Returns an awaitable that resolves to `None`.
116+
- `set_attributes(attributes: list[dict])` — Set attribute modifiers. Returns an awaitable that resolves to `None`.
117+
- `set_nbt(nbt: dict)` — Set NBT map. Returns an awaitable that resolves to `None`.
99118
- `broadcast(message: str)` — Broadcast a message to all players and console. Returns an awaitable that resolves to `None`.
100119
- `create_boss_bar(title: str, color: [`BarColor`](#barcolor), style: [`BarStyle`](#barstyle))` — Create a boss bar. Returns an awaitable that resolves to [`BossBar`](#bossbar).
101120
- `get_advancement(key: str)` — Get an advancement by namespaced key. Returns an awaitable that resolves to [`Advancement`](#advancement).
@@ -104,6 +123,10 @@ Common examples: `server_boot`, `player_join`, `block_break`, `block_place`, `bl
104123
- `players()` — Return the online players. Returns an awaitable that resolves to a list of [`Player`](#player).
105124
- `scoreboard_manager()` — Get the scoreboard manager. Returns an awaitable that resolves to a scoreboard manager object.
106125
- `scheduler()` — Get the server scheduler. Returns an awaitable that resolves to a server scheduler object.
126+
- `wait(ticks: int = 1, after: callable | None = None)` — Wait for ticks then optionally run a callback. Returns an awaitable that resolves to `None`.
127+
- `frame()` — Async context manager that batches calls into one send.
128+
- `atomic()` — Async context manager that batches calls as an atomic group.
129+
- `flush()` — Send all pending batched requests immediately. Returns an awaitable that resolves to `None`.
107130
- `world(name: str)` — Get a world by name. Returns an awaitable that resolves to [`World`](#world).
108131
- `worlds()` — Return all loaded worlds. Returns an awaitable that resolves to a list of [`World`](#world).
109132

@@ -124,6 +147,7 @@ Base entity proxy.
124147
- `custom_name()` — Get custom name. Returns an awaitable that resolves to any name value.
125148
- `fire_ticks()` — Get fire ticks. Returns an awaitable that resolves to int.
126149
- `is_dead()` — Check if entity is dead. Returns an awaitable that resolves to bool.
150+
- `is_alive()` — Check if entity is alive. Returns an awaitable that resolves to bool.
127151
- `is_valid()` — Check if entity is valid. Returns an awaitable that resolves to bool.
128152
- `passengers()` — Get passengers. Returns an awaitable that resolves to a list of [`Entity`](#entity).
129153
- `remove()` — Remove the entity. Returns an awaitable that resolves to `None`.
@@ -139,6 +163,10 @@ Base entity proxy.
139163

140164
`Player` API (inherits [`Entity`](#entity)).
141165

166+
### Constructor
167+
168+
- `Player(uuid: str | None = None, name: str | None = None)` — Resolve a player by UUID or name.
169+
142170
### Attributes
143171

144172
- `food_level`: int — Hunger level.
@@ -158,6 +186,14 @@ Base entity proxy.
158186
- `exp()` — Get experience progress $0..1$. Returns an awaitable that resolves to float.
159187
- `give_exp(amount: int)` — Give raw experience points. Returns an awaitable that resolves to `None`.
160188
- `has_permission(permission: str)` — Check a permission. Returns an awaitable that resolves to bool.
189+
- `is_alive()` — Check if the player is alive. Returns an awaitable that resolves to bool.
190+
- `add_permission(permission: str, value: bool = True)` — Add or set a permission (LuckPerms-aware). Returns an awaitable that resolves to bool.
191+
- `remove_permission(permission: str)` — Remove a permission (LuckPerms-aware). Returns an awaitable that resolves to bool.
192+
- `permission_groups()` — Get permission groups (LuckPerms-aware). Returns an awaitable that resolves to list of str.
193+
- `primary_group()` — Get primary permission group (LuckPerms-aware). Returns an awaitable that resolves to str or `None`.
194+
- `has_group(group: str)` — Check group membership (LuckPerms-only). Returns an awaitable that resolves to bool.
195+
- `add_group(group: str)` — Add a permission group (LuckPerms-only). Returns an awaitable that resolves to bool.
196+
- `remove_group(group: str)` — Remove a permission group (LuckPerms-only). Returns an awaitable that resolves to bool.
161197
- `is_flying()` — Check if the player is flying. Returns an awaitable that resolves to bool.
162198
- `is_op()` — Check if the player is op. Returns an awaitable that resolves to bool.
163199
- `is_sneaking()` — Check if sneaking. Returns an awaitable that resolves to bool.
@@ -286,16 +322,24 @@ Item (ItemStack) API.
286322
### Attributes
287323

288324
- `amount`: int — Stack amount.
289-
- `meta`: [`ItemMeta`](#itemmeta) — Item metadata.
290325
- `type`: [`Material`](#material) — Item material.
326+
- `name`: str | None — Display name.
327+
- `lore`: list[str] — Lore lines.
328+
- `custom_model_data`: int | None — Custom model data.
329+
- `attributes`: list[dict] — Attribute modifiers.
330+
- `nbt`: dict — Serialized NBT map.
291331

292332
### Methods
293333

294334
- `clone()` — Clone this item. Returns an awaitable that resolves to [`Item`](#item).
295335
- `is_similar(other: [`Item`](#item))` — Check if items are similar. Returns an awaitable that resolves to bool.
296336
- `max_stack_size()` — Get max stack size. Returns an awaitable that resolves to int.
297337
- `set_amount(value: int)` — Set item amount. Returns an awaitable that resolves to `None`.
298-
- `set_meta(meta: [`ItemMeta`](#itemmeta))` — Set item meta. Returns an awaitable that resolves to `None`.
338+
- `set_name(name: str)` — Set display name. Returns an awaitable that resolves to `None`.
339+
- `set_lore(lore: list[str])` — Set lore. Returns an awaitable that resolves to `None`.
340+
- `set_custom_model_data(value: int)` — Set custom model data. Returns an awaitable that resolves to `None`.
341+
- `set_attributes(attributes: list[dict])` — Set attribute modifiers. Returns an awaitable that resolves to `None`.
342+
- `set_nbt(nbt: dict)` — Set NBT map. Returns an awaitable that resolves to `None`.
299343

300344
## Chunk
301345

@@ -368,6 +412,10 @@ None.
368412

369413
Active potion effect.
370414

415+
### Constructor
416+
417+
- `Effect(effect_type: EffectType | str, duration: int = 0, amplifier: int = 0, ambient: bool = False, particles: bool = True, icon: bool = True)` — Create a value effect.
418+
371419
### Attributes
372420

373421
- `ambient`: bool — Whether the effect is ambient.
@@ -386,26 +434,37 @@ Active potion effect.
386434

387435
Inventory. Can belong to an entity or block entity, or exist as a standalone open inventory screen.
388436

437+
### Constructor
438+
439+
- `Inventory(size: int = 9, title: str = "", contents: list[Item] | None = None)` — Create a value inventory.
440+
389441
### Attributes
390442

391443
- `contents`: list of [`Item`](#item) — Inventory contents.
392444
- `holder`: any — Inventory holder.
393445
- `size`: int — Inventory size.
446+
- `title`: str — Inventory title.
394447

395448
### Methods
396449

397450
- `add_item(item: [`Item`](#item))` — Add an item to the inventory. Returns an awaitable that resolves to any add result.
398451
- `clear()` — Clear inventory contents. Returns an awaitable that resolves to `None`.
452+
- `close(player: [`Player`](#player) | None = None)` — Close this inventory for a player. Returns an awaitable that resolves to `None`.
399453
- `contains(material: [`Material`](#material), amount: int = 1)` — Check if inventory contains a material. Returns an awaitable that resolves to bool.
400454
- `first_empty()` — Get first empty slot index. Returns an awaitable that resolves to int.
401455
- `get_item(slot: int)` — Get item in a slot. Returns an awaitable that resolves to [`Item`](#item).
456+
- `open(player: [`Player`](#player))` — Open this inventory for a player. Returns an awaitable that resolves to any open result.
402457
- `remove_item(item: [`Item`](#item))` — Remove an item from the inventory. Returns an awaitable that resolves to any remove result.
403458
- `set_item(slot: int, item: [`Item`](#item))` — Set item in a slot. Returns an awaitable that resolves to `None`.
404459

405460
## Material
406461

407462
Material enum proxy, such as diamond, netherite, wood.
408463

464+
### Constructor
465+
466+
- `Material(name: str)` — Create a material enum value (case-insensitive).
467+
409468
### Attributes
410469

411470
None.
@@ -540,21 +599,6 @@ None.
540599
- `remaining_criteria()` — Get remaining criteria. Returns an awaitable that resolves to a set of str.
541600
- `revoke_criteria(name: str)` — Revoke a criterion. Returns an awaitable that resolves to bool.
542601

543-
## ItemMeta
544-
545-
Item metadata proxy.
546-
547-
### Attributes
548-
549-
- `custom_model_data`: int — Custom model data value.
550-
- `has_custom_model_data`: bool — Whether custom model data exists.
551-
552-
### Methods
553-
554-
- `has_lore()` — Check if lore exists. Returns an awaitable that resolves to bool.
555-
- `lore()` — Get lore lines. Returns an awaitable that resolves to a list of str.
556-
- `set_custom_model_data(value: int)` — Set custom model data. Returns an awaitable that resolves to `None`.
557-
- `set_lore(lore: list[str])` — Set lore lines. Returns an awaitable that resolves to `None`.
558602

559603
## Difficulty
560604

@@ -585,6 +629,34 @@ None.
585629
- `set_display_name(name: str)` — Set display name. Returns an awaitable that resolves to `None`.
586630
- `set_display_slot(slot: any)` — Set display slot. Returns an awaitable that resolves to `None`.
587631

632+
## Chat
633+
634+
Chat helper API.
635+
636+
### Methods
637+
638+
- `broadcast(message: str)` — Broadcast a chat message to all players and console. Returns an awaitable that resolves to `None`.
639+
640+
## Raycast
641+
642+
Raycast helpers.
643+
644+
### Methods
645+
646+
- `raycast(world, start, direction, max_distance=64.0, ray_size=0.2, include_entities=True, include_blocks=True, ignore_passable=True)` — Raycast in a world. `world` can be a world object or name. `direction` is a `(yaw, pitch)` tuple. Returns an awaitable that resolves to a [`RaycastResult`](#raycastresult) or `None` when no hit.
647+
648+
## RaycastResult
649+
650+
Raycast result data.
651+
652+
### Attributes
653+
654+
- `x`, `y`, `z`: float — Hit position.
655+
- `entity`: [`Entity`](#entity) | None — Hit entity.
656+
- `block`: [`Block`](#block) | None — Hit block.
657+
- `start_x`, `start_y`, `start_z`: float — Start position.
658+
- `yaw`, `pitch`: float — Direction angles used for the ray.
659+
588660
## Enums
589661

590662
## EntityType

examples/command.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,53 @@
11
from bridge import *
22

3+
# Test auto-generated usage
34
@command("Hello world command")
45
async def helloworld(event: Event):
56
event.player.send_message("Hello, World!")
67

8+
# Test command args
79
@command("Greet command")
810
async def greet(event: Event, name: str = None):
911
event.player.send_message(f"Hello, {name or event.player.name}!")
1012

13+
# Test raycast / teleport
14+
@command("Teleport to spawn")
15+
async def spawn(event: Event):
16+
ray:RaycastResult = await raycast('world', (0.5, 320, 0.5), (0, 90), 384, 0.5, False)
17+
event.player.teleport((0.5, ray.y+0.5, 0.5))
18+
19+
# Test Inventory API
20+
@command("open test gui")
21+
async def gui(event: Event):
22+
inv = Inventory(3*9, title="Test GUI", contents=[Item('light_gray_stained_glass_pane').set_name(" ") for _ in range(3*9)])
23+
24+
inv.set_item(9+4, Item('totem_of_undying').set_name("Bing chilling"))
25+
26+
inv.open(event.player)
27+
28+
# Test Inventory Click event
29+
@event
30+
async def inventory_click(event: Event):
31+
if event.inventory.title != 'Test GUI':
32+
return
33+
34+
event.cancel()
35+
36+
if event.slot == 9+4:
37+
event.player.play_sound('item_totem_use')
38+
event.player.send_message('Bing chilling')
39+
event.inventory.close()
40+
41+
@event
42+
async def inventory_drag(event: Event):
43+
if event.inventory.title == 'Test GUI':
44+
event.cancel()
45+
46+
@command("Open someones inv")
47+
async def invsee(event: Event, p: str):
48+
if p == event.player.name:
49+
event.player.send_message("You cannot open your own inventory!")
50+
event.player.play_sound('block_note_block_bass')
51+
return
52+
53+
Player(p).inventory.open(event.player)

examples/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
# Examples
3+
4+
This folder contains some examples. They *should* all work on the latest dev version.

0 commit comments

Comments
 (0)