Skip to content

Commit

Permalink
packet/event.go: Unified Marshal/Unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Mar 22, 2023
1 parent af50825 commit e1c6f5f
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 101 deletions.
46 changes: 46 additions & 0 deletions minecraft/protocol/events.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
package protocol

// TODO: Support the last seven new events.
const (
EventTypeAchievementAwarded = iota
EventTypeEntityInteract
EventTypePortalBuilt
EventTypePortalUsed
EventTypeMobKilled
EventTypeCauldronUsed
EventTypePlayerDied
EventTypeBossKilled
EventTypeAgentCommand
EventTypeAgentCreated // Unused for whatever reason?
EventTypePatternRemoved
EventTypeSlashCommandExecuted
EventTypeFishBucketed
EventTypeMobBorn
EventTypePetDied
EventTypeCauldronInteract
EventTypeComposterInteract
EventTypeBellUsed
EventTypeEntityDefinitionTrigger
EventTypeRaidUpdate
EventTypeMovementAnomaly
EventTypeMovementCorrected
EventTypeExtractHoney
EventTypeTargetBlockHit
EventTypePiglinBarter
EventTypePlayerWaxedOrUnwaxedCopper
EventTypeCodeBuilderRuntimeAction
EventTypeCodeBuilderScoreboard
EventTypeStriderRiddenInLavaInOverworld
EventTypeSneakCloseToSculkSensor
)

// Event is an event in the world that is transmitted for telemetry purposes in
// a packet.Event.
type Event struct {
// Type is the type of the event to be called. It is one of the constants that may be found above.
Type int32
// UsePlayerID ...
// TODO: Figure out what UsePlayerID is for.
UsePlayerID byte
// Data is the parsed event data.
Data EventData
}

// EventData represents an object that holds data specific to an event.
// The data it holds depends on the type.
type EventData interface {
Expand Down
1 change: 1 addition & 0 deletions minecraft/protocol/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type IO interface {
ItemDescriptorCount(i *ItemDescriptorCount)
MaterialReducer(x *MaterialReducer)
Recipe(x *Recipe)
Event(x *Event)
GameRule(x *GameRule)
AbilityValue(x *any)

Expand Down
109 changes: 8 additions & 101 deletions minecraft/protocol/packet/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,14 @@ import (
"github.com/sandertv/gophertunnel/minecraft/protocol"
)

// TODO: Support the last seven new events.
const (
EventTypeAchievementAwarded = iota
EventTypeEntityInteract
EventTypePortalBuilt
EventTypePortalUsed
EventTypeMobKilled
EventTypeCauldronUsed
EventTypePlayerDied
EventTypeBossKilled
EventTypeAgentCommand
EventTypeAgentCreated // Unused for whatever reason?
EventTypePatternRemoved
EventTypeSlashCommandExecuted
EventTypeFishBucketed
EventTypeMobBorn
EventTypePetDied
EventTypeCauldronInteract
EventTypeComposterInteract
EventTypeBellUsed
EventTypeEntityDefinitionTrigger
EventTypeRaidUpdate
EventTypeMovementAnomaly
EventTypeMovementCorrected
EventTypeExtractHoney
EventTypeTargetBlockHit
EventTypePiglinBarter
EventTypePlayerWaxedOrUnwaxedCopper
EventTypeCodeBuilderRuntimeAction
EventTypeCodeBuilderScoreboard
EventTypeStriderRiddenInLavaInOverworld
EventTypeSneakCloseToSculkSensor
)

// Event is sent by the server to send an event with additional data. It is typically sent to the client for
// telemetry reasons, much like the SimpleEvent packet.
// TODO: Figure out what UsePlayerID is for.
type Event struct {
// EntityRuntimeID is the runtime ID of the player. The runtime ID is unique for each world session, and
// entities are generally identified in packets using this runtime ID.
EntityRuntimeID uint64
// EventType is the type of the event to be called. It is one of the constants that may be found above.
EventType int32
// UsePlayerID ...
UsePlayerID byte
// EventData is the parsed event data.
EventData protocol.EventData
// Event is the event that is transmitted.
Event protocol.Event
}

// ID ...
Expand All @@ -60,69 +21,15 @@ func (*Event) ID() uint32 {

// Marshal ...
func (pk *Event) Marshal(w *protocol.Writer) {
w.Varuint64(&pk.EntityRuntimeID)
w.Varint32(&pk.EventType)
w.Uint8(&pk.UsePlayerID)

pk.EventData.Marshal(w)
pk.marshal(w)
}

// Unmarshal ...
func (pk *Event) Unmarshal(r *protocol.Reader) {
r.Varuint64(&pk.EntityRuntimeID)
r.Varint32(&pk.EventType)
r.Uint8(&pk.UsePlayerID)

switch pk.EventType {
case EventTypeAchievementAwarded:
pk.EventData = &protocol.AchievementAwardedEventData{}
case EventTypeEntityInteract:
pk.EventData = &protocol.EntityInteractEventData{}
case EventTypePortalBuilt:
pk.EventData = &protocol.PortalBuiltEventData{}
case EventTypePortalUsed:
pk.EventData = &protocol.PortalUsedEventData{}
case EventTypeMobKilled:
pk.EventData = &protocol.MobKilledEventData{}
case EventTypeCauldronUsed:
pk.EventData = &protocol.CauldronUsedEventData{}
case EventTypePlayerDied:
pk.EventData = &protocol.PlayerDiedEventData{}
case EventTypeBossKilled:
pk.EventData = &protocol.BossKilledEventData{}
case EventTypeAgentCommand:
pk.EventData = &protocol.AgentCommandEventData{}
case EventTypePatternRemoved:
pk.EventData = &protocol.PatternRemovedEventData{}
case EventTypeSlashCommandExecuted:
pk.EventData = &protocol.SlashCommandExecutedEventData{}
case EventTypeFishBucketed:
pk.EventData = &protocol.FishBucketedEventData{}
case EventTypeMobBorn:
pk.EventData = &protocol.MobBornEventData{}
case EventTypePetDied:
pk.EventData = &protocol.PetDiedEventData{}
case EventTypeCauldronInteract:
pk.EventData = &protocol.CauldronInteractEventData{}
case EventTypeComposterInteract:
pk.EventData = &protocol.ComposterInteractEventData{}
case EventTypeBellUsed:
pk.EventData = &protocol.BellUsedEventData{}
case EventTypeEntityDefinitionTrigger:
pk.EventData = &protocol.EntityDefinitionTriggerEventData{}
case EventTypeRaidUpdate:
pk.EventData = &protocol.RaidUpdateEventData{}
case EventTypeMovementAnomaly:
pk.EventData = &protocol.MovementAnomalyEventData{}
case EventTypeMovementCorrected:
pk.EventData = &protocol.MovementCorrectedEventData{}
case EventTypeExtractHoney:
pk.EventData = &protocol.ExtractHoneyEventData{}
case EventTypePlayerWaxedOrUnwaxedCopper:
pk.EventData = &protocol.WaxedOrUnwaxedCopperEventData{}
case EventTypeSneakCloseToSculkSensor:
pk.EventData = &protocol.SneakCloseToSculkSensorEventData{}
}
pk.marshal(r)
}

pk.EventData.Marshal(r)
func (pk *Event) marshal(r protocol.IO) {
r.Varuint64(&pk.EntityRuntimeID)
r.Event(&pk.Event)
}
63 changes: 63 additions & 0 deletions minecraft/protocol/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,65 @@ func (r *Reader) Recipe(x *Recipe) {
(*x).Unmarshal(r)
}

// Event reads an Event from the reader.
func (r *Reader) Event(x *Event) {
r.Varint32(&x.Type)
r.Uint8(&x.UsePlayerID)

switch x.Type {
case EventTypeAchievementAwarded:
x.Data = &AchievementAwardedEventData{}
case EventTypeEntityInteract:
x.Data = &EntityInteractEventData{}
case EventTypePortalBuilt:
x.Data = &PortalBuiltEventData{}
case EventTypePortalUsed:
x.Data = &PortalUsedEventData{}
case EventTypeMobKilled:
x.Data = &MobKilledEventData{}
case EventTypeCauldronUsed:
x.Data = &CauldronUsedEventData{}
case EventTypePlayerDied:
x.Data = &PlayerDiedEventData{}
case EventTypeBossKilled:
x.Data = &BossKilledEventData{}
case EventTypeAgentCommand:
x.Data = &AgentCommandEventData{}
case EventTypePatternRemoved:
x.Data = &PatternRemovedEventData{}
case EventTypeSlashCommandExecuted:
x.Data = &SlashCommandExecutedEventData{}
case EventTypeFishBucketed:
x.Data = &FishBucketedEventData{}
case EventTypeMobBorn:
x.Data = &MobBornEventData{}
case EventTypePetDied:
x.Data = &PetDiedEventData{}
case EventTypeCauldronInteract:
x.Data = &CauldronInteractEventData{}
case EventTypeComposterInteract:
x.Data = &ComposterInteractEventData{}
case EventTypeBellUsed:
x.Data = &BellUsedEventData{}
case EventTypeEntityDefinitionTrigger:
x.Data = &EntityDefinitionTriggerEventData{}
case EventTypeRaidUpdate:
x.Data = &RaidUpdateEventData{}
case EventTypeMovementAnomaly:
x.Data = &MovementAnomalyEventData{}
case EventTypeMovementCorrected:
x.Data = &MovementCorrectedEventData{}
case EventTypeExtractHoney:
x.Data = &ExtractHoneyEventData{}
case EventTypePlayerWaxedOrUnwaxedCopper:
x.Data = &WaxedOrUnwaxedCopperEventData{}
case EventTypeSneakCloseToSculkSensor:
x.Data = &SneakCloseToSculkSensorEventData{}
}

x.Data.Marshal(r)
}

// AbilityValue reads an ability value from the reader.
func (r *Reader) AbilityValue(x *any) {
valType, boolVal, floatVal := uint8(0), false, float32(0)
Expand All @@ -512,6 +571,10 @@ func (r *Reader) AbilityValue(x *any) {
}
}

func (r *Reader) EventData(x *EventData) {

}

// LimitUint32 checks if the value passed is lower than the limit passed. If not, the Reader panics.
func (r *Reader) LimitUint32(value uint32, max uint32) {
if max == math.MaxUint32 {
Expand Down
7 changes: 7 additions & 0 deletions minecraft/protocol/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ func (w *Writer) Recipe(x *Recipe) {
(*x).Marshal(w)
}

// Event writes an Event to the writer.
func (w *Writer) Event(x *Event) {
w.Varint32(&x.Type)
w.Uint8(&x.UsePlayerID)
x.Data.Marshal(w)
}

// AbilityValue writes an ability value to the writer.
func (w *Writer) AbilityValue(x *any) {
switch val := (*x).(type) {
Expand Down

0 comments on commit e1c6f5f

Please sign in to comment.