Skip to content

Commit 8d6db34

Browse files
committed
Fix EntityRegistry system being broken.
1 parent df4a839 commit 8d6db34

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
lines changed

loader/src/main/java/com/fox2code/foxloader/registry/EntityRegistry.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public final class EntityRegistry {
3636
public static final int INITIAL_ENTITY_ID = 256;
3737
private static final int MAX_ENTITY_ID = 1024;
3838
private static final int[] networkMappingIn = new int[MAX_ENTITY_ID];
39-
private static final HashSet<String> registeredEntities = new HashSet<>();
39+
private static final Set<String> registryEntitiesTypeNamesIdsVanilla = EntityList.getEntityTypeNames();
40+
private static final HashSet<String> registeredEntities = new HashSet<>(registryEntitiesTypeNamesIdsVanilla);
4041
private static final Set<String> registryEntitiesTypeNamesIdsRaw = Collections.unmodifiableSet(registeredEntities);
4142
private static Set<String> registryEntitiesTypeNamesIds = registryEntitiesTypeNamesIdsRaw;
4243
static final LinkedHashMap<String, RegistryEntry> entityEntries = new LinkedHashMap<>();
@@ -52,6 +53,13 @@ public static Entity createEntityRemote(int id, World world) {
5253
return EntityList.createEntity(networkMappingIn[id], world);
5354
}
5455

56+
public static Set<String> getCommandCompletionEntityIDs() {
57+
if (registryEntitiesTypeNamesIds == null || registryEntitiesTypeNamesIds.isEmpty()) {
58+
throw new IllegalStateException("Not initialized yet!");
59+
}
60+
return registryEntitiesTypeNamesIds;
61+
}
62+
5563
public static final class Internal {
5664
static int nextEntityId = INITIAL_ENTITY_ID;
5765

@@ -96,7 +104,7 @@ static void resetEntityMappings(boolean asLocalMapping) {
96104
networkMappingIn[i] = i;
97105
}
98106
} else {
99-
registryEntitiesTypeNamesIds = Collections.emptySet();
107+
registryEntitiesTypeNamesIds = registryEntitiesTypeNamesIdsVanilla;
100108
Arrays.fill(networkMappingIn, -1);
101109
for (int i = 0; i < INITIAL_ENTITY_ID; i++) {
102110
networkMappingIn[i] = i;
@@ -106,8 +114,9 @@ static void resetEntityMappings(boolean asLocalMapping) {
106114

107115
static ServerRegistry initializeEntityMappingsEx(ServerRegistry serverRegistry, boolean localWorld) {
108116
if (!localWorld) {
109-
registryEntitiesTypeNamesIds =
110-
Collections.unmodifiableSet(serverRegistry.registryEntries.keySet());
117+
HashSet<String> hashSet = new HashSet<>(registryEntitiesTypeNamesIdsVanilla);
118+
hashSet.addAll(serverRegistry.registryEntries.keySet());
119+
registryEntitiesTypeNamesIds = Collections.unmodifiableSet(hashSet);
111120
}
112121
LinkedHashMap<String, RegistryEntry> toInjectEntityEntries =
113122
localWorld ? new LinkedHashMap<>(serverRegistry.entityEntries) : null;

loader/src/main/java/com/fox2code/foxloader/registry/GameRegistry.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ public static void initialize() {
264264
if (realId < INITIAL_ENTITY_ID)
265265
throw new IOException("Invalid non-loader id: " + realId);
266266
EntityRegistry.entityEntries.put(registryEntry.name, registryEntry);
267-
registryEntriesItemIds[registryEntry.realId - INITIAL_TRANSLATED_BLOCK_ID] = registryEntry;
268267
initialEntityID = Math.max(initialEntityID, realId);
269268
}
270269
// Allow to load new game content without conflicting with item ids

patching/src/main/java/com/fox2code/foxloader/patching/game/CommandGamePatch.java

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package com.fox2code.foxloader.patching.game;
2525

2626
import com.fox2code.foxloader.patching.TransformerUtils;
27+
import org.objectweb.asm.Opcodes;
2728
import org.objectweb.asm.tree.*;
2829

2930
final class CommandGamePatch extends GamePatch {
@@ -32,28 +33,42 @@ final class CommandGamePatch extends GamePatch {
3233
private static final String ServerPlayerCommandHandler = "net/minecraft/server/command/ServerPlayerCommandHandler";
3334
private static final String CommandRegistry$Internal = "com/fox2code/foxloader/registry/CommandRegistry$Internal";
3435
private static final String GameRegistry = "com/fox2code/foxloader/registry/GameRegistry";
36+
private static final String EntityRegistry = "com/fox2code/foxloader/registry/EntityRegistry";
3537
private static final String CommandCompletionRegistry$Item =
3638
"net/minecraft/common/command/completion/CommandCompletionRegistry$Type$1";
39+
private static final String CommandCompletionRegistry$Entity =
40+
"net/minecraft/common/command/completion/CommandCompletionRegistry$Type$2";
3741

3842
CommandGamePatch() {
3943
super(new String[]{PlayerCommandHandler, ClientPlayerCommandHandler, ServerPlayerCommandHandler,
40-
CommandCompletionRegistry$Item});
44+
CommandCompletionRegistry$Item, CommandCompletionRegistry$Entity});
4145
}
4246

4347
@Override
4448
public ClassNode transform(ClassNode classNode) {
45-
if (ClientPlayerCommandHandler.equals(classNode.name) ||
46-
ServerPlayerCommandHandler.equals(classNode.name)) {
47-
MethodNode init = TransformerUtils.getMethod(classNode, "<init>");
48-
TransformerUtils.insertToEndOfCode(init,
49-
new MethodInsnNode(INVOKESTATIC, CommandRegistry$Internal, "register", "()V", false));
50-
TransformerUtils.bringSelfCallToEndOfCode(init, "reloadCommandCompletions");
51-
} else if (PlayerCommandHandler.equals(classNode.name)) {
52-
MethodNode init = TransformerUtils.getMethod(classNode, "reloadCommands");
53-
TransformerUtils.insertToEndOfCode(init,
54-
new MethodInsnNode(INVOKESTATIC, CommandRegistry$Internal, "register", "()V", false));
55-
} else if (CommandCompletionRegistry$Item.equals(classNode.name)) {
56-
patchCommandCompletionRegistry$Item(classNode);
49+
switch (classNode.name) {
50+
case ClientPlayerCommandHandler:
51+
case ServerPlayerCommandHandler: {
52+
MethodNode init = TransformerUtils.getMethod(classNode, "<init>");
53+
TransformerUtils.insertToEndOfCode(init,
54+
new MethodInsnNode(INVOKESTATIC, CommandRegistry$Internal, "register", "()V", false));
55+
TransformerUtils.bringSelfCallToEndOfCode(init, "reloadCommandCompletions");
56+
break;
57+
}
58+
case PlayerCommandHandler: {
59+
MethodNode init = TransformerUtils.getMethod(classNode, "reloadCommands");
60+
TransformerUtils.insertToEndOfCode(init,
61+
new MethodInsnNode(INVOKESTATIC, CommandRegistry$Internal, "register", "()V", false));
62+
break;
63+
}
64+
case CommandCompletionRegistry$Item: {
65+
patchCommandCompletionRegistry$Item(classNode);
66+
break;
67+
}
68+
case CommandCompletionRegistry$Entity: {
69+
patchCommandCompletionRegistry$Entity(classNode);
70+
break;
71+
}
5772
}
5873
return classNode;
5974
}
@@ -112,4 +127,17 @@ public ClassNode transform(ClassNode classNode) {
112127
classNode.methods.remove(complete);
113128
classNode.methods.add(newComplete);
114129
}
130+
131+
private static void patchCommandCompletionRegistry$Entity(ClassNode classNode) {
132+
MethodNode complete = TransformerUtils.getMethod(classNode, "complete");
133+
for (AbstractInsnNode abstractInsnNode : complete.instructions) {
134+
if (abstractInsnNode.getOpcode() == Opcodes.INVOKESTATIC) {
135+
MethodInsnNode methodInsnNode = (MethodInsnNode) abstractInsnNode;
136+
if ("getEntityTypeNames".equals(methodInsnNode.name)) {
137+
methodInsnNode.owner = EntityRegistry;
138+
methodInsnNode.name = "getCommandCompletionEntityIDs";
139+
}
140+
}
141+
}
142+
}
115143
}

0 commit comments

Comments
 (0)