Refactor error codes

This commit is contained in:
Melledy
2026-04-15 02:01:41 -07:00
parent 6eadc42b27
commit 50f9995785
7 changed files with 59 additions and 28 deletions
@@ -33,7 +33,8 @@ import emu.nebula.proto.Public.CharGemSlot;
import emu.nebula.proto.Public.UI32; import emu.nebula.proto.Public.UI32;
import emu.nebula.proto.PublicStarTower.StarTowerChar; import emu.nebula.proto.PublicStarTower.StarTowerChar;
import emu.nebula.proto.PublicStarTower.StarTowerCharGem; import emu.nebula.proto.PublicStarTower.StarTowerCharGem;
import emu.nebula.server.error.ServerException; import emu.nebula.server.error.ErrorCode;
import emu.nebula.server.error.NebulaException;
import emu.nebula.util.Bitset; import emu.nebula.util.Bitset;
import emu.nebula.util.ints.CustomIntArray; import emu.nebula.util.ints.CustomIntArray;
import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntArrayList;
@@ -667,16 +668,16 @@ public class GameCharacter implements GameDatabaseObject {
return true; return true;
} }
public synchronized PlayerChangeInfo generateGem(int slotId) throws ServerException { public synchronized PlayerChangeInfo generateGem(int slotId) throws NebulaException {
// Get gem slot // Get gem slot
var slot = this.getGemSlot(slotId); var slot = this.getGemSlot(slotId);
if (slot == null) { if (slot == null) {
throw new ServerException(110105, "Emblem slot doesn't exist"); throw new NebulaException(ErrorCode.SERVER_ERROR, "Emblem slot doesn't exist");
} }
// Skip if slot is full // Skip if slot is full
if (slot.isFull()) { if (slot.isFull()) {
throw new ServerException(110105, "Emblem slots are full"); throw new NebulaException(ErrorCode.SERVER_ERROR, "Emblem slots are full");
} }
// Get gem data // Get gem data
@@ -684,17 +685,17 @@ public class GameCharacter implements GameDatabaseObject {
var gemControl = GameData.getCharGemSlotControlDataTable().get(slotId); var gemControl = GameData.getCharGemSlotControlDataTable().get(slotId);
if (gemControl == null) { if (gemControl == null) {
throw new ServerException(110105, "Emblem slot data doesn't exist"); throw new NebulaException(ErrorCode.SERVER_ERROR, "Emblem slot data doesn't exist");
} }
// Check character level // Check character level
if (this.getLevel() < gemControl.getUnlockLevel()) { if (this.getLevel() < gemControl.getUnlockLevel()) {
throw new ServerException(110105, "Trekker needs to be at least level " + gemControl.getUnlockLevel()); throw new NebulaException(ErrorCode.SERVER_ERROR, "Trekker needs to be at least level " + gemControl.getUnlockLevel());
} }
// Make sure the player has the materials to craft the emblem // Make sure the player has the materials to craft the emblem
if (!getPlayer().getInventory().hasItem(gemData.getGenerateCostTid(), gemControl.getGeneratenCostQty())) { if (!getPlayer().getInventory().hasItem(gemData.getGenerateCostTid(), gemControl.getGeneratenCostQty())) {
throw new ServerException(119903); throw new NebulaException(ErrorCode.INSUFFICIENT_RESOURCES);
} }
// Generate attributes and create gem // Generate attributes and create gem
@@ -718,11 +719,11 @@ public class GameCharacter implements GameDatabaseObject {
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public synchronized PlayerChangeInfo refreshGem(int slotId, int gemIndex, RepeatedInt lockedAttributes) throws ServerException { public synchronized PlayerChangeInfo refreshGem(int slotId, int gemIndex, RepeatedInt lockedAttributes) throws NebulaException {
// Get gem from slot // Get gem from slot
var gem = this.getGemFromSlot(slotId, gemIndex); var gem = this.getGemFromSlot(slotId, gemIndex);
if (gem == null) { if (gem == null) {
throw new ServerException(111609); throw new NebulaException(ErrorCode.GEM_NOT_EXIST);
} }
// Get gem data // Get gem data
@@ -730,17 +731,17 @@ public class GameCharacter implements GameDatabaseObject {
var gemControl = GameData.getCharGemSlotControlDataTable().get(slotId); var gemControl = GameData.getCharGemSlotControlDataTable().get(slotId);
if (gemControl == null) { if (gemControl == null) {
throw new ServerException(110105, "Emblem slot data doesn't exist"); throw new NebulaException(ErrorCode.SERVER_ERROR, "Emblem slot data doesn't exist");
} }
// Check character level // Check character level
if (this.getLevel() < gemControl.getUnlockLevel()) { if (this.getLevel() < gemControl.getUnlockLevel()) {
throw new ServerException(110105, "Trekker needs to be at least level " + gemControl.getUnlockLevel()); throw new NebulaException(ErrorCode.SERVER_ERROR, "Trekker needs to be at least level " + gemControl.getUnlockLevel());
} }
// Get locked attributes // Get locked attributes
if (lockedAttributes.length() > gemControl.getLockableNum()) { if (lockedAttributes.length() > gemControl.getLockableNum()) {
throw new ServerException(110105, "You can only lock up to " + gemControl.getLockableNum() + " attributes"); throw new NebulaException(ErrorCode.SERVER_ERROR, "You can only lock up to " + gemControl.getLockableNum() + " attributes");
} }
// Calculate the materials we need // Calculate the materials we need
@@ -11,7 +11,8 @@ import emu.nebula.game.inventory.ItemParamMap;
import emu.nebula.game.player.Player; import emu.nebula.game.player.Player;
import emu.nebula.game.player.PlayerChangeInfo; import emu.nebula.game.player.PlayerChangeInfo;
import emu.nebula.game.player.PlayerManager; import emu.nebula.game.player.PlayerManager;
import emu.nebula.server.error.ErrorCode;
import emu.nebula.server.error.NebulaException;
import lombok.Getter; import lombok.Getter;
@Getter @Getter
@@ -43,17 +44,17 @@ public class ScoreBossManager extends PlayerManager {
return this.ranking; return this.ranking;
} }
public boolean apply(int levelId, long buildId) { public boolean apply(int levelId, long buildId) throws NebulaException {
// Get level // Get level from control data
var control = getControlData(); var control = getControlData();
if (control == null || !control.getLevelGroup().contains(levelId)) { if (control == null || !control.getLevelGroup().contains(levelId)) {
return false; throw new NebulaException(ErrorCode.SCORE_BOSS_NOT_AVAILABLE);
} }
// Get build // Get build
var build = this.getPlayer().getStarTowerManager().getBuildById(buildId); var build = this.getPlayer().getStarTowerManager().getBuildById(buildId);
if (build == null) { if (build == null) {
return false; throw new NebulaException(ErrorCode.BUILD_NOT_EXIST);
} }
// Set // Set
@@ -0,0 +1,24 @@
package emu.nebula.server.error;
import lombok.Getter;
// TODO Add rest of the error codes
@Getter
public enum ErrorCode {
INVALID_MESSAGE_TOKEN (100101),
INVALID_MESSAGE_TIME (100102),
INVALID_MESSAGE_COMMAND_NUMBER (100103),
SERVER_ERROR (110105),
BUILD_NOT_EXIST (111103),
SCORE_BOSS_NOT_AVAILABLE (112801),
GEM_NOT_EXIST (111609),
CONFIG_ERROR (119902),
INSUFFICIENT_RESOURCES (119903);
private int value;
private ErrorCode(int value) {
this.value = value;
}
}
@@ -4,17 +4,17 @@ import lombok.Getter;
import emu.nebula.proto.Public.Error; import emu.nebula.proto.Public.Error;
@Getter @Getter
public class ServerException extends Exception { public class NebulaException extends Exception {
private static final long serialVersionUID = -8953641375717705518L; private static final long serialVersionUID = -8953641375717705518L;
private int code; private int code;
private String[] args; private String[] args;
public ServerException(int code) { public NebulaException(ErrorCode code) {
this.code = code; this.code = code.getValue();
} }
public ServerException(int code, String... args) { public NebulaException(ErrorCode code, String... args) {
this.code = code; this.code = code.getValue();
this.args = args; this.args = args;
} }
@@ -4,7 +4,7 @@ import emu.nebula.net.NetHandler;
import emu.nebula.net.NetMsgId; import emu.nebula.net.NetMsgId;
import emu.nebula.proto.CharGemGenerate.CharGemGenerateReq; import emu.nebula.proto.CharGemGenerate.CharGemGenerateReq;
import emu.nebula.proto.CharGemGenerate.CharGemGenerateResp; import emu.nebula.proto.CharGemGenerate.CharGemGenerateResp;
import emu.nebula.server.error.ServerException; import emu.nebula.server.error.NebulaException;
import emu.nebula.net.HandlerId; import emu.nebula.net.HandlerId;
import emu.nebula.game.character.CharacterGem; import emu.nebula.game.character.CharacterGem;
import emu.nebula.game.player.PlayerChangeInfo; import emu.nebula.game.player.PlayerChangeInfo;
@@ -29,7 +29,7 @@ public class HandlerCharGemGenerateReq extends NetHandler {
try { try {
change = character.generateGem(req.getSlotId()); change = character.generateGem(req.getSlotId());
} catch (ServerException e) { } catch (NebulaException e) {
return session.encodeMsg(NetMsgId.char_gem_generate_failed_ack, e.toProto()); return session.encodeMsg(NetMsgId.char_gem_generate_failed_ack, e.toProto());
} }
@@ -4,7 +4,7 @@ import emu.nebula.net.NetHandler;
import emu.nebula.net.NetMsgId; import emu.nebula.net.NetMsgId;
import emu.nebula.proto.CharGemRefresh.CharGemRefreshReq; import emu.nebula.proto.CharGemRefresh.CharGemRefreshReq;
import emu.nebula.proto.CharGemRefresh.CharGemRefreshResp; import emu.nebula.proto.CharGemRefresh.CharGemRefreshResp;
import emu.nebula.server.error.ServerException; import emu.nebula.server.error.NebulaException;
import emu.nebula.net.HandlerId; import emu.nebula.net.HandlerId;
import emu.nebula.game.character.CharacterGem; import emu.nebula.game.character.CharacterGem;
import emu.nebula.game.player.PlayerChangeInfo; import emu.nebula.game.player.PlayerChangeInfo;
@@ -30,7 +30,7 @@ public class HandlerCharGemRefreshReq extends NetHandler {
try { try {
change = character.refreshGem(req.getSlotId(), req.getGemIndex(), req.getLockAttrs()); change = character.refreshGem(req.getSlotId(), req.getGemIndex(), req.getLockAttrs());
} catch (ServerException e) { } catch (NebulaException e) {
return session.encodeMsg(NetMsgId.char_gem_refresh_failed_ack, e.toProto()); return session.encodeMsg(NetMsgId.char_gem_refresh_failed_ack, e.toProto());
} }
@@ -3,6 +3,7 @@ package emu.nebula.server.handlers;
import emu.nebula.net.NetHandler; import emu.nebula.net.NetHandler;
import emu.nebula.net.NetMsgId; import emu.nebula.net.NetMsgId;
import emu.nebula.proto.ScoreBossApply.ScoreBossApplyReq; import emu.nebula.proto.ScoreBossApply.ScoreBossApplyReq;
import emu.nebula.server.error.NebulaException;
import emu.nebula.net.HandlerId; import emu.nebula.net.HandlerId;
import emu.nebula.net.GameSession; import emu.nebula.net.GameSession;
@@ -15,10 +16,14 @@ public class HandlerScoreBossApplyReq extends NetHandler {
var req = ScoreBossApplyReq.parseFrom(message); var req = ScoreBossApplyReq.parseFrom(message);
// Apply // Apply
boolean success = session.getPlayer().getScoreBossManager().apply(req.getLevelId(), req.getBuildId()); try {
session.getPlayer().getScoreBossManager().apply(req.getLevelId(), req.getBuildId());
} catch (NebulaException e) {
return session.encodeMsg(NetMsgId.score_boss_apply_failed_ack, e.toProto());
}
// Encode and send // Encode and send
return session.encodeMsg(success ? NetMsgId.score_boss_apply_succeed_ack : NetMsgId.score_boss_apply_failed_ack); return session.encodeMsg(NetMsgId.score_boss_apply_succeed_ack);
} }
} }