Skip to content

Commit

Permalink
feat(core): handle scoreboard teams packet with packetevents
Browse files Browse the repository at this point in the history
Refreshing functionality is still not implemented.
  • Loading branch information
diogotcorreia committed Nov 17, 2024
1 parent a8a1722 commit 99f4833
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import com.rexcantor64.triton.Triton;
import com.rexcantor64.triton.packetinterceptor.handlers.ScoreboardPacketHandler;
import com.rexcantor64.triton.player.LanguagePlayer;
import lombok.val;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
Expand All @@ -23,19 +25,20 @@
*/
public class PacketEventsListener implements PacketListener {

private Map<PacketTypeCommon, Consumer<PacketSendEvent>> receiveHandlers = Collections.emptyMap();
private Map<PacketTypeCommon, BiConsumer<PacketSendEvent, LanguagePlayer>> receiveHandlers = Collections.emptyMap();

/**
* Setup handlers according to what is enabled on config.
*
* @since 4.0.0
*/
public void setupHandlers() {
val parser = Triton.get().getMessageParser();
val config = Triton.get().getConfig();
val updatedHandlers = new HashMap<PacketTypeCommon, Consumer<PacketSendEvent>>();
val updatedHandlers = new HashMap<PacketTypeCommon, BiConsumer<PacketSendEvent, LanguagePlayer>>();

if (config.isScoreboards()) {
val scoreboardHandler = new ScoreboardPacketHandler();
val scoreboardHandler = new ScoreboardPacketHandler(parser, config);
updatedHandlers.put(PacketType.Play.Server.TEAMS, scoreboardHandler::onTeamsPacket);
}

Expand All @@ -45,11 +48,11 @@ public void setupHandlers() {
@Override
public void onPacketSend(PacketSendEvent event) {
val type = event.getPacketType();
System.out.println("type = " + type);

val handler = receiveHandlers.get(type);
if (handler != null) {
handler.accept(event);
val languagePlayer = Triton.get().getPlayerManager().get(event.getUser().getUUID());
handler.accept(event, languagePlayer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,84 @@

import com.github.retrooper.packetevents.event.PacketSendEvent;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerTeams;
import com.rexcantor64.triton.config.MainConfig;
import com.rexcantor64.triton.language.parser.AdventureParser;
import com.rexcantor64.triton.player.LanguagePlayer;
import lombok.RequiredArgsConstructor;
import lombok.val;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;

@RequiredArgsConstructor
public class ScoreboardPacketHandler {

public void onTeamsPacket(PacketSendEvent event) {
private final @NotNull AdventureParser parser;
private final @NotNull MainConfig.FeatureSyntax syntax;

public ScoreboardPacketHandler(@NotNull AdventureParser parser, @NotNull MainConfig config) {
this.parser = parser;
this.syntax = config.getScoreboardSyntax();
}

public void onTeamsPacket(@NotNull PacketSendEvent event, @NotNull LanguagePlayer languagePlayer) {
WrapperPlayServerTeams teams = new WrapperPlayServerTeams(event);
// TODO

val action = teams.getTeamMode();
if (action == WrapperPlayServerTeams.TeamMode.REMOVE) {
// TODO remove from cache
}

if (action != WrapperPlayServerTeams.TeamMode.CREATE && action != WrapperPlayServerTeams.TeamMode.UPDATE) {
// we are only interested in new/update actions
return;
}

val infoOpt = teams.getTeamInfo();
if (!infoOpt.isPresent()) {
// this should never happen since we have filtered by the actions before
return;
}
val info = infoOpt.get();

// display name
parser.translateComponent(
info.getDisplayName(),
languagePlayer,
syntax
)
.getResultOrToRemove(Component::empty)
.ifPresent(result -> {
info.setDisplayName(result);
event.markForReEncode(true);
});
// prefix
parser.translateComponent(
info.getPrefix(),
languagePlayer,
syntax
)
.getResultOrToRemove(Component::empty)
.ifPresent(result -> {
info.setPrefix(result);
event.markForReEncode(true);
});
// suffix
parser.translateComponent(
info.getSuffix(),
languagePlayer,
syntax
)
.getResultOrToRemove(Component::empty)
.ifPresent(result -> {
info.setSuffix(result);
event.markForReEncode(true);
});

if (event.needsReEncode()) {
// TODO save data to cache
} else {
// TODO remove team from cache if exists
}
}

}

0 comments on commit 99f4833

Please sign in to comment.