Add mob spawn rewriter function for pre 1.11 (#724)

This commit is contained in:
EnZaXD 2024-04-23 10:45:12 +02:00 committed by GitHub
parent 948c615622
commit 87f2a6ba7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 26 additions and 23 deletions

View File

@ -113,7 +113,7 @@ public abstract class LegacyEntityRewriter<C extends ClientboundPacketType, T ex
registerMetadataRewriter(packetType, null, metaType);
}
protected PacketHandler getMobSpawnRewriter(Type<List<Metadata>> metaType) {
protected PacketHandler getMobSpawnRewriter(Type<List<Metadata>> metaType, IdSetter idSetter) {
return wrapper -> {
int entityId = wrapper.get(Type.VAR_INT, 0);
EntityType type = tracker(wrapper.user()).entityType(entityId);
@ -123,7 +123,7 @@ public abstract class LegacyEntityRewriter<C extends ClientboundPacketType, T ex
EntityData entityData = entityDataForType(type);
if (entityData != null) {
wrapper.set(Type.VAR_INT, 1, entityData.replacementId());
idSetter.setId(wrapper, entityData.replacementId());
if (entityData.hasBaseMeta()) {
entityData.defaultMeta().createMeta(new WrappedMetadata(metadata));
}
@ -131,6 +131,14 @@ public abstract class LegacyEntityRewriter<C extends ClientboundPacketType, T ex
};
}
public PacketHandler getMobSpawnRewriter(Type<List<Metadata>> metaType) {
return getMobSpawnRewriter(metaType, (wrapper, id) -> wrapper.set(Type.UNSIGNED_BYTE, 0, (short) id));
}
public PacketHandler getMobSpawnRewriter1_11(Type<List<Metadata>> metaType) {
return getMobSpawnRewriter(metaType, (wrapper, id) -> wrapper.set(Type.VAR_INT, 1, id));
}
protected PacketHandler getObjectTrackerHandler() {
return wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), objectTypeFromId(wrapper.get(Type.BYTE, 0)));
}
@ -165,4 +173,10 @@ public abstract class LegacyEntityRewriter<C extends ClientboundPacketType, T ex
protected void addTrackedEntity(PacketWrapper wrapper, int entityId, EntityType type) throws Exception {
tracker(wrapper.user()).addEntity(entityId, type);
}
@FunctionalInterface
protected interface IdSetter {
void setId(PacketWrapper wrapper, int id) throws Exception;
}
}

View File

@ -111,24 +111,13 @@ public class EntityPackets1_11 extends LegacyEntityRewriter<ClientboundPackets1_
handler(getTrackerHandler(Type.UNSIGNED_BYTE, 0));
// Rewrite entity type / metadata
handler(getMobSpawnRewriter(Types1_9.METADATA_LIST));
// Sub 1.11 clients will error if the list is empty
handler(wrapper -> {
int entityId = wrapper.get(Type.VAR_INT, 0);
EntityType type = tracker(wrapper.user()).entityType(entityId);
List<Metadata> list = wrapper.get(Types1_9.METADATA_LIST, 0);
handleMetadata(wrapper.get(Type.VAR_INT, 0), list, wrapper.user());
EntityData entityData = entityDataForType(type);
if (entityData != null) {
wrapper.set(Type.UNSIGNED_BYTE, 0, (short) entityData.replacementId());
if (entityData.hasBaseMeta()) {
entityData.defaultMeta().createMeta(new WrappedMetadata(list));
}
}
// Sub 1.11 clients will error if the list is empty
if (list.isEmpty()) {
list.add(new Metadata(0, MetaType1_9.Byte, (byte) 0));
List<Metadata> metadata = wrapper.get(Types1_9.METADATA_LIST, 0);
if (metadata.isEmpty()) {
metadata.add(new Metadata(0, MetaType1_9.Byte, (byte) 0));
}
});
}

View File

@ -89,7 +89,7 @@ public class EntityPackets1_12 extends LegacyEntityRewriter<ClientboundPackets1_
handler(getTrackerHandler());
// Rewrite entity type / metadata
handler(getMobSpawnRewriter(Types1_12.METADATA_LIST));
handler(getMobSpawnRewriter1_11(Types1_12.METADATA_LIST));
}
});

View File

@ -78,7 +78,7 @@ public class EntityPackets1_11_1 extends LegacyEntityRewriter<ClientboundPackets
handler(getTrackerHandler());
// Rewrite entity type / metadata
handler(getMobSpawnRewriter(Types1_9.METADATA_LIST));
handler(getMobSpawnRewriter1_11(Types1_9.METADATA_LIST));
}
});

View File

@ -155,7 +155,7 @@ public class EntityPackets1_13 extends LegacyEntityRewriter<ClientboundPackets1_
});
// Rewrite entity type / metadata
handler(getMobSpawnRewriter(Types1_12.METADATA_LIST));
handler(getMobSpawnRewriter1_11(Types1_12.METADATA_LIST));
}
});

View File

@ -223,7 +223,7 @@ public class EntityPackets1_14 extends LegacyEntityRewriter<ClientboundPackets1_
});
// Handle entity type & metadata
handler(getMobSpawnRewriter(Types1_13_2.METADATA_LIST));
handler(getMobSpawnRewriter1_11(Types1_13_2.METADATA_LIST));
}
});