Skip to content

Commit

Permalink
fix js race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
olebeck committed Jul 26, 2024
1 parent 61f2079 commit 1aa3647
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 5 deletions.
6 changes: 4 additions & 2 deletions handlers/worlds/packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ func (w *worldsHandler) packetCB(_pk packet.Packet, toServer bool, timeReceived
for i, gameRule := range pk.GameRules {
if gameRule.Name == "showCoordinates" {
haveGameRule = true
gameRule.Value = true
pk.GameRules[i] = gameRule
pk.GameRules[i] = protocol.GameRule{
Name: "showCoordinates",
Value: true,
}
break
}
}
Expand Down
4 changes: 4 additions & 0 deletions handlers/worlds/scripting/scripting.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"strconv"
"strings"
"sync"
"time"

"github.com/bedrock-tool/bedrocktool/handlers/worlds/entity"
Expand All @@ -24,6 +25,7 @@ var enums_js string

type VM struct {
vm *goja.Runtime
l sync.Mutex
log *logrus.Entry
CB struct {
OnEntityAdd func(entity any, metadata entityDataObject, properties map[string]*entity.EntityProperty, timeReceived float64) (apply bool)
Expand Down Expand Up @@ -234,6 +236,8 @@ func (v *VM) OnPacket(pk packet.Packet, toServer bool, timeReceived time.Time) (
return false
}

v.l.Lock()
defer v.l.Unlock()
err := recovery.Call(func() error {
packetName := strings.Split(reflect.TypeOf(pk).String(), ".")[1]
drop = v.CB.OnPacket(packetName, pk, toServer, float64(timeReceived.UnixMilli()))
Expand Down
2 changes: 1 addition & 1 deletion scripting/bedrocktool.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ declare type SpawnParticleCallback = (name: string, pos: [number, number, number
* @param packet - The packet data.
* @param time - The time the particle was spawned.
*/
declare type PacketCallback = (name: string, packet: any, time: number) => void;
declare type PacketCallback = (name: string, packet: any, toServer: boolean, time: number) => void;



Expand Down
2 changes: 1 addition & 1 deletion scripting/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ events.register('SpawnParticle', (name, pos, time) => {
console.log(`SpawnParticle ${name}`);
});

events.register('Packet', (name, packet, time) => {
events.register('Packet', (name, packet, toServer, time) => {
if(name == "LevelSoundEvent") {
console.log(`Packet ${name} ${JSON.stringify(packet)}`);
}
Expand Down
2 changes: 1 addition & 1 deletion scripting/scripting-ts/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ events.register('SpawnParticle', (name, pos, time) => {
console.log(`SpawnParticle ${name}`);
});

events.register('Packet', (name, packet, time) => {
events.register('Packet', (name, packet, toServer, time) => {
if(name == "LevelSoundEvent") {
console.log(`Packet ${name} ${JSON.stringify(packet)}`);
}
Expand Down

0 comments on commit 1aa3647

Please sign in to comment.