Skip to content

Commit

Permalink
Reworked cache again and removed async packet sending
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdev03 committed Jun 25, 2024
1 parent cbb2164 commit 334ab43
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class TextHolderProviderImpl extends TextHolderProvider {

private static final Map<String, TextHolder> legacyCache = new ConcurrentHashMap<>();
private static final Map<Component, TextHolder> modernCache = new ConcurrentHashMap<>();
private static final Map<Integer, TextHolder> pairCache = new ConcurrentHashMap<>();
private static final Map<Long, TextHolder> pairCache = new ConcurrentHashMap<>();

public TextHolderProviderImpl() {
super();
Expand Down Expand Up @@ -107,7 +107,10 @@ public TextHolder ofCombined(@NotNull String legacyText, @NotNull Component mode
} catch (ExecutionException e) {
throw new RuntimeException(e);
}*/
return pairCache.computeIfAbsent(Objects.hash(legacyText, modernText), i -> new TextHolderImpl(legacyText, modernText));
int hash1 = legacyText.hashCode();
int hash2 = modernText.hashCode();
long hash = ((long) hash1 << 32) | (hash2 & 0xFFFFFFFFL);
return pairCache.computeIfAbsent(hash, i -> new TextHolderImpl(legacyText, modernText));
}

private record Pair(@NotNull String legacy, @NotNull Component modern) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,11 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class VelocityScoreboard implements ProxyScoreboard {

public static final ProtocolVersion MAXIMUM_SUPPORTED_VERSION = ProtocolVersion.MINECRAFT_1_21;

public static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(15);

@NotNull
private final ProxyServer server;

Expand Down Expand Up @@ -187,8 +183,7 @@ public ProxyServer getServer() {

public void sendPacket(@NotNull DisplayObjectivePacket packet) {
if (viewer.getProtocolVersion().greaterThan(MAXIMUM_SUPPORTED_VERSION)) return;
// viewer.getConnection().write(packet);
EXECUTOR.submit(() -> viewer.getConnection().write(packet));
viewer.getConnection().write(packet);

// Check if a slot was freed
for (DisplaySlot slot : DisplaySlot.values()) {
Expand All @@ -197,8 +192,7 @@ public void sendPacket(@NotNull DisplayObjectivePacket packet) {
DownstreamObjective objective = downstream.getObjective(packet.getPosition());
if (objective != null) {
// Backend tried to display something in this slot, allow it now
// viewer.getConnection().write(new DisplayObjectivePacket(slot, objective.getName()));
EXECUTOR.submit(() -> viewer.getConnection().write(new DisplayObjectivePacket(slot, objective.getName())));
viewer.getConnection().write(new DisplayObjectivePacket(slot, objective.getName()));
}
}
}
Expand All @@ -211,30 +205,24 @@ public void sendPacket(@NotNull ObjectivePacket packet) {
DownstreamObjective objective = downstream.getObjective(packet.getObjectiveName());
if (objective != null) {
// Backend is using this scoreboard, unregister it to allow this
// viewer.getConnection().write(new ObjectivePacket(ObjectiveAction.UNREGISTER, packet.getObjectiveName(), null, null, null));
EXECUTOR.submit(() -> viewer.getConnection().write(new ObjectivePacket(ObjectiveAction.UNREGISTER, packet.getObjectiveName(), null, null, null)));
viewer.getConnection().write(new ObjectivePacket(ObjectiveAction.UNREGISTER, packet.getObjectiveName(), null, null, null));
}
// viewer.getConnection().write(packet);
EXECUTOR.submit(() -> viewer.getConnection().write(packet));
viewer.getConnection().write(packet);
}
case UNREGISTER -> {
// viewer.getConnection().write(packet);
EXECUTOR.submit(() -> viewer.getConnection().write(packet));
viewer.getConnection().write(packet);

// Check if backend wanted to display an objective with this name
DownstreamObjective objective = downstream.getObjective(packet.getObjectiveName());
if (objective != null) {
// Backend wants this too, send the objective and scores
// viewer.getConnection().write(new ObjectivePacket(ObjectiveAction.REGISTER, objective.getName(), objective.getTitle(), objective.getHealthDisplay(), objective.getNumberFormat()));
EXECUTOR.submit(() -> viewer.getConnection().write(new ObjectivePacket(ObjectiveAction.REGISTER, objective.getName(), objective.getTitle(), objective.getHealthDisplay(), objective.getNumberFormat())));
viewer.getConnection().write(new ObjectivePacket(ObjectiveAction.REGISTER, objective.getName(), objective.getTitle(), objective.getHealthDisplay(), objective.getNumberFormat()));
for (DownstreamScore score : objective.getAllScores()) {
if (viewer.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_20_3)) {
ComponentHolder cHolder = score.getDisplayName() == null ? null : new ComponentHolder(viewer.getProtocolVersion(), score.getDisplayName());
// viewer.getConnection().write(new ScoreSetPacket(score.getHolder(), objective.getName(), score.getScore(), cHolder, score.getNumberFormat()));
EXECUTOR.submit(() -> viewer.getConnection().write(new ScoreSetPacket(score.getHolder(), objective.getName(), score.getScore(), cHolder, score.getNumberFormat())));
viewer.getConnection().write(new ScoreSetPacket(score.getHolder(), objective.getName(), score.getScore(), cHolder, score.getNumberFormat()));
} else {
// viewer.getConnection().write(new ScorePacket(ScorePacket.ScoreAction.SET, score.getHolder(), objective.getName(), score.getScore()));
EXECUTOR.submit(() -> viewer.getConnection().write(new ScorePacket(ScorePacket.ScoreAction.SET, score.getHolder(), objective.getName(), score.getScore())));
viewer.getConnection().write(new ScorePacket(ScorePacket.ScoreAction.SET, score.getHolder(), objective.getName(), score.getScore()));
}
}
}
Expand All @@ -245,35 +233,30 @@ public void sendPacket(@NotNull ObjectivePacket packet) {
DownstreamObjective obj = downstream.getObjective(slot);
if (obj != null) {
// This slot is only used by backend, display it (may send unnecessary packets)
// viewer.getConnection().write(new DisplayObjectivePacket(slot, obj.getName()));
EXECUTOR.submit(() -> viewer.getConnection().write(new DisplayObjectivePacket(slot, obj.getName())));
viewer.getConnection().write(new DisplayObjectivePacket(slot, obj.getName()));
}
}
}
case UPDATE -> {
// Nothing should be needed here
// viewer.getConnection().write(packet);
EXECUTOR.submit(() -> viewer.getConnection().write(packet));
viewer.getConnection().write(packet);
}
}
}

public void sendPacket(@NotNull ScorePacket packet) {
if (viewer.getProtocolVersion().greaterThan(MAXIMUM_SUPPORTED_VERSION)) return;
// viewer.getConnection().write(packet);
EXECUTOR.submit(() -> viewer.getConnection().write(packet));
viewer.getConnection().write(packet);
}

public void sendPacket(@NotNull ScoreSetPacket packet) {
if (viewer.getProtocolVersion().greaterThan(MAXIMUM_SUPPORTED_VERSION)) return;
// viewer.getConnection().write(packet);
EXECUTOR.submit(() -> viewer.getConnection().write(packet));
viewer.getConnection().write(packet);
}

public void sendPacket(@NotNull ScoreResetPacket packet) {
if (viewer.getProtocolVersion().greaterThan(MAXIMUM_SUPPORTED_VERSION)) return;
// viewer.getConnection().write(packet);
EXECUTOR.submit(() -> viewer.getConnection().write(packet));
viewer.getConnection().write(packet);
}

public void sendPacket(@NotNull TeamPacket packet) {
Expand All @@ -283,37 +266,31 @@ public void sendPacket(@NotNull TeamPacket packet) {
DownstreamTeam team = downstream.getTeam(packet.getName());
if (team != null) {
// Backend is using this team, unregister it to allow this
// viewer.getConnection().write(TeamPacket.unregister(packet.getName()));
EXECUTOR.submit(() -> viewer.getConnection().write(TeamPacket.unregister(packet.getName())));
viewer.getConnection().write(TeamPacket.unregister(packet.getName()));
}
// viewer.getConnection().write(packet);
EXECUTOR.submit(() -> viewer.getConnection().write(packet));
viewer.getConnection().write(packet);
}
case UNREGISTER -> {
viewer.getConnection().write(packet);
// Check if backend wanted to display a team with this name
DownstreamTeam team = downstream.getTeam(packet.getName());
if (team != null) {
// Backend wants this too, send it
// viewer.getConnection().write(new TeamPacket(TeamPacket.TeamAction.REGISTER, team.getName(), team.getProperties(), team.getEntries()));
EXECUTOR.submit(() -> viewer.getConnection().write(new TeamPacket(TeamPacket.TeamAction.REGISTER, team.getName(), team.getProperties(), team.getEntries())));
viewer.getConnection().write(new TeamPacket(TeamPacket.TeamAction.REGISTER, team.getName(), team.getProperties(), team.getEntries()));
}
}
case UPDATE, ADD_PLAYER -> {
// Nothing should be needed here
// viewer.getConnection().write(packet);
EXECUTOR.submit(() -> viewer.getConnection().write(packet));
viewer.getConnection().write(packet);
}
case REMOVE_PLAYER -> {
// viewer.getConnection().write(packet);
EXECUTOR.submit(() -> viewer.getConnection().write(packet));
viewer.getConnection().write(packet);
// Check if backend wanted to display this player
for (Team team : downstream.getTeams()) {
for (String removedEntry : packet.getEntries()) {
if (team.getEntries().contains(removedEntry)) {
// Backend team has this player, add back
// viewer.getConnection().write(TeamPacket.addOrRemovePlayer(team.getName(), removedEntry, true));
EXECUTOR.submit(() -> viewer.getConnection().write(TeamPacket.addOrRemovePlayer(team.getName(), removedEntry, true)));
viewer.getConnection().write(TeamPacket.addOrRemovePlayer(team.getName(), removedEntry, true));
}
}
}
Expand Down

0 comments on commit 334ab43

Please sign in to comment.