Improve error handling

This commit is contained in:
William 2023-02-24 18:42:02 +00:00
parent 0fb1a3146a
commit a968fc32fc
No known key found for this signature in database
2 changed files with 40 additions and 20 deletions

View File

@ -1,8 +1,8 @@
package net.william278.velocitab;
import com.google.inject.Inject;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Dependency;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
@ -22,6 +22,7 @@ import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Optional;
@Plugin(
@ -127,4 +128,11 @@ public class Velocitab {
.orElse(0));
}
public void log(@NotNull String message, @NotNull Throwable... exceptions) {
Arrays.stream(exceptions).findFirst().ifPresentOrElse(
exception -> logger.error(message, exception),
() -> logger.warn(message)
);
}
}

View File

@ -23,12 +23,16 @@ public class ScoreboardManager {
}
public void registerPacket() {
Protocolize.protocolRegistration().registerPacket(
UpdateTeamsPacket.MAPPINGS,
Protocol.PLAY,
PacketDirection.CLIENTBOUND,
UpdateTeamsPacket.class
);
try {
Protocolize.protocolRegistration().registerPacket(
UpdateTeamsPacket.MAPPINGS,
Protocol.PLAY,
PacketDirection.CLIENTBOUND,
UpdateTeamsPacket.class
);
} catch (Exception e) {
plugin.log("Failed to register UpdateTeamsPacket", e);
}
}
public void setPlayerTeam(@NotNull TabPlayer player) {
@ -38,23 +42,31 @@ public class ScoreboardManager {
private void createTeam(@NotNull String teamName, @NotNull Player member) {
final UUID uuid = member.getUniqueId();
final UpdateTeamsPacket createTeamPacket = UpdateTeamsPacket.create(teamName, member.getUsername());
plugin.getServer().getAllPlayers().stream()
.map(Player::getUniqueId)
.map(Protocolize.playerProvider()::player)
.forEach(protocolPlayer -> protocolPlayer.sendPacket(createTeamPacket));
fauxTeams.put(uuid, teamName);
try {
final UpdateTeamsPacket createTeamPacket = UpdateTeamsPacket.create(teamName, member.getUsername());
fauxTeams.put(uuid, teamName);
plugin.getServer().getAllPlayers().stream()
.map(Player::getUniqueId)
.map(Protocolize.playerProvider()::player)
.forEach(protocolPlayer -> protocolPlayer.sendPacket(createTeamPacket));
} catch (Exception e) {
plugin.log("Skipped setting team for " + member.getUsername());
}
}
public void removeTeam(@NotNull Player member) {
final UUID uuid = member.getUniqueId();
final String teamName = fauxTeams.getOrDefault(uuid, null);
if (teamName != null) {
final UpdateTeamsPacket removeTeamPacket = UpdateTeamsPacket.remove(teamName);
plugin.getServer().getAllPlayers().stream()
.map(Player::getUniqueId)
.map(Protocolize.playerProvider()::player)
.forEach(protocolPlayer -> protocolPlayer.sendPacket(removeTeamPacket));
try {
final String teamName = fauxTeams.getOrDefault(uuid, null);
if (teamName != null) {
final UpdateTeamsPacket removeTeamPacket = UpdateTeamsPacket.remove(teamName);
plugin.getServer().getAllPlayers().stream()
.map(Player::getUniqueId)
.map(Protocolize.playerProvider()::player)
.forEach(protocolPlayer -> protocolPlayer.sendPacket(removeTeamPacket));
}
} catch (Exception e) {
plugin.log("Skipped removing team for " + member.getUsername());
}
fauxTeams.remove(uuid);
}