Remove -1 workarounds in favor of new user error handling, add exception subclasses for MagicValues errors.

This commit is contained in:
Steveice10 2020-05-23 15:02:39 -07:00
parent 8e6bc6386b
commit 28dac2ec32
10 changed files with 41 additions and 14 deletions

View file

@ -62,7 +62,7 @@
<dependency> <dependency>
<groupId>com.github.steveice10</groupId> <groupId>com.github.steveice10</groupId>
<artifactId>packetlib</artifactId> <artifactId>packetlib</artifactId>
<version>43b394dfdc</version> <version>614d56cdc0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>

View file

@ -1173,11 +1173,6 @@ public class MagicValues {
register(sound, sound.ordinal()); register(sound, sound.ordinal());
register(sound, sound.getName()); 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() { 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") @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);
} }
} }

View file

@ -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() + ".");
}
}

View file

@ -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() + ".");
}
}

View file

@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.data.game.entity.attribute; 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.MagicValues;
import com.github.steveice10.mc.protocol.data.UnmappedValueException;
import lombok.Data; import lombok.Data;
import lombok.NonNull; import lombok.NonNull;
@ -25,7 +26,7 @@ public class AttributeModifier {
ModifierType type = null; ModifierType type = null;
try { try {
type = MagicValues.key(ModifierType.class, uuid); type = MagicValues.key(ModifierType.class, uuid);
} catch(IllegalArgumentException e) { } catch(UnmappedValueException e) {
} }
this.type = type; this.type = type;

View file

@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.packet.ingame.server; package com.github.steveice10.mc.protocol.packet.ingame.server;
import com.github.steveice10.mc.protocol.data.MagicValues; 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.BreakBlockStatistic;
import com.github.steveice10.mc.protocol.data.game.statistic.BreakItemStatistic; import com.github.steveice10.mc.protocol.data.game.statistic.BreakItemStatistic;
import com.github.steveice10.mc.protocol.data.game.statistic.CraftItemStatistic; import com.github.steveice10.mc.protocol.data.game.statistic.CraftItemStatistic;
@ -73,7 +74,7 @@ public class ServerStatisticsPacket implements Packet {
default: default:
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} catch (IllegalArgumentException e) { } catch (UnmappedValueException e) {
statistic = new CustomStatistic(categoryId, statisticId); statistic = new CustomStatistic(categoryId, statisticId);
} }
this.statistics.put(statistic, in.readVarInt()); this.statistics.put(statistic, in.readVarInt());

View file

@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.packet.ingame.server; package com.github.steveice10.mc.protocol.packet.ingame.server;
import com.github.steveice10.mc.protocol.data.MagicValues; 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.BuiltinSound;
import com.github.steveice10.mc.protocol.data.game.world.sound.CustomSound; import com.github.steveice10.mc.protocol.data.game.world.sound.CustomSound;
import com.github.steveice10.mc.protocol.data.game.world.sound.Sound; import com.github.steveice10.mc.protocol.data.game.world.sound.Sound;
@ -41,7 +42,7 @@ public class ServerStopSoundPacket implements Packet {
String value = in.readString(); String value = in.readString();
try { try {
this.sound = MagicValues.key(BuiltinSound.class, value); this.sound = MagicValues.key(BuiltinSound.class, value);
} catch(IllegalArgumentException e) { } catch(UnmappedValueException e) {
this.sound = new CustomSound(value); this.sound = new CustomSound(value);
} }
} else { } else {

View file

@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard; 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.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.CollisionRule;
import com.github.steveice10.mc.protocol.data.game.scoreboard.NameTagVisibility; import com.github.steveice10.mc.protocol.data.game.scoreboard.NameTagVisibility;
import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamAction; import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamAction;
@ -100,7 +101,7 @@ public class ServerTeamPacket implements Packet {
try { try {
this.color = MagicValues.key(TeamColor.class, in.readVarInt()); this.color = MagicValues.key(TeamColor.class, in.readVarInt());
} catch(IllegalArgumentException e) { } catch(UnmappedValueException e) {
this.color = TeamColor.NONE; this.color = TeamColor.NONE;
} }

View file

@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.packet.ingame.server.world; 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.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.metadata.Position;
import com.github.steveice10.mc.protocol.data.game.entity.player.BlockBreakStage; import com.github.steveice10.mc.protocol.data.game.entity.player.BlockBreakStage;
import com.github.steveice10.packetlib.io.NetInput; import com.github.steveice10.packetlib.io.NetInput;
@ -29,7 +30,7 @@ public class ServerBlockBreakAnimPacket implements Packet {
this.position = Position.read(in); this.position = Position.read(in);
try { try {
this.stage = MagicValues.key(BlockBreakStage.class, in.readUnsignedByte()); this.stage = MagicValues.key(BlockBreakStage.class, in.readUnsignedByte());
} catch(IllegalArgumentException e) { } catch(UnmappedValueException e) {
this.stage = BlockBreakStage.RESET; this.stage = BlockBreakStage.RESET;
} }
} }

View file

@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.packet.ingame.server.world; 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.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.BuiltinSound;
import com.github.steveice10.mc.protocol.data.game.world.sound.CustomSound; import com.github.steveice10.mc.protocol.data.game.world.sound.CustomSound;
import com.github.steveice10.mc.protocol.data.game.world.sound.Sound; import com.github.steveice10.mc.protocol.data.game.world.sound.Sound;
@ -35,7 +36,7 @@ public class ServerPlaySoundPacket implements Packet {
String value = in.readString(); String value = in.readString();
try { try {
this.sound = MagicValues.key(BuiltinSound.class, value); this.sound = MagicValues.key(BuiltinSound.class, value);
} catch(IllegalArgumentException e) { } catch(UnmappedValueException e) {
this.sound = new CustomSound(value); this.sound = new CustomSound(value);
} }