Skip to content

Commit

Permalink
add support for newer minecraft versions (tested in 1.18.2)
Browse files Browse the repository at this point in the history
  • Loading branch information
sduoduo233 committed Mar 13, 2024
1 parent e894e2b commit 8147520
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
7 changes: 4 additions & 3 deletions core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

const VERSION_1_8_9 = 47
const VERSION_1_18_2 = 758

var onlineCount atomic.Int32

Expand Down Expand Up @@ -51,11 +52,11 @@ type statusResponse struct {
}

// write ping response packet
func sendResponse(w io.Writer) error {
func sendResponse(w io.Writer, protocol int) error {
resp, err := json.Marshal(statusResponse{
Version: statusVersion{
Name: "1.8.9",
Protocol: VERSION_1_8_9,
Name: "gomcproxy",
Protocol: protocol,
},
Players: statusPlayers{
Max: cfg.MaxPlayer,
Expand Down
6 changes: 3 additions & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ func handler(conn net.Conn) {
switch nextState {
case 1: // status

err := handlePing(reader, conn)
err := handlePing(reader, conn, int(protocol))
if err != nil {
log.Println("handle ping error:", err)
}

case 2: // login

if protocol != VERSION_1_8_9 {
if protocol < VERSION_1_8_9 {
err := sendDisconnect(conn, "unsupported client version")
if err != nil {
log.Println("disconnect error:", err)
Expand All @@ -83,7 +83,7 @@ func handler(conn net.Conn) {
return
}

err := handleForward(reader, conn, strings.HasSuffix(string(address), "\x00FML\x00"))
err := handleForward(reader, conn, strings.HasSuffix(string(address), "\x00FML\x00"), int(protocol))
if err != nil {
log.Println("handle forward error:", err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"
)

func handleForward(reader io.Reader, writer io.Writer, fml bool) error {
func handleForward(reader io.Reader, writer io.Writer, fml bool, protocol int) error {
onlineCount.Add(1)
defer onlineCount.Add(-1)

Expand Down Expand Up @@ -59,7 +59,7 @@ func handleForward(reader io.Reader, writer io.Writer, fml bool) error {
}

pktHandshake, err := Pack(
VarInt(VERSION_1_8_9),
VarInt(protocol),
String(rewriteHost),
UShort(cfg.RewirtePort),
VarInt(2), // next state login
Expand Down
4 changes: 2 additions & 2 deletions core/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"
)

func handlePing(reader io.Reader, writer io.Writer) error {
func handlePing(reader io.Reader, writer io.Writer, protocol int) error {

// request
pkt, err := ReadPacket(reader)
Expand All @@ -19,7 +19,7 @@ func handlePing(reader io.Reader, writer io.Writer) error {
}

// response
err = sendResponse(writer)
err = sendResponse(writer, protocol)
if err != nil {
return err
}
Expand Down

0 comments on commit 8147520

Please sign in to comment.