forked from CodeCrafter47/BungeeTabListPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeam.java
More file actions
185 lines (167 loc) · 6.9 KB
/
Copy pathTeam.java
File metadata and controls
185 lines (167 loc) · 6.9 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (C) 2025 proferabg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package codecrafter47.bungeetablistplus.protocol;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class Team implements MinecraftPacket {
private String name;
private int mode;
private ComponentHolder displayName;
private ComponentHolder prefix;
private ComponentHolder suffix;
private NameTagVisibility nameTagVisibility;
private CollisionRule collisionRule;
private int color;
private byte friendlyFire;
private String[] players;
public Team(String name)
{
this.name = name;
this.mode = 1;
}
@Override
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
name = ProtocolUtils.readString(buf);
mode = buf.readByte();
if (mode == 0 || mode == 2) {
displayName = ComponentHolder.read(buf, version);
if (version.compareTo(ProtocolVersion.MINECRAFT_1_13) < 0) {
prefix = ComponentHolder.read(buf, version);
suffix = ComponentHolder.read(buf, version);
}
friendlyFire = buf.readByte();
if (version.compareTo(ProtocolVersion.MINECRAFT_1_21_5) >= 0) {
nameTagVisibility = NameTagVisibility.BY_ID[ProtocolUtils.readVarInt( buf )];
collisionRule = CollisionRule.BY_ID[ProtocolUtils.readVarInt( buf )];
} else {
nameTagVisibility = readStringToMap( buf, NameTagVisibility.BY_NAME );
if (version.compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) {
collisionRule = readStringToMap( buf, CollisionRule.BY_NAME );
}
}
color = (version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) ? ProtocolUtils.readVarInt(buf) : buf.readByte();
if (version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) {
prefix = ComponentHolder.read(buf, version);
suffix = ComponentHolder.read(buf, version);
}
}
if (mode == 0 || mode == 3 || mode == 4) {
int len = ProtocolUtils.readVarInt(buf);
players = new String[len];
for (int i = 0; i < len; i++) {
players[i] = ProtocolUtils.readString(buf);
}
}
}
@Override
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
ProtocolUtils.writeString(buf, name);
buf.writeByte(mode);
if (mode == 0 || mode == 2) {
displayName.write(buf);
if (version.compareTo(ProtocolVersion.MINECRAFT_1_13) < 0) {
prefix.write(buf);
suffix.write(buf);
}
buf.writeByte(friendlyFire);
if (version.compareTo(ProtocolVersion.MINECRAFT_1_21_5) >= 0) {
ProtocolUtils.writeVarInt(buf, nameTagVisibility.ordinal());
ProtocolUtils.writeVarInt(buf, collisionRule.ordinal());
} else {
ProtocolUtils.writeString(buf, nameTagVisibility.getKey());
if (version.compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) {
ProtocolUtils.writeString(buf, collisionRule.getKey());
}
}
if (version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) {
ProtocolUtils.writeVarInt(buf, color);
prefix.write(buf);
suffix.write(buf);
} else {
buf.writeByte(color);
}
}
if (mode == 0 || mode == 3 || mode == 4) {
ProtocolUtils.writeVarInt(buf, players.length);
for (String player : players) {
ProtocolUtils.writeString(buf, player);
}
}
}
@Override
public boolean handle(MinecraftSessionHandler minecraftSessionHandler) {
return false;
}
@Getter
@RequiredArgsConstructor
public enum NameTagVisibility {
ALWAYS("always"),
NEVER("never"),
HIDE_FOR_OTHER_TEAMS("hideForOtherTeams"),
HIDE_FOR_OWN_TEAM("hideForOwnTeam"),
UNKNOWN( "" );
private final String key;
private static final Map<String, NameTagVisibility> BY_NAME;
private static final NameTagVisibility[] BY_ID;
static {
NameTagVisibility[] values = NameTagVisibility.values();
BY_ID = Arrays.copyOf( values, values.length - 1 );
BY_NAME = Arrays.stream(values).collect(Collectors.toUnmodifiableMap(e -> e.key, e -> e));
}
}
@Getter
@RequiredArgsConstructor
public enum CollisionRule {
ALWAYS("always"),
NEVER("never"),
PUSH_OTHER_TEAMS("pushOtherTeams"),
PUSH_OWN_TEAM("pushOwnTeam");
//
private final String key;
//
private static final Map<String, CollisionRule> BY_NAME;
private static final CollisionRule[] BY_ID;
static {
CollisionRule[] values = BY_ID = CollisionRule.values();
BY_NAME = Arrays.stream(values).collect(Collectors.toUnmodifiableMap(e -> e.key, e -> e));
}
}
public static <T> T readStringToMap(ByteBuf buf, Map<String, T> map) {
String string = ProtocolUtils.readString( buf );
T result = map.get( string );
Preconditions.checkArgument( result != null, "Unknown string key %s", string );
return result;
}
}