Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 0186171

Browse files
committed
Features needed to get IronChests ingame
1 parent 53f8dfb commit 0186171

File tree

6 files changed

+54
-29
lines changed

6 files changed

+54
-29
lines changed

patchwork-extensions/src/main/java/net/patchworkmc/impl/extensions/asm/InstanceFinder.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.objectweb.asm.tree.AbstractInsnNode;
2626
import org.objectweb.asm.tree.MethodInsnNode;
27+
import org.objectweb.asm.tree.MethodNode;
2728
import org.objectweb.asm.tree.analysis.Analyzer;
2829
import org.objectweb.asm.tree.analysis.AnalyzerException;
2930
import org.objectweb.asm.tree.analysis.Frame;
@@ -46,13 +47,22 @@ public static void classLoadMePlease() {
4647

4748
private class InternalFrame extends Frame<SourceValue> {
4849
private AbstractInsnNode current;
50+
boolean capturing = false;
51+
SourceValue last = null;
52+
4953
InternalFrame(int numLocals, int numStack) {
5054
super(numLocals, numStack);
5155
}
5256

5357
@Override
5458
public void execute(AbstractInsnNode insn, Interpreter<SourceValue> interpreter) throws AnalyzerException {
5559
this.current = insn;
60+
61+
// The instance is pushed first and therefore popped last
62+
if (capturing && current != target) {
63+
throw new FoundException(last.insns);
64+
}
65+
5666
super.execute(insn, interpreter);
5767
}
5868

@@ -63,11 +73,14 @@ public void push(SourceValue value) {
6373

6474
@Override
6575
public SourceValue pop() {
76+
SourceValue ret = super.pop();
77+
6678
if (current == target) {
67-
throw new FoundException(super.pop().insns);
79+
capturing = true;
80+
this.last = ret;
6881
}
6982

70-
return super.pop();
83+
return ret;
7184
}
7285
}
7386

@@ -76,6 +89,20 @@ protected Frame<SourceValue> newFrame(int numLocals, int numStack) {
7689
return new InternalFrame(numLocals, numStack);
7790
}
7891

92+
@Override
93+
public Frame<SourceValue>[] analyze(String owner, MethodNode method) throws AnalyzerException {
94+
Frame<SourceValue>[] frames = super.analyze(owner, method);
95+
InternalFrame ours = (InternalFrame) frames[0];
96+
97+
// Handle the case where the MIN is the last of the whole method
98+
// I don't actually know if this could ever even happen, but better safe than sorry
99+
if (ours.capturing) {
100+
throw new FoundException(ours.last.insns);
101+
}
102+
103+
return frames;
104+
}
105+
79106
class FoundException extends RuntimeException {
80107
public final Set<AbstractInsnNode> insns;
81108

patchwork-extensions/src/main/java/net/patchworkmc/impl/extensions/asm/TileEntityHacks.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
*/
3333
class TileEntityHacks {
3434
private static final String BLOCK_ENTITY_PROVIDER = Transformer.mapSlashedClass("net/minecraft/block/BlockEntityProvider");
35-
// it's easier to just hardcode this method name that will never change since we don't need the desc
36-
private static final String CREATE_BE =
37-
FabricLoader.getInstance().isDevelopmentEnvironment() ? "createBlockEntity" : "method_10123";
35+
// it's easier to just hardcode these method names that will never change since we don't need the desc
36+
private static final String CREATE_BE = FabricLoader.getInstance().isDevelopmentEnvironment() ? "createBlockEntity" : "method_10123";
37+
private static final String HAS_BLOCK_ENTITY = FabricLoader.getInstance().isDevelopmentEnvironment() ? "hasBlockEntity" : "method_9570";
3838
private static final String BLOCK = Transformer.mapSlashedClass("net/minecraft/class_2248");
3939

4040
public static void classLoadMePlease() {
@@ -45,12 +45,18 @@ public static boolean transform(MethodNode node) {
4545
boolean didSomething = false;
4646

4747
for (AbstractInsnNode instruction : node.instructions) {
48-
if (instruction.getOpcode() == Opcodes.CHECKCAST) {
48+
if (instruction instanceof TypeInsnNode) {
4949
TypeInsnNode tin = (TypeInsnNode) instruction;
5050

5151
if (tin.desc.equals(BLOCK_ENTITY_PROVIDER)) {
52-
// this could be problematic for weird use cases. too bad!
53-
node.instructions.remove(tin);
52+
if (tin.getOpcode() == Opcodes.CHECKCAST) {
53+
// this could be problematic for weird use cases. too bad!
54+
node.instructions.remove(tin);
55+
} else if (tin.getOpcode() == Opcodes.INSTANCEOF) {
56+
MethodInsnNode newMin = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, BLOCK, HAS_BLOCK_ENTITY, "()Z");
57+
node.instructions.insertBefore(tin, newMin);
58+
node.instructions.remove(tin);
59+
}
5460
}
5561
} else if (instruction.getOpcode() == Opcodes.INVOKEINTERFACE) {
5662
MethodInsnNode min = (MethodInsnNode) instruction;

patchwork-extensions/src/main/java/net/patchworkmc/impl/extensions/asm/Transformer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
*/
5151
class Transformer {
5252
private static final FabricLoader LOADER = FabricLoader.getInstance();
53-
private static final boolean CHANGE_ME_TO_AUDIT = false;
53+
private static final boolean CHANGE_ME_TO_AUDIT = true;
5454
private static final boolean AUDIT = FabricLoader.getInstance().isDevelopmentEnvironment() && CHANGE_ME_TO_AUDIT;
5555

5656
public static final Logger LOGGER = LogManager.getLogger("Patchwork Mass ASM");
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,39 @@
2121

2222
import java.util.Map;
2323
import java.util.concurrent.ConcurrentHashMap;
24+
import java.util.function.Function;
2425

2526
import net.minecraft.block.entity.BlockEntity;
27+
import net.minecraft.block.entity.BlockEntityType;
2628
import net.minecraft.client.options.KeyBinding;
27-
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
2829
import net.minecraft.entity.Entity;
2930
import net.minecraft.util.Identifier;
3031

31-
import net.fabricmc.fabric.api.client.keybinding.KeyBindingRegistry;
32-
import net.fabricmc.fabric.api.client.render.BlockEntityRendererRegistry;
32+
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
33+
import net.fabricmc.fabric.api.client.rendereregistry.v1.BlockEntityRendererRegistry;
3334

3435
import net.patchworkmc.impl.registries.AddedKeybinds;
3536

3637
public class ClientRegistry {
37-
private static Map<Class<? extends Entity>, Identifier> entityShaderMap = new ConcurrentHashMap<>();
38+
private static final Map<Class<? extends Entity>, Identifier> entityShaderMap = new ConcurrentHashMap<>();
3839

3940
/**
40-
* Registers a {@link BlockEntityRenderer}
41+
* Registers a Tile Entity renderer.
4142
* Call this during {@link net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent}.
4243
* This method is safe to call during parallel mod loading.
4344
*/
44-
public static synchronized <T extends BlockEntity> void bindTileEntitySpecialRenderer(Class<T> tileEntityClass, BlockEntityRenderer<? super T> specialRenderer) {
45-
BlockEntityRendererRegistry.INSTANCE.register(tileEntityClass, specialRenderer);
46-
// Done by Fabric API: specialRenderer.setRenderManager(BlockEntityRenderDispatcher.INSTANCE);
45+
public static synchronized <T extends BlockEntity> void bindTileEntityRenderer(BlockEntityType<T> tileEntityType,
46+
Function rendererFactory) {
47+
BlockEntityRendererRegistry.INSTANCE.register(tileEntityType, rendererFactory);
4748
}
4849

4950
/**
50-
* Registers a {@link KeyBinding}.
51+
* Registers a KeyBinding.
5152
* Call this during {@link net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent}.
5253
* This method is safe to call during parallel mod loading.
5354
*/
5455
public static synchronized void registerKeyBinding(KeyBinding key) {
55-
KeyBindingRegistry.INSTANCE.addCategory(key.getCategory());
56+
KeyBindingHelper.registerKeyBinding(key);
5657
AddedKeybinds.registerKeyBinding(key);
5758
}
5859

patchwork-registries/src/main/java/net/minecraftforge/registries/DeferredRegister.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public Supplier<IForgeRegistry<T>> makeRegistry(final String name, final Supplie
154154
* @param bus The Mod Specific event bus.
155155
*/
156156
public void register(IEventBus bus) {
157+
EventRegistrarRegistry.INSTANCE.registerInstance(EventDispatcher.class, (in, it) -> it.addListener(in::handleEvent));
157158
bus.register(new EventDispatcher(this));
158159

159160
if (this.type == null && this.registryFactory != null) {
@@ -173,11 +174,6 @@ public void handleEvent(RegistryEvent.Register<?> event) {
173174
}
174175
}
175176

176-
// can't use $ because of how the ${version} replacement works
177-
public static void patchwork_registerEventRegistrar() {
178-
EventRegistrarRegistry.INSTANCE.registerInstance(EventDispatcher.class, (in, bus) -> bus.addListener(in::handleEvent));
179-
}
180-
181177
/**
182178
* @return The unmodifiable view of registered entries. Useful for bulk operations on all values.
183179
*/

patchwork-registries/src/main/resources/fabric.mod.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
"mixins": [
2121
"patchwork-registries.mixins.json"
2222
],
23-
"entrypoints": {
24-
"patchwork:commonAutomaticSubscribers": [
25-
"net.minecraftforge.registries.DeferredRegister::patchwork_registerEventRegistrar"
26-
]
27-
},
2823
"accessWidener": "patchwork-registries.accesswidener",
2924
"custom": {
3025
"modmenu:api": true,

0 commit comments

Comments
 (0)