Created
October 11, 2023 03:56
-
-
Save theoparis/c8ab4ee8496a76c7f25c7ba1993770c3 to your computer and use it in GitHub Desktop.
bruh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.theoparis.fluidcraft_datagen; | |
import com.google.common.base.CaseFormat; | |
import java.lang.reflect.RecordComponent; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class PacketCodeGenerator { | |
public record PacketList(List<PacketType> packets, List<PacketType> types) { | |
} | |
public record PacketField(String name, String type) { | |
} | |
public record PacketType(String name, List<PacketField> fields, Map<String, Object> values) { | |
} | |
public static PacketList convertType(Class<?> clazz, boolean isPacket) { | |
PacketList packetList = new PacketList(new ArrayList<>(), new ArrayList<>()); | |
String typeName = clazz.getTypeName() | |
.replace(clazz.getPackageName() + ".", "") | |
.replace("Packet", "") | |
.replace("$", "_"); | |
CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, typeName); | |
List<PacketField> fields = new ArrayList<>(); | |
if (clazz.isRecord()) { | |
for (RecordComponent recordComponent : clazz.getRecordComponents()) { | |
String fieldName = recordComponent.getName(); | |
Class<?> fieldClazz = recordComponent.getType(); | |
fields.add(new PacketField(fieldName, | |
CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, | |
fieldClazz.getTypeName() | |
.replace(clazz.getPackageName() + ".", "") | |
.replace("Packet", "") | |
.replace("$", "") | |
) | |
)); | |
if (!fieldClazz.isPrimitive()) { | |
var pkls = convertType(fieldClazz, false); | |
packetList.types.addAll(pkls.types); | |
} | |
} | |
var packetType = new PacketType(typeName, fields, new HashMap<>()); | |
if (isPacket) | |
packetList.packets.add(packetType); | |
else | |
packetList.types.add(packetType); | |
} else if (clazz.isEnum()) { | |
Map<String, Object> values = new HashMap<>(); | |
for (var constant : clazz.getEnumConstants()) { | |
String constantTypeName = CaseFormat.UPPER_CAMEL.to( | |
CaseFormat.LOWER_UNDERSCORE, constant.getClass().getTypeName() | |
.replace(constant.getClass().getPackageName() + ".", "") | |
.replace("Packet", "") | |
.replace("$", "_") | |
); | |
values.put(constantTypeName, constant); | |
} | |
packetList.types.add(new PacketType(typeName, fields, values)); | |
} | |
return packetList; | |
} | |
public static PacketList convertUnknownType(Class<?> clazz) { | |
return convertType(clazz, !clazz.getTypeName().endsWith("$") && clazz.getPackageName().startsWith("net.minecraft.network.packet")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment