Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-11-16 04:50:08 +01:00
Don't unnecessarily re-wrap objects in passthrough
Dieser Commit ist enthalten in:
Ursprung
2841bf3040
Commit
a9c947517c
@ -54,7 +54,7 @@ public abstract class PacketHandlers implements PacketHandler {
|
|||||||
* @param type type to map
|
* @param type type to map
|
||||||
*/
|
*/
|
||||||
public <T> void map(Type<T> type) {
|
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
|
@Override
|
||||||
public <T> T read(Type<T> type) {
|
public <T> T read(Type<T> type) {
|
||||||
if (readableObjects.isEmpty()) {
|
return readableObjects.isEmpty() ? readFromBuffer(type) : pollReadableObject(type).value;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
Type<?> readType = readValue.type();
|
||||||
if (readType == type
|
if (readType == type
|
||||||
|| (type.getBaseClass() == readType.getBaseClass()
|
|| (type.getBaseClass() == readType.getBaseClass()
|
||||||
&& type.getOutputClass() == readType.getOutputClass())) {
|
&& type.getOutputClass() == readType.getOutputClass())) {
|
||||||
//noinspection unchecked
|
//noinspection unchecked
|
||||||
return (T) readValue.value();
|
return (PacketValue<T>) readValue;
|
||||||
} else {
|
} else {
|
||||||
throw createInformativeException(new IOException("Unable to read type " + type.getTypeName() + ", found " + readValue.type().getTypeName()), type, readableObjects.size());
|
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
|
@Override
|
||||||
public <T> T passthrough(Type<T> type) throws InformativeException {
|
public <T> T passthrough(Type<T> type) throws InformativeException {
|
||||||
T value = read(type);
|
if (readableObjects.isEmpty()) {
|
||||||
write(type, value);
|
T value = readFromBuffer(type);
|
||||||
return value;
|
packetValues.add(new PacketValue<>(type, value));
|
||||||
|
return value;
|
||||||
|
} else {
|
||||||
|
PacketValue<T> value = pollReadableObject(type);
|
||||||
|
packetValues.add(value);
|
||||||
|
return value.value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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.protocol.remapper.PacketHandlers;
|
||||||
import com.viaversion.viaversion.api.type.Types;
|
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_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_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.protocols.v1_9_1to1_9_3.packet.ClientboundPackets1_9_3;
|
||||||
import com.viaversion.viaversion.rewriter.EntityRewriter;
|
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); // 9 - Velocity X
|
||||||
map(Types.SHORT); // 10 - Velocity Y
|
map(Types.SHORT); // 10 - Velocity Y
|
||||||
map(Types.SHORT); // 11 - Velocity Z
|
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
|
// Track mob and rewrite entity data
|
||||||
handler(trackerAndRewriterHandler(Types1_12.ENTITY_DATA_LIST));
|
handler(trackerAndRewriterHandler(Types1_12.ENTITY_DATA_LIST));
|
||||||
@ -70,7 +71,7 @@ public class EntityPacketRewriter1_12 extends EntityRewriter<ClientboundPackets1
|
|||||||
});
|
});
|
||||||
|
|
||||||
registerRemoveEntities(ClientboundPackets1_9_3.REMOVE_ENTITIES);
|
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
|
@Override
|
||||||
|
@ -334,11 +334,6 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
|
|||||||
registerTracker(packetType, entityType, Types.VAR_INT);
|
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) {
|
public void registerRemoveEntities(C packetType) {
|
||||||
protocol.registerClientbound(packetType, wrapper -> {
|
protocol.registerClientbound(packetType, wrapper -> {
|
||||||
int[] entityIds = wrapper.passthrough(Types.VAR_INT_ARRAY_PRIMITIVE);
|
int[] entityIds = wrapper.passthrough(Types.VAR_INT_ARRAY_PRIMITIVE);
|
||||||
@ -349,34 +344,17 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void registerSetEntityData(C packetType, @Nullable Type<List<EntityData>> dataType, Type<List<EntityData>> mappedDataType) {
|
||||||
* 1.17+ method for entity remove packets.
|
|
||||||
*
|
|
||||||
* @param packetType remove entities packet type
|
|
||||||
*/
|
|
||||||
public void registerRemoveEntity(C packetType) {
|
|
||||||
protocol.registerClientbound(packetType, wrapper -> {
|
protocol.registerClientbound(packetType, wrapper -> {
|
||||||
int entityId = wrapper.passthrough(Types.VAR_INT);
|
int entityId = wrapper.passthrough(Types.VAR_INT);
|
||||||
tracker(wrapper.user()).removeEntity(entityId);
|
List<EntityData> entityData;
|
||||||
});
|
if (dataType != null) {
|
||||||
}
|
entityData = wrapper.read(dataType);
|
||||||
|
wrapper.write(mappedDataType, entityData);
|
||||||
public void registerSetEntityData(C packetType, @Nullable Type<List<EntityData>> dataType, Type<List<EntityData>> mappedDataType) {
|
} else {
|
||||||
protocol.registerClientbound(packetType, new PacketHandlers() {
|
entityData = wrapper.passthrough(mappedDataType);
|
||||||
@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());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
handleEntityData(entityId, entityData, wrapper.user());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren