Skip to content

Commit 39e9bf6

Browse files
committed
Support player/world mutation responses
1 parent 9fc9b8a commit 39e9bf6

File tree

8 files changed

+1143
-173
lines changed

8 files changed

+1143
-173
lines changed

plugin/adapters/handlers/player.go

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -26,158 +26,92 @@ func NewPlayerHandler(manager ports.EventManager) player.Handler {
2626
}
2727

2828
func (h *PlayerHandler) HandleChat(ctx *player.Context, message *string) {
29-
if h.manager == nil {
30-
return
31-
}
3229
h.manager.EmitChat(ctx, ctx.Val(), message)
3330
}
3431

3532
func (h *PlayerHandler) HandleMove(ctx *player.Context, newPos mgl64.Vec3, newRot cube.Rotation) {
36-
if h.manager == nil {
37-
return
38-
}
3933
h.manager.EmitPlayerMove(ctx, ctx.Val(), newPos, newRot)
4034
}
4135

4236
func (h *PlayerHandler) HandleJump(p *player.Player) {
43-
if h.manager == nil {
44-
return
45-
}
4637
h.manager.EmitPlayerJump(p)
4738
}
4839

4940
func (h *PlayerHandler) HandleTeleport(ctx *player.Context, pos mgl64.Vec3) {
50-
if h.manager == nil {
51-
return
52-
}
5341
h.manager.EmitPlayerTeleport(ctx, ctx.Val(), pos)
5442
}
5543

5644
func (h *PlayerHandler) HandleChangeWorld(p *player.Player, before, after *world.World) {
57-
if h.manager == nil {
58-
return
59-
}
6045
h.manager.EmitPlayerChangeWorld(p, before, after)
6146
}
6247

6348
func (h *PlayerHandler) HandleToggleSprint(ctx *player.Context, after bool) {
64-
if h.manager == nil {
65-
return
66-
}
6749
h.manager.EmitPlayerToggleSprint(ctx, ctx.Val(), after)
6850
}
6951

7052
func (h *PlayerHandler) HandleToggleSneak(ctx *player.Context, after bool) {
71-
if h.manager == nil {
72-
return
73-
}
7453
h.manager.EmitPlayerToggleSneak(ctx, ctx.Val(), after)
7554
}
7655

7756
func (h *PlayerHandler) HandleFoodLoss(ctx *player.Context, from int, to *int) {
78-
if h.manager == nil {
79-
return
80-
}
8157
h.manager.EmitPlayerFoodLoss(ctx, ctx.Val(), from, to)
8258
}
8359

8460
func (h *PlayerHandler) HandleHeal(ctx *player.Context, health *float64, src world.HealingSource) {
85-
if h.manager == nil {
86-
return
87-
}
8861
h.manager.EmitPlayerHeal(ctx, ctx.Val(), health, src)
8962
}
9063

9164
func (h *PlayerHandler) HandleHurt(ctx *player.Context, damage *float64, immune bool, attackImmunity *time.Duration, src world.DamageSource) {
92-
if h.manager == nil {
93-
return
94-
}
9565
h.manager.EmitPlayerHurt(ctx, ctx.Val(), damage, immune, attackImmunity, src)
9666
}
9767

9868
func (h *PlayerHandler) HandleDeath(p *player.Player, src world.DamageSource, keepInv *bool) {
99-
if h.manager == nil {
100-
return
101-
}
10269
h.manager.EmitPlayerDeath(p, src, keepInv)
10370
}
10471

10572
func (h *PlayerHandler) HandleRespawn(p *player.Player, pos *mgl64.Vec3, w **world.World) {
106-
if h.manager == nil {
107-
return
108-
}
10973
h.manager.EmitPlayerRespawn(p, pos, w)
11074
}
11175

11276
func (h *PlayerHandler) HandleSkinChange(ctx *player.Context, skin *skin.Skin) {
113-
if h.manager == nil {
114-
return
115-
}
11677
h.manager.EmitPlayerSkinChange(ctx, ctx.Val(), skin)
11778
}
11879

11980
func (h *PlayerHandler) HandleFireExtinguish(ctx *player.Context, pos cube.Pos) {
120-
if h.manager == nil {
121-
return
122-
}
12381
h.manager.EmitPlayerFireExtinguish(ctx, ctx.Val(), pos)
12482
}
12583

12684
func (h *PlayerHandler) HandleStartBreak(ctx *player.Context, pos cube.Pos) {
127-
if h.manager == nil {
128-
return
129-
}
13085
h.manager.EmitPlayerStartBreak(ctx, ctx.Val(), pos)
13186
}
13287

13388
func (h *PlayerHandler) HandleCommandExecution(ctx *player.Context, command cmd.Command, args []string) {
134-
if h.manager == nil {
135-
return
136-
}
13789
h.manager.EmitCommand(ctx, ctx.Val(), command.Name(), args)
13890
}
13991

14092
func (h *PlayerHandler) HandleBlockBreak(ctx *player.Context, pos cube.Pos, drops *[]item.Stack, xp *int) {
141-
if h.manager == nil {
142-
return
143-
}
14493
p := ctx.Val()
14594
worldDim := fmt.Sprint(p.Tx().World().Dimension())
14695
h.manager.EmitBlockBreak(ctx, p, pos, drops, xp, worldDim)
14796
}
14897

14998
func (h *PlayerHandler) HandleQuit(p *player.Player) {
150-
if h.manager == nil {
151-
return
152-
}
15399
h.manager.EmitPlayerQuit(p)
154100
}
155101

156102
func (h *PlayerHandler) HandleBlockPlace(ctx *player.Context, pos cube.Pos, b world.Block) {
157-
if h.manager == nil {
158-
return
159-
}
160103
h.manager.EmitPlayerBlockPlace(ctx, ctx.Val(), pos, b)
161104
}
162105

163106
func (h *PlayerHandler) HandleBlockPick(ctx *player.Context, pos cube.Pos, b world.Block) {
164-
if h.manager == nil {
165-
return
166-
}
167107
h.manager.EmitPlayerBlockPick(ctx, ctx.Val(), pos, b)
168108
}
169109

170110
func (h *PlayerHandler) HandleItemUse(ctx *player.Context) {
171-
if h.manager == nil {
172-
return
173-
}
174111
h.manager.EmitPlayerItemUse(ctx, ctx.Val())
175112
}
176113

177114
func (h *PlayerHandler) HandleItemUseOnBlock(ctx *player.Context, pos cube.Pos, face cube.Face, clickPos mgl64.Vec3) {
178-
if h.manager == nil {
179-
return
180-
}
181115
p := ctx.Val()
182116
if p == nil {
183117
return
@@ -190,99 +124,57 @@ func (h *PlayerHandler) HandleItemUseOnBlock(ctx *player.Context, pos cube.Pos,
190124
}
191125

192126
func (h *PlayerHandler) HandleItemUseOnEntity(ctx *player.Context, e world.Entity) {
193-
if h.manager == nil {
194-
return
195-
}
196127
h.manager.EmitPlayerItemUseOnEntity(ctx, ctx.Val(), e)
197128
}
198129

199130
func (h *PlayerHandler) HandleItemRelease(ctx *player.Context, it item.Stack, dur time.Duration) {
200-
if h.manager == nil {
201-
return
202-
}
203131
h.manager.EmitPlayerItemRelease(ctx, ctx.Val(), it, dur)
204132
}
205133

206134
func (h *PlayerHandler) HandleItemConsume(ctx *player.Context, it item.Stack) {
207-
if h.manager == nil {
208-
return
209-
}
210135
h.manager.EmitPlayerItemConsume(ctx, ctx.Val(), it)
211136
}
212137

213138
func (h *PlayerHandler) HandleAttackEntity(ctx *player.Context, e world.Entity, force, height *float64, critical *bool) {
214-
if h.manager == nil {
215-
return
216-
}
217139
h.manager.EmitPlayerAttackEntity(ctx, ctx.Val(), e, force, height, critical)
218140
}
219141

220142
func (h *PlayerHandler) HandleExperienceGain(ctx *player.Context, amount *int) {
221-
if h.manager == nil {
222-
return
223-
}
224143
h.manager.EmitPlayerExperienceGain(ctx, ctx.Val(), amount)
225144
}
226145

227146
func (h *PlayerHandler) HandlePunchAir(ctx *player.Context) {
228-
if h.manager == nil {
229-
return
230-
}
231147
h.manager.EmitPlayerPunchAir(ctx, ctx.Val())
232148
}
233149

234150
func (h *PlayerHandler) HandleSignEdit(ctx *player.Context, pos cube.Pos, frontSide bool, oldText, newText string) {
235-
if h.manager == nil {
236-
return
237-
}
238151
h.manager.EmitPlayerSignEdit(ctx, ctx.Val(), pos, frontSide, oldText, newText)
239152
}
240153

241154
func (h *PlayerHandler) HandleLecternPageTurn(ctx *player.Context, pos cube.Pos, oldPage int, newPage *int) {
242-
if h.manager == nil {
243-
return
244-
}
245155
h.manager.EmitPlayerLecternPageTurn(ctx, ctx.Val(), pos, oldPage, newPage)
246156
}
247157

248158
func (h *PlayerHandler) HandleItemDamage(ctx *player.Context, it item.Stack, damage int) {
249-
if h.manager == nil {
250-
return
251-
}
252159
h.manager.EmitPlayerItemDamage(ctx, ctx.Val(), it, damage)
253160
}
254161

255162
func (h *PlayerHandler) HandleItemPickup(ctx *player.Context, it *item.Stack) {
256-
if h.manager == nil {
257-
return
258-
}
259163
h.manager.EmitPlayerItemPickup(ctx, ctx.Val(), it)
260164
}
261165

262166
func (h *PlayerHandler) HandleHeldSlotChange(ctx *player.Context, from, to int) {
263-
if h.manager == nil {
264-
return
265-
}
266167
h.manager.EmitPlayerHeldSlotChange(ctx, ctx.Val(), from, to)
267168
}
268169

269170
func (h *PlayerHandler) HandleItemDrop(ctx *player.Context, it item.Stack) {
270-
if h.manager == nil {
271-
return
272-
}
273171
h.manager.EmitPlayerItemDrop(ctx, ctx.Val(), it)
274172
}
275173

276174
func (h *PlayerHandler) HandleTransfer(ctx *player.Context, addr *net.UDPAddr) {
277-
if h.manager == nil {
278-
return
279-
}
280175
h.manager.EmitPlayerTransfer(ctx, ctx.Val(), addr)
281176
}
282177

283178
func (h *PlayerHandler) HandleDiagnostics(p *player.Player, d session.Diagnostics) {
284-
if h.manager == nil {
285-
return
286-
}
287179
h.manager.EmitPlayerDiagnostics(p, d)
288180
}

plugin/adapters/handlers/world.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,85 +19,49 @@ func NewWorldHandler(manager ports.EventManager) world.Handler {
1919
}
2020

2121
func (h *WorldHandler) HandleClose(tx *world.Tx) {
22-
if h.manager == nil {
23-
return
24-
}
2522
h.manager.EmitWorldClose(tx)
2623
}
2724

2825
func (h *WorldHandler) HandleLiquidFlow(ctx *world.Context, from, into cube.Pos, liquid world.Liquid, replaced world.Block) {
29-
if h.manager == nil {
30-
return
31-
}
3226
h.manager.EmitWorldLiquidFlow(ctx, from, into, liquid, replaced)
3327
}
3428

3529
func (h *WorldHandler) HandleLiquidDecay(ctx *world.Context, pos cube.Pos, before, after world.Liquid) {
36-
if h.manager == nil {
37-
return
38-
}
3930
h.manager.EmitWorldLiquidDecay(ctx, pos, before, after)
4031
}
4132

4233
func (h *WorldHandler) HandleLiquidHarden(ctx *world.Context, pos cube.Pos, liquidHardened, otherLiquid, newBlock world.Block) {
43-
if h.manager == nil {
44-
return
45-
}
4634
h.manager.EmitWorldLiquidHarden(ctx, pos, liquidHardened, otherLiquid, newBlock)
4735
}
4836

4937
func (h *WorldHandler) HandleSound(ctx *world.Context, s world.Sound, pos mgl64.Vec3) {
50-
if h.manager == nil {
51-
return
52-
}
5338
h.manager.EmitWorldSound(ctx, s, pos)
5439
}
5540

5641
func (h *WorldHandler) HandleFireSpread(ctx *world.Context, from, to cube.Pos) {
57-
if h.manager == nil {
58-
return
59-
}
6042
h.manager.EmitWorldFireSpread(ctx, from, to)
6143
}
6244

6345
func (h *WorldHandler) HandleBlockBurn(ctx *world.Context, pos cube.Pos) {
64-
if h.manager == nil {
65-
return
66-
}
6746
h.manager.EmitWorldBlockBurn(ctx, pos)
6847
}
6948

7049
func (h *WorldHandler) HandleCropTrample(ctx *world.Context, pos cube.Pos) {
71-
if h.manager == nil {
72-
return
73-
}
7450
h.manager.EmitWorldCropTrample(ctx, pos)
7551
}
7652

7753
func (h *WorldHandler) HandleLeavesDecay(ctx *world.Context, pos cube.Pos) {
78-
if h.manager == nil {
79-
return
80-
}
8154
h.manager.EmitWorldLeavesDecay(ctx, pos)
8255
}
8356

8457
func (h *WorldHandler) HandleEntitySpawn(tx *world.Tx, e world.Entity) {
85-
if h.manager == nil {
86-
return
87-
}
8858
h.manager.EmitWorldEntitySpawn(tx, e)
8959
}
9060

9161
func (h *WorldHandler) HandleEntityDespawn(tx *world.Tx, e world.Entity) {
92-
if h.manager == nil {
93-
return
94-
}
9562
h.manager.EmitWorldEntityDespawn(tx, e)
9663
}
9764

9865
func (h *WorldHandler) HandleExplosion(ctx *world.Context, position mgl64.Vec3, entities *[]world.Entity, blocks *[]cube.Pos, itemDropChance *float64, spawnFire *bool) {
99-
if h.manager == nil {
100-
return
101-
}
10266
h.manager.EmitWorldExplosion(ctx, position, entities, blocks, itemDropChance, spawnFire)
10367
}

plugin/adapters/plugin/event_helpers.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,53 @@ func protoItemStackPtr(it *item.Stack) *pb.ItemStack {
114114
return protoItemStack(*it)
115115
}
116116

117+
func convertProtoItemStackValue(stack *pb.ItemStack) (item.Stack, bool) {
118+
if stack == nil || stack.Name == "" {
119+
return item.Stack{}, false
120+
}
121+
material, ok := world.ItemByName(stack.Name, int16(stack.Meta))
122+
if !ok {
123+
return item.Stack{}, false
124+
}
125+
count := int(stack.Count)
126+
if count <= 0 {
127+
return item.Stack{}, false
128+
}
129+
return item.NewStack(material, count), true
130+
}
131+
132+
func convertProtoBlockPositionsToCube(blocks []*pb.BlockPos) []cube.Pos {
133+
if len(blocks) == 0 {
134+
return nil
135+
}
136+
converted := make([]cube.Pos, 0, len(blocks))
137+
for _, blk := range blocks {
138+
if blk == nil {
139+
continue
140+
}
141+
converted = append(converted, cube.Pos{int(blk.X), int(blk.Y), int(blk.Z)})
142+
}
143+
if len(converted) == 0 {
144+
return nil
145+
}
146+
return converted
147+
}
148+
149+
func vec3FromProto(vec *pb.Vec3) (mgl64.Vec3, bool) {
150+
if vec == nil {
151+
return mgl64.Vec3{}, false
152+
}
153+
return mgl64.Vec3{float64(vec.X), float64(vec.Y), float64(vec.Z)}, true
154+
}
155+
156+
func parseProtoAddress(addr *pb.Address) *net.UDPAddr {
157+
if addr == nil {
158+
return nil
159+
}
160+
parsed := net.ParseIP(addr.Host)
161+
return &net.UDPAddr{IP: parsed, Port: int(addr.Port)}
162+
}
163+
117164
func protoWorldRef(w *world.World) *pb.WorldRef {
118165
if w == nil {
119166
return nil

0 commit comments

Comments
 (0)