Skip to content

Commit

Permalink
preader/pwriter
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer committed Dec 28, 2023
1 parent fd44931 commit ac98a93
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 246 deletions.
232 changes: 45 additions & 187 deletions pkg/edition/java/proto/packet/joingame.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ type DeathPosition struct {
Value int64
}

func (d *DeathPosition) encode(wr io.Writer) error {
func (d *DeathPosition) encode(wr io.Writer) {
w := util.PanicWriter(wr)
w.Bool(d != nil)
if d != nil {
w.String(d.Key)
w.Int64(d.Value)
}
return nil
}

func decodeDeathPosition(rd io.Reader) (*DeathPosition, error) {
Expand Down Expand Up @@ -126,10 +125,7 @@ func (j *JoinGame) encode116Up(c *proto.PacketContext, wr io.Writer) error {
w.Bool(j.DimensionInfo.DebugType)
w.Bool(j.DimensionInfo.Flat)
if c.Protocol.GreaterEqual(version.Minecraft_1_19) {
err = j.LastDeathPosition.encode(wr)
if err != nil {
return err
}
j.LastDeathPosition.encode(wr)
}
if c.Protocol.GreaterEqual(version.Minecraft_1_20) {
w.VarInt(j.PortalCooldown)
Expand Down Expand Up @@ -173,107 +169,34 @@ func (j *JoinGame) encodeLegacy(c *proto.PacketContext, wr io.Writer) error {
return nil
}
func (j *JoinGame) encode1202Up(c *proto.PacketContext, wr io.Writer) error {
err := util.WriteInt(wr, j.EntityID)
if err != nil {
return err
}
err = util.WriteBool(wr, j.Hardcore)
if err != nil {
return err
}

err = util.WriteStrings(wr, j.LevelNames)
if err != nil {
return err
}

err = util.WriteVarInt(wr, j.MaxPlayers)
if err != nil {
return err
}

err = util.WriteVarInt(wr, j.ViewDistance)
if err != nil {
return err
}
err = util.WriteVarInt(wr, j.SimulationDistance)
if err != nil {
return err
}

err = util.WriteBool(wr, j.ReducedDebugInfo)
if err != nil {
return err
}
err = util.WriteBool(wr, j.ShowRespawnScreen)
if err != nil {
return err
}
err = util.WriteBool(wr, j.DoLimitedCrafting)
if err != nil {
return err
}

err = util.WriteString(wr, j.DimensionInfo.RegistryIdentifier)
if err != nil {
return err
}
w := util.PanicWriter(wr)
w.Int(j.EntityID)
w.Bool(j.Hardcore)
w.Strings(j.LevelNames)
w.VarInt(j.MaxPlayers)
w.VarInt(j.ViewDistance)
w.VarInt(j.SimulationDistance)
w.Bool(j.ReducedDebugInfo)
w.Bool(j.ShowRespawnScreen)
w.Bool(j.DoLimitedCrafting)
w.String(j.DimensionInfo.RegistryIdentifier)
if j.DimensionInfo.LevelName == nil {
return errors.New("dimension info level name must not be nil")
}
err = util.WriteString(wr, *j.DimensionInfo.LevelName)
if err != nil {
return err
}
err = util.WriteInt64(wr, j.PartialHashedSeed)
if err != nil {
return err
}

err = util.WriteByte(wr, byte(j.Gamemode))
if err != nil {
return err
}
err = util.WriteByte(wr, byte(j.PreviousGamemode))
if err != nil {
return err
}

err = util.WriteBool(wr, j.DimensionInfo.DebugType)
if err != nil {
return err
}
err = util.WriteBool(wr, j.DimensionInfo.Flat)
if err != nil {
return err
}

// optional death location
w.String(*j.DimensionInfo.LevelName)
w.Int64(j.PartialHashedSeed)
w.Byte(byte(j.Gamemode))
w.Byte(byte(j.PreviousGamemode))
w.Bool(j.DimensionInfo.DebugType)
w.Bool(j.DimensionInfo.Flat)
if j.LastDeathPosition != nil {
err = util.WriteBool(wr, true)
if err != nil {
return err
}
err = util.WriteString(wr, j.LastDeathPosition.Key)
if err != nil {
return err
}
err = util.WriteInt64(wr, j.LastDeathPosition.Value)
if err != nil {
return err
}
w.Bool(true)
w.String(j.LastDeathPosition.Key)
w.Int64(j.LastDeathPosition.Value)
} else {
err = util.WriteBool(wr, false)
if err != nil {
return err
}
}

err = util.WriteVarInt(wr, j.PortalCooldown)
if err != nil {
return err
w.Bool(false)
}

w.VarInt(j.PortalCooldown)
return nil
}

Expand Down Expand Up @@ -405,83 +328,29 @@ func (j *JoinGame) decode116Up(c *proto.PacketContext, rd io.Reader) (err error)
}
return nil
}

func (j *JoinGame) decode1202Up(c *proto.PacketContext, rd io.Reader) error {
var err error
j.EntityID, err = util.ReadInt(rd)
if err != nil {
return err
}
j.Hardcore, err = util.ReadBool(rd)
if err != nil {
return err
}

j.LevelNames, err = util.ReadStringArray(rd)
if err != nil {
return err
}

j.MaxPlayers, err = util.ReadVarInt(rd)
if err != nil {
return err
}

j.ViewDistance, err = util.ReadVarInt(rd)
if err != nil {
return err
}
j.SimulationDistance, err = util.ReadVarInt(rd)
if err != nil {
return err
}
r := util.PanicReader(rd)

j.ReducedDebugInfo, err = util.ReadBool(rd)
if err != nil {
return err
}
j.ShowRespawnScreen, err = util.ReadBool(rd)
if err != nil {
return err
}
j.DoLimitedCrafting, err = util.ReadBool(rd)
if err != nil {
return err
}
r.Int(&j.EntityID)
r.Bool(&j.Hardcore)
r.Strings(&j.LevelNames)
r.VarInt(&j.MaxPlayers)
r.VarInt(&j.ViewDistance)
r.VarInt(&j.SimulationDistance)
r.Bool(&j.ReducedDebugInfo)
r.Bool(&j.ShowRespawnScreen)
r.Bool(&j.DoLimitedCrafting)

dimensionIdentifier, err := util.ReadString(rd)
if err != nil {
return err
}
levelName, err := util.ReadString(rd)
if err != nil {
return err
}
j.PartialHashedSeed, err = util.ReadInt64(rd)
if err != nil {
return err
}
dimensionIdentifier := util.PReadStringVal(rd)
levelName := util.PReadStringVal(rd)
r.Int64(&j.PartialHashedSeed)

gamemode, err := util.ReadByte(rd)
if err != nil {
return err
}
j.Gamemode = int16(gamemode)
j.Gamemode = int16(util.PReadByteVal(rd))

previousGamemode, err := util.ReadByte(rd)
if err != nil {
return err
}
j.PreviousGamemode = int16(previousGamemode)
j.PreviousGamemode = int16(util.PReadByteVal(rd))

isDebug, err := util.ReadBool(rd)
if err != nil {
return err
}
isFlat, err := util.ReadBool(rd)
if err != nil {
return err
}
isDebug := r.Ok()
isFlat := r.Ok()
j.DimensionInfo = &DimensionInfo{
RegistryIdentifier: dimensionIdentifier,
LevelName: &levelName,
Expand All @@ -490,24 +359,13 @@ func (j *JoinGame) decode1202Up(c *proto.PacketContext, rd io.Reader) error {
}

// optional death location
if ok, err := util.ReadBool(rd); err != nil {
return err
} else if ok {
key, err := util.ReadString(rd)
if err != nil {
return err
}
value, err := util.ReadInt64(rd)
if err != nil {
return err
}
if r.Ok() {
key := util.PReadStringVal(rd)
value := util.PReadInt64Val(rd)
j.LastDeathPosition = &DeathPosition{Key: key, Value: value}
}

j.PortalCooldown, err = util.ReadVarInt(rd)
if err != nil {
return err
}
r.VarInt(&j.PortalCooldown)

return nil
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/edition/java/proto/packet/respawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ func (r *Respawn) Encode(c *proto.PacketContext, wr io.Writer) (err error) {

// optional death location
if c.Protocol.GreaterEqual(version.Minecraft_1_19) {
err = r.LastDeathPosition.encode(wr)
if err != nil {
return err
}
r.LastDeathPosition.encode(wr)
}
if c.Protocol.GreaterEqual(version.Minecraft_1_20) {
err = util.WriteVarInt(wr, r.PortalCooldown)
Expand Down
Loading

0 comments on commit ac98a93

Please sign in to comment.