use of com.viaversion.viaversion.exception.InformativeException in project ViaVersion by ViaVersion.
the class AbstractProtocol method transform.
@Override
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
Packet statePacket = new Packet(state, packetWrapper.getId());
Map<Packet, ProtocolPacket> packetMap = direction == Direction.CLIENTBOUND ? clientbound : serverbound;
ProtocolPacket protocolPacket = packetMap.get(statePacket);
if (protocolPacket == null) {
return;
}
// Write packet id
int unmappedId = packetWrapper.getId();
if (protocolPacket.isMappedOverTypes()) {
packetWrapper.setPacketType(protocolPacket.getMappedPacketType());
} else {
int mappedId = direction == Direction.CLIENTBOUND ? protocolPacket.getNewId() : protocolPacket.getOldId();
if (unmappedId != mappedId) {
packetWrapper.setId(mappedId);
}
}
PacketRemapper remapper = protocolPacket.getRemapper();
if (remapper != null) {
try {
remapper.remap(packetWrapper);
} catch (InformativeException e) {
// Catch InformativeExceptions, pass through CancelExceptions
throwRemapError(direction, state, unmappedId, packetWrapper.getId(), e);
return;
}
if (packetWrapper.isCancelled()) {
throw CancelException.generate();
}
}
}
use of com.viaversion.viaversion.exception.InformativeException in project ViaVersion by ViaVersion.
the class PacketWrapperImpl method read.
@Override
public <T> T read(Type<T> type) throws Exception {
if (type == Type.NOTHING)
return null;
if (readableObjects.isEmpty()) {
Preconditions.checkNotNull(inputBuffer, "This packet does not have an input buffer.");
// We could in the future log input read values, but honestly for things like bulk maps, mem waste D:
try {
return type.read(inputBuffer);
} catch (Exception e) {
throw new InformativeException(e).set("Type", type.getTypeName()).set("Packet ID", getId()).set("Packet Type", packetType).set("Data", packetValues);
}
}
Pair<Type, Object> read = readableObjects.poll();
Type rtype = read.key();
if (rtype == type || (type.getBaseClass() == rtype.getBaseClass() && type.getOutputClass() == rtype.getOutputClass())) {
return (T) read.value();
} else if (rtype == Type.NOTHING) {
// retry
return read(type);
} else {
Exception e = new IOException("Unable to read type " + type.getTypeName() + ", found " + read.key().getTypeName());
throw new InformativeException(e).set("Type", type.getTypeName()).set("Packet ID", getId()).set("Packet Type", packetType).set("Data", packetValues);
}
}
use of com.viaversion.viaversion.exception.InformativeException in project ViaVersion by ViaVersion.
the class PacketWrapperImpl method get.
@Override
public <T> T get(Type<T> type, int index) throws Exception {
int currentIndex = 0;
for (Pair<Type, Object> packetValue : packetValues) {
if (packetValue.key() != type)
continue;
if (currentIndex == index) {
return (T) packetValue.value();
}
currentIndex++;
}
Exception e = new ArrayIndexOutOfBoundsException("Could not find type " + type.getTypeName() + " at " + index);
throw new InformativeException(e).set("Type", type.getTypeName()).set("Index", index).set("Packet ID", getId()).set("Packet Type", packetType).set("Data", packetValues);
}
use of com.viaversion.viaversion.exception.InformativeException in project ViaVersion by ViaVersion.
the class PacketWrapperImpl method set.
@Override
public <T> void set(Type<T> type, int index, T value) throws Exception {
int currentIndex = 0;
for (Pair<Type, Object> packetValue : packetValues) {
if (packetValue.key() != type)
continue;
if (currentIndex == index) {
packetValue.setValue(attemptTransform(type, value));
return;
}
currentIndex++;
}
Exception e = new ArrayIndexOutOfBoundsException("Could not find type " + type.getTypeName() + " at " + index);
throw new InformativeException(e).set("Type", type.getTypeName()).set("Index", index).set("Packet ID", getId()).set("Packet Type", packetType);
}
Aggregations