mirror of
https://github.com/ViaVersion/ViaVersion.git
synced 2024-11-21 17:45:36 +01:00
Don't unnecessarily re-wrap objects in passthrough
This commit is contained in:
parent
2841bf3040
commit
a9c947517c
@ -54,7 +54,7 @@ public abstract class PacketHandlers implements PacketHandler {
|
||||
* @param type type to map
|
||||
*/
|
||||
public <T> void map(Type<T> type) {
|
||||
handler(wrapper -> wrapper.write(type, wrapper.read(type)));
|
||||
handler(wrapper -> wrapper.passthrough(type));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -133,23 +133,26 @@ public class PacketWrapperImpl implements PacketWrapper {
|
||||
|
||||
@Override
|
||||
public <T> T read(Type<T> type) {
|
||||
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 createInformativeException(e, type, packetValues.size() + 1);
|
||||
}
|
||||
}
|
||||
return readableObjects.isEmpty() ? readFromBuffer(type) : pollReadableObject(type).value;
|
||||
}
|
||||
|
||||
PacketValue readValue = readableObjects.poll();
|
||||
private <T> T readFromBuffer(Type<T> type) {
|
||||
Preconditions.checkNotNull(inputBuffer, "This packet does not have an input buffer.");
|
||||
try {
|
||||
return type.read(inputBuffer);
|
||||
} catch (Exception e) {
|
||||
throw createInformativeException(e, type, packetValues.size() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
private <T> PacketValue<T> pollReadableObject(Type<T> type) {
|
||||
PacketValue<?> readValue = readableObjects.poll();
|
||||
Type<?> readType = readValue.type();
|
||||
if (readType == type
|
||||
|| (type.getBaseClass() == readType.getBaseClass()
|
||||
&& type.getOutputClass() == readType.getOutputClass())) {
|
||||
//noinspection unchecked
|
||||
return (T) readValue.value();
|
||||
return (PacketValue<T>) readValue;
|
||||
} else {
|
||||
throw createInformativeException(new IOException("Unable to read type " + type.getTypeName() + ", found " + readValue.type().getTypeName()), type, readableObjects.size());
|
||||
}
|
||||
@ -182,9 +185,15 @@ public class PacketWrapperImpl implements PacketWrapper {
|
||||
|
||||
@Override
|
||||
public <T> T passthrough(Type<T> type) throws InformativeException {
|
||||
T value = read(type);
|
||||
write(type, value);
|
||||
return value;
|
||||
if (readableObjects.isEmpty()) {
|
||||
T value = readFromBuffer(type);
|
||||
packetValues.add(new PacketValue<>(type, value));
|
||||
return value;
|
||||
} else {
|
||||
PacketValue<T> value = pollReadableObject(type);
|
||||
packetValues.add(value);
|
||||
return value.value;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -23,6 +23,7 @@ import com.viaversion.viaversion.api.minecraft.item.Item;
|
||||
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
|
||||
import com.viaversion.viaversion.api.type.Types;
|
||||
import com.viaversion.viaversion.api.type.types.version.Types1_12;
|
||||
import com.viaversion.viaversion.api.type.types.version.Types1_9;
|
||||
import com.viaversion.viaversion.protocols.v1_11_1to1_12.Protocol1_11_1To1_12;
|
||||
import com.viaversion.viaversion.protocols.v1_9_1to1_9_3.packet.ClientboundPackets1_9_3;
|
||||
import com.viaversion.viaversion.rewriter.EntityRewriter;
|
||||
@ -62,7 +63,7 @@ public class EntityPacketRewriter1_12 extends EntityRewriter<ClientboundPackets1
|
||||
map(Types.SHORT); // 9 - Velocity X
|
||||
map(Types.SHORT); // 10 - Velocity Y
|
||||
map(Types.SHORT); // 11 - Velocity Z
|
||||
map(Types1_12.ENTITY_DATA_LIST); // 12 - Entity data
|
||||
map(Types1_9.ENTITY_DATA_LIST, Types1_12.ENTITY_DATA_LIST); // 12 - Entity data
|
||||
|
||||
// Track mob and rewrite entity data
|
||||
handler(trackerAndRewriterHandler(Types1_12.ENTITY_DATA_LIST));
|
||||
@ -70,7 +71,7 @@ public class EntityPacketRewriter1_12 extends EntityRewriter<ClientboundPackets1
|
||||
});
|
||||
|
||||
registerRemoveEntities(ClientboundPackets1_9_3.REMOVE_ENTITIES);
|
||||
registerSetEntityData(ClientboundPackets1_9_3.SET_ENTITY_DATA, Types1_12.ENTITY_DATA_LIST);
|
||||
registerSetEntityData(ClientboundPackets1_9_3.SET_ENTITY_DATA, Types1_9.ENTITY_DATA_LIST, Types1_12.ENTITY_DATA_LIST);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -334,11 +334,6 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
|
||||
registerTracker(packetType, entityType, Types.VAR_INT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sub 1.17 method for entity remove packets.
|
||||
*
|
||||
* @param packetType remove entities packet type
|
||||
*/
|
||||
public void registerRemoveEntities(C packetType) {
|
||||
protocol.registerClientbound(packetType, wrapper -> {
|
||||
int[] entityIds = wrapper.passthrough(Types.VAR_INT_ARRAY_PRIMITIVE);
|
||||
@ -349,34 +344,17 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 1.17+ method for entity remove packets.
|
||||
*
|
||||
* @param packetType remove entities packet type
|
||||
*/
|
||||
public void registerRemoveEntity(C packetType) {
|
||||
public void registerSetEntityData(C packetType, @Nullable Type<List<EntityData>> dataType, Type<List<EntityData>> mappedDataType) {
|
||||
protocol.registerClientbound(packetType, wrapper -> {
|
||||
int entityId = wrapper.passthrough(Types.VAR_INT);
|
||||
tracker(wrapper.user()).removeEntity(entityId);
|
||||
});
|
||||
}
|
||||
|
||||
public void registerSetEntityData(C packetType, @Nullable Type<List<EntityData>> dataType, Type<List<EntityData>> mappedDataType) {
|
||||
protocol.registerClientbound(packetType, new PacketHandlers() {
|
||||
@Override
|
||||
public void register() {
|
||||
map(Types.VAR_INT); // 0 - Entity ID
|
||||
if (dataType != null) {
|
||||
map(dataType, mappedDataType);
|
||||
} else {
|
||||
map(mappedDataType);
|
||||
}
|
||||
handler(wrapper -> {
|
||||
int entityId = wrapper.get(Types.VAR_INT, 0);
|
||||
List<EntityData> entityData = wrapper.get(mappedDataType, 0);
|
||||
handleEntityData(entityId, entityData, wrapper.user());
|
||||
});
|
||||
List<EntityData> entityData;
|
||||
if (dataType != null) {
|
||||
entityData = wrapper.read(dataType);
|
||||
wrapper.write(mappedDataType, entityData);
|
||||
} else {
|
||||
entityData = wrapper.passthrough(mappedDataType);
|
||||
}
|
||||
handleEntityData(entityId, entityData, wrapper.user());
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user