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
src/main/java/net/william278/velocitab

View File

@ -1,8 +1,8 @@
package net.william278.velocitab; package net.william278.velocitab;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Dependency; import com.velocitypowered.api.plugin.Dependency;
import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.plugin.annotation.DataDirectory;
@ -22,6 +22,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays;
import java.util.Optional; import java.util.Optional;
@Plugin( @Plugin(
@ -127,4 +128,11 @@ public class Velocitab {
.orElse(0)); .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() { public void registerPacket() {
Protocolize.protocolRegistration().registerPacket( try {
UpdateTeamsPacket.MAPPINGS, Protocolize.protocolRegistration().registerPacket(
Protocol.PLAY, UpdateTeamsPacket.MAPPINGS,
PacketDirection.CLIENTBOUND, Protocol.PLAY,
UpdateTeamsPacket.class PacketDirection.CLIENTBOUND,
); UpdateTeamsPacket.class
);
} catch (Exception e) {
plugin.log("Failed to register UpdateTeamsPacket", e);
}
} }
public void setPlayerTeam(@NotNull TabPlayer player) { public void setPlayerTeam(@NotNull TabPlayer player) {
@ -38,23 +42,31 @@ public class ScoreboardManager {
private void createTeam(@NotNull String teamName, @NotNull Player member) { private void createTeam(@NotNull String teamName, @NotNull Player member) {
final UUID uuid = member.getUniqueId(); final UUID uuid = member.getUniqueId();
final UpdateTeamsPacket createTeamPacket = UpdateTeamsPacket.create(teamName, member.getUsername()); try {
plugin.getServer().getAllPlayers().stream() final UpdateTeamsPacket createTeamPacket = UpdateTeamsPacket.create(teamName, member.getUsername());
.map(Player::getUniqueId) fauxTeams.put(uuid, teamName);
.map(Protocolize.playerProvider()::player) plugin.getServer().getAllPlayers().stream()
.forEach(protocolPlayer -> protocolPlayer.sendPacket(createTeamPacket)); .map(Player::getUniqueId)
fauxTeams.put(uuid, teamName); .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) { public void removeTeam(@NotNull Player member) {
final UUID uuid = member.getUniqueId(); final UUID uuid = member.getUniqueId();
final String teamName = fauxTeams.getOrDefault(uuid, null); try {
if (teamName != null) { final String teamName = fauxTeams.getOrDefault(uuid, null);
final UpdateTeamsPacket removeTeamPacket = UpdateTeamsPacket.remove(teamName); if (teamName != null) {
plugin.getServer().getAllPlayers().stream() final UpdateTeamsPacket removeTeamPacket = UpdateTeamsPacket.remove(teamName);
.map(Player::getUniqueId) plugin.getServer().getAllPlayers().stream()
.map(Protocolize.playerProvider()::player) .map(Player::getUniqueId)
.forEach(protocolPlayer -> protocolPlayer.sendPacket(removeTeamPacket)); .map(Protocolize.playerProvider()::player)
.forEach(protocolPlayer -> protocolPlayer.sendPacket(removeTeamPacket));
}
} catch (Exception e) {
plugin.log("Skipped removing team for " + member.getUsername());
} }
fauxTeams.remove(uuid); fauxTeams.remove(uuid);
} }