-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
147 lines (129 loc) · 3.85 KB
/
client.go
File metadata and controls
147 lines (129 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package goproxy
import (
"net"
"github.com/infinitygamers/goproxy/packets"
"github.com/google/uuid"
packets2 "github.com/Irmine/GoMine/net/packets"
"time"
"github.com/golang/geo/r3"
)
type Client struct {
Host // Server extends a host that has writable packets
// Server's udp address
addr net.UDPAddr
// the main Proxy
Proxy *Proxy
// the Connection between the Server
// and the Client
Conn *Connection
// a bool that is true if the Client
// is connected with the Server/Proxy
Connection bool
// the UUID of the Client's player
uuid uuid.UUID
// the entity runtime id of
// the Client's player
//entityRuntimeID uint64
// the Client's position in the level
Position r3.Vector
}
// returns new Client host
// that has writable packets
func NewClient(proxy *Proxy, conn *Connection) *Client {
client := Client{}
client.Proxy = proxy
client.Conn = conn
client.Connection = false
return &client
}
// returns if this host is the client
// this is the client struct to it returns true
func (client Client) IsClient() bool {
return true
}
// returns if this host is the client
// this is the client struct to it returns false
func (client Client) IsServer() bool {
return false
}
// this function is from the host interface
// it writes a packet buffer to the Client
func (client Client) WritePacket(buffer []byte) {
_, err := client.Proxy.UDPConn.WriteTo(buffer, &client.addr)
if err != nil {
Alert(err.Error())
}
}
// this function is from the host interface
// This sends a single packet
func (client Client) SendPacket(packet packets2.IPacket) {
client.SendBatchPacket([]packets2.IPacket{packet})
}
// this function is from the host interface
// it sends a batch packet:
// a packet with multiple packets inside
func (client Client) SendBatchPacket(packets []packets2.IPacket) {
datagram := client.Conn.pkHandler.DatagramBuilder.BuildFromPackets(packets)
client.WritePacket(datagram.Buffer)
}
// this set's the udp address
// which is used to communicate with the Client
func (client *Client) SetAddress(addr net.UDPAddr) {
client.addr = addr
}
// returns the Client's address as net.UDPAddr
func (client Client) GetAddress() net.UDPAddr {
return client.addr
}
// set true if the Client is connected with the
// Server/Proxy
func (client *Client) SetConnected(b bool) {
client.Connection = b
}
// returns true if the Client is connected with the
// Server/Proxy
func (client *Client) IsConnected() bool {
return client.Connection
}
// sends the Client's player a string message
func (client *Client) SendMessage(m string) {
text := packets.NewTextPacket()
text.Message = m
client.SendPacket(text)
}
// changes the Client's player game mode
// although it updates for the Client, it will
// not on the Server's side
func (client *Client) SetGameMode(g int32) {
gm := packets.NewSetGamemodePacket()
gm.GameMode = g
client.Conn.Server.SendPacket(gm)
client.SendPacket(gm)
}
// Set a screen title and subtitle to the Client
func (client *Client) SetTitle(title, subtitle string, fadeInTime, stayTime, fadeOutTime int32) {
// title
t := packets.NewSetTitlePacket()
t.TitleType = packets.SetTitle
t.Text = title
t.FadeInTime = fadeInTime
t.StayTime = stayTime
t.FadeOutTime = fadeOutTime
// subtitle
t2 := packets.NewSetTitlePacket()
t2.TitleType = packets.SetSubtitle
t2.Text = subtitle
t2.FadeInTime = fadeInTime
t2.StayTime = stayTime
t2.FadeOutTime = fadeOutTime
client.SendBatchPacket([]packets2.IPacket{t, t2})
}
func (client *Client) SendJoinMessage() {
go func() {
time.Sleep(5 * time.Second)
client.SendMessage(BrightBlue + "==============================")
client.SendMessage(BrightGreen + Prefix + Orange + "You are using " + Author + "'s Proxy version " + Version)
client.SendMessage(BrightBlue + "==============================")
client.SetTitle(BrightGreen + Author + "'s Proxy", Orange + Author + "'s Proxy version " + Version, 1, 1, 1)
}()
}