-
Notifications
You must be signed in to change notification settings - Fork 2
/
VersionedScoreboardInterceptor.java
177 lines (150 loc) · 6.68 KB
/
VersionedScoreboardInterceptor.java
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
/*
* FlintMC
* Copyright (C) 2020-2021 LabyMedia GmbH and contributors
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package net.flintmc.mcapi.v1_15_2.world.scoreboard;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import net.flintmc.framework.stereotype.type.Type;
import net.flintmc.mcapi.chat.MinecraftComponentMapper;
import net.flintmc.mcapi.chat.format.ChatColor;
import net.flintmc.mcapi.world.mapper.ScoreboardMapper;
import net.flintmc.mcapi.world.scoreboad.Scoreboard;
import net.flintmc.mcapi.world.scoreboad.score.Criterias;
import net.flintmc.mcapi.world.scoreboad.score.Objective;
import net.flintmc.mcapi.world.scoreboad.score.PlayerTeam;
import net.flintmc.mcapi.world.scoreboad.score.Score;
import net.flintmc.mcapi.world.scoreboad.type.CollisionType;
import net.flintmc.mcapi.world.scoreboad.type.VisibleType;
import net.flintmc.transform.hook.Hook;
import net.minecraft.network.play.server.SDisplayObjectivePacket;
import net.minecraft.network.play.server.SScoreboardObjectivePacket;
import net.minecraft.network.play.server.STeamsPacket;
import net.minecraft.network.play.server.SUpdateScorePacket;
/** 1.15.2 implementation of the scoreboard interceptor. */
@Singleton
public class VersionedScoreboardInterceptor {
private final Scoreboard scoreboard;
private final ScoreboardMapper scoreboardMapper;
private final MinecraftComponentMapper componentMapper;
@Inject
public VersionedScoreboardInterceptor(
Scoreboard scoreboard,
ScoreboardMapper scoreboardMapper,
MinecraftComponentMapper componentMapper) {
this.scoreboard = scoreboard;
this.scoreboardMapper = scoreboardMapper;
this.componentMapper = componentMapper;
}
@Hook(
className = "net.minecraft.client.network.play.ClientPlayNetHandler",
methodName = "handleScoreboardObjective",
parameters = {@Type(reference = SScoreboardObjectivePacket.class)})
public void hookHandleScoreboardObjective(@Named("args") Object[] args) {
SScoreboardObjectivePacket packet = (SScoreboardObjectivePacket) args[0];
String objectiveName = packet.getObjectiveName();
int action = packet.getAction();
if (action == 0) {
this.scoreboard.addObjective(
objectiveName,
Criterias.DUMMY,
this.componentMapper.fromMinecraft(packet.getDisplayName()),
this.scoreboardMapper.fromMinecraftRenderType(packet.getRenderType()));
} else if (this.scoreboard.hasObjective(objectiveName)) {
Objective objective = this.scoreboard.getObjective(objectiveName);
if (action == 1) {
this.scoreboard.removeObjective(objective);
} else if (action == 2) {
objective.setRenderType(
this.scoreboardMapper.fromMinecraftRenderType(packet.getRenderType()));
objective.setDisplayName(this.componentMapper.fromMinecraft(packet.getDisplayName()));
}
}
}
@Hook(
className = "net.minecraft.client.network.play.ClientPlayNetHandler",
methodName = "handleUpdateScore",
parameters = {@Type(reference = SUpdateScorePacket.class)})
public void hookHandleUpdateScore(@Named("args") Object[] args) {
SUpdateScorePacket packet = (SUpdateScorePacket) args[0];
String objectiveName = packet.getObjectiveName();
switch (packet.getAction()) {
case CHANGE:
Objective objective = this.scoreboard.getObjective(objectiveName);
Score score = this.scoreboard.getOrCreateScore(packet.getPlayerName(), objective);
score.setScorePoints(packet.getScoreValue());
break;
case REMOVE:
this.scoreboard.removeObjectiveFromEntity(
packet.getPlayerName(), this.scoreboard.getObjective(objectiveName));
break;
}
}
@Hook(
className = "net.minecraft.client.network.play.ClientPlayNetHandler",
methodName = "handleDisplayObjective",
parameters = {@Type(reference = SDisplayObjectivePacket.class)})
public void hookHandleDisplayObjective(@Named("args") Object[] args) {
SDisplayObjectivePacket packet = (SDisplayObjectivePacket) args[0];
String name = packet.getName();
Objective objective = name == null ? null : this.scoreboard.getObjective(name);
this.scoreboard.setObjectiveInDisplaySlot(packet.getPosition(), objective);
}
@Hook(
className = "net.minecraft.client.network.play.ClientPlayNetHandler",
methodName = "handleTeams",
parameters = {@Type(reference = STeamsPacket.class)})
public void hookHandleTeams(@Named("args") Object[] args) {
STeamsPacket packet = (STeamsPacket) args[0];
int action = packet.getAction();
PlayerTeam playerTeam;
playerTeam =
action == 0
? this.scoreboard.createTeam(packet.getName())
: this.scoreboard.getTeam(packet.getName());
if (action == 0 || action == 2) {
playerTeam.setDisplayName(this.componentMapper.fromMinecraft(packet.getDisplayName()));
playerTeam.setColor(ChatColor.getByName(packet.getColor().getFriendlyName().toUpperCase()));
playerTeam.setFriendlyFlags(packet.getFriendlyFlags());
VisibleType teamVisibility = VisibleType.getByName(packet.getNameTagVisibility());
if (teamVisibility != null) {
playerTeam.setNameTagVisibility(teamVisibility);
}
CollisionType collisionType = CollisionType.getByName(packet.getCollisionRule());
if (collisionType != null) {
playerTeam.setCollisionType(collisionType);
}
playerTeam.setPrefix(this.componentMapper.fromMinecraft(packet.getPrefix()));
playerTeam.setSuffix(this.componentMapper.fromMinecraft(packet.getSuffix()));
}
if (action == 0 || action == 3) {
for (String player : packet.getPlayers()) {
this.scoreboard.addPlayerToTeam(player, playerTeam);
}
}
if (action == 4) {
for (String player : packet.getPlayers()) {
this.scoreboard.removePlayerFromTeam(player, playerTeam);
}
}
// Removes a team from the scoreboard
if (action == 1) {
this.scoreboard.removeTeam(playerTeam);
}
}
}