From 28dac2ec327a8b64c6bc31694729deabd5a6540e Mon Sep 17 00:00:00 2001 From: Steveice10 Date: Sat, 23 May 2020 15:02:39 -0700 Subject: [PATCH] Remove -1 workarounds in favor of new user error handling, add exception subclasses for MagicValues errors. --- pom.xml | 2 +- .../steveice10/mc/protocol/data/MagicValues.java | 9 ++------- .../mc/protocol/data/UnmappedKeyException.java | 13 +++++++++++++ .../mc/protocol/data/UnmappedValueException.java | 13 +++++++++++++ .../game/entity/attribute/AttributeModifier.java | 3 ++- .../ingame/server/ServerStatisticsPacket.java | 3 ++- .../packet/ingame/server/ServerStopSoundPacket.java | 3 ++- .../ingame/server/scoreboard/ServerTeamPacket.java | 3 ++- .../server/world/ServerBlockBreakAnimPacket.java | 3 ++- .../ingame/server/world/ServerPlaySoundPacket.java | 3 ++- 10 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/github/steveice10/mc/protocol/data/UnmappedKeyException.java create mode 100644 src/main/java/com/github/steveice10/mc/protocol/data/UnmappedValueException.java diff --git a/pom.xml b/pom.xml index a6eb63dd..0ed778e6 100644 --- a/pom.xml +++ b/pom.xml @@ -62,7 +62,7 @@ com.github.steveice10 packetlib - 43b394dfdc + 614d56cdc0 compile diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/MagicValues.java b/src/main/java/com/github/steveice10/mc/protocol/data/MagicValues.java index 07186ea6..16c5bc78 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/MagicValues.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/MagicValues.java @@ -1173,11 +1173,6 @@ public class MagicValues { register(sound, sound.ordinal()); register(sound, sound.getName()); } - - // Handle some protocol version translators that may send -1 for untranslatable IDs. - // Choics are based on what (I think?) vanilla would default to. - register(EntityType.PIG, -1); - register(BuiltinSound.ENTITY_ITEM_PICKUP, -1); } private MagicValues() { @@ -1211,7 +1206,7 @@ public class MagicValues { } } - throw new IllegalArgumentException("Value " + value + " has no mapping for key class " + keyType.getName() + "."); + throw new UnmappedValueException(value, keyType); } @SuppressWarnings("unchecked") @@ -1238,6 +1233,6 @@ public class MagicValues { } } - throw new IllegalArgumentException("Key " + key + " has no mapping for value class " + valueType.getName() + "."); + throw new UnmappedKeyException(key, valueType); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/UnmappedKeyException.java b/src/main/java/com/github/steveice10/mc/protocol/data/UnmappedKeyException.java new file mode 100644 index 00000000..5e0c420c --- /dev/null +++ b/src/main/java/com/github/steveice10/mc/protocol/data/UnmappedKeyException.java @@ -0,0 +1,13 @@ +package com.github.steveice10.mc.protocol.data; + +import lombok.Getter; + +@Getter +public class UnmappedKeyException extends IllegalArgumentException { + private Enum key; + private Class valueType; + + public UnmappedKeyException(Object key, Class valueType) { + super("Key " + key + " has no mapping for value class " + valueType.getName() + "."); + } +} diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/UnmappedValueException.java b/src/main/java/com/github/steveice10/mc/protocol/data/UnmappedValueException.java new file mode 100644 index 00000000..e4bb5dbe --- /dev/null +++ b/src/main/java/com/github/steveice10/mc/protocol/data/UnmappedValueException.java @@ -0,0 +1,13 @@ +package com.github.steveice10.mc.protocol.data; + +import lombok.Getter; + +@Getter +public class UnmappedValueException extends IllegalArgumentException { + private Object value; + private Class keyType; + + public UnmappedValueException(Object value, Class keyType) { + super("Value " + value + " has no mapping for key class " + keyType.getName() + "."); + } +} diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/AttributeModifier.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/AttributeModifier.java index f578acc6..2fd1c92a 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/AttributeModifier.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/AttributeModifier.java @@ -1,6 +1,7 @@ package com.github.steveice10.mc.protocol.data.game.entity.attribute; import com.github.steveice10.mc.protocol.data.MagicValues; +import com.github.steveice10.mc.protocol.data.UnmappedValueException; import lombok.Data; import lombok.NonNull; @@ -25,7 +26,7 @@ public class AttributeModifier { ModifierType type = null; try { type = MagicValues.key(ModifierType.class, uuid); - } catch(IllegalArgumentException e) { + } catch(UnmappedValueException e) { } this.type = type; diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStatisticsPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStatisticsPacket.java index 6e167ae0..8a9bdad6 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStatisticsPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStatisticsPacket.java @@ -1,6 +1,7 @@ package com.github.steveice10.mc.protocol.packet.ingame.server; import com.github.steveice10.mc.protocol.data.MagicValues; +import com.github.steveice10.mc.protocol.data.UnmappedValueException; import com.github.steveice10.mc.protocol.data.game.statistic.BreakBlockStatistic; import com.github.steveice10.mc.protocol.data.game.statistic.BreakItemStatistic; import com.github.steveice10.mc.protocol.data.game.statistic.CraftItemStatistic; @@ -73,7 +74,7 @@ public class ServerStatisticsPacket implements Packet { default: throw new IllegalArgumentException(); } - } catch (IllegalArgumentException e) { + } catch (UnmappedValueException e) { statistic = new CustomStatistic(categoryId, statisticId); } this.statistics.put(statistic, in.readVarInt()); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStopSoundPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStopSoundPacket.java index 66a3e42b..dc3bb298 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStopSoundPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStopSoundPacket.java @@ -1,6 +1,7 @@ package com.github.steveice10.mc.protocol.packet.ingame.server; import com.github.steveice10.mc.protocol.data.MagicValues; +import com.github.steveice10.mc.protocol.data.UnmappedValueException; import com.github.steveice10.mc.protocol.data.game.world.sound.BuiltinSound; import com.github.steveice10.mc.protocol.data.game.world.sound.CustomSound; import com.github.steveice10.mc.protocol.data.game.world.sound.Sound; @@ -41,7 +42,7 @@ public class ServerStopSoundPacket implements Packet { String value = in.readString(); try { this.sound = MagicValues.key(BuiltinSound.class, value); - } catch(IllegalArgumentException e) { + } catch(UnmappedValueException e) { this.sound = new CustomSound(value); } } else { diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/scoreboard/ServerTeamPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/scoreboard/ServerTeamPacket.java index 5b50218e..6aed8bf0 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/scoreboard/ServerTeamPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/scoreboard/ServerTeamPacket.java @@ -1,6 +1,7 @@ package com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard; import com.github.steveice10.mc.protocol.data.MagicValues; +import com.github.steveice10.mc.protocol.data.UnmappedValueException; import com.github.steveice10.mc.protocol.data.game.scoreboard.CollisionRule; import com.github.steveice10.mc.protocol.data.game.scoreboard.NameTagVisibility; import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamAction; @@ -100,7 +101,7 @@ public class ServerTeamPacket implements Packet { try { this.color = MagicValues.key(TeamColor.class, in.readVarInt()); - } catch(IllegalArgumentException e) { + } catch(UnmappedValueException e) { this.color = TeamColor.NONE; } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockBreakAnimPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockBreakAnimPacket.java index 5d1903c2..ae61a773 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockBreakAnimPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockBreakAnimPacket.java @@ -1,6 +1,7 @@ package com.github.steveice10.mc.protocol.packet.ingame.server.world; import com.github.steveice10.mc.protocol.data.MagicValues; +import com.github.steveice10.mc.protocol.data.UnmappedValueException; import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; import com.github.steveice10.mc.protocol.data.game.entity.player.BlockBreakStage; import com.github.steveice10.packetlib.io.NetInput; @@ -29,7 +30,7 @@ public class ServerBlockBreakAnimPacket implements Packet { this.position = Position.read(in); try { this.stage = MagicValues.key(BlockBreakStage.class, in.readUnsignedByte()); - } catch(IllegalArgumentException e) { + } catch(UnmappedValueException e) { this.stage = BlockBreakStage.RESET; } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerPlaySoundPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerPlaySoundPacket.java index fc57b1cd..4db28ac8 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerPlaySoundPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerPlaySoundPacket.java @@ -1,6 +1,7 @@ package com.github.steveice10.mc.protocol.packet.ingame.server.world; import com.github.steveice10.mc.protocol.data.MagicValues; +import com.github.steveice10.mc.protocol.data.UnmappedValueException; import com.github.steveice10.mc.protocol.data.game.world.sound.BuiltinSound; import com.github.steveice10.mc.protocol.data.game.world.sound.CustomSound; import com.github.steveice10.mc.protocol.data.game.world.sound.Sound; @@ -35,7 +36,7 @@ public class ServerPlaySoundPacket implements Packet { String value = in.readString(); try { this.sound = MagicValues.key(BuiltinSound.class, value); - } catch(IllegalArgumentException e) { + } catch(UnmappedValueException e) { this.sound = new CustomSound(value); }