Skip to content

Commit

Permalink
Some basic optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba6000 committed Dec 27, 2024
1 parent abd5250 commit c7b8630
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/mitchej123/hodgepodge/mixins/Mixins.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ public enum Mixins {
TRANSPARENT_CHAT(new Builder("Transparent Chat").setPhase(Phase.EARLY)
.addMixinClasses("minecraft.MixinGuiNewChat_TransparentChat").setSide(Side.CLIENT)
.setApplyIf(() -> TweaksConfig.transparentChat).addTargetedMod(TargetedMod.VANILLA)),
CRAFTING_OPTIMIZATIONS(new Builder("Crafting Optimizations").setPhase(Phase.EARLY)
.addMixinClasses("minecraft.MixinShapedOreRecipe", "minecraft.MixinInventoryCrafting").setSide(Side.BOTH)
.setApplyIf(() -> true).addTargetedMod(TargetedMod.VANILLA)),
// config handled in mixin due to server->client config sync
LONGER_MESSAGES_CLIENT(new Builder("Longer Messages Client Side").setPhase(Phase.EARLY)
.addMixinClasses("minecraft.MixinGuiChat_LongerMessages").setApplyIf(() -> true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.mitchej123.hodgepodge.mixins.early.minecraft;

import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.world.World;
import net.minecraftforge.oredict.ShapedOreRecipe;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

@Mixin(value = ShapedOreRecipe.class)
public class MixinShapedOreRecipe {

@Shadow(remap = false)
private int width;

@Shadow(remap = false)
private int height;

@Shadow(remap = false)
private Object[] input;

@Shadow(remap = false)
private boolean mirrored;

@Unique
private int hodgepodge$activeSize = -1;

/**
* @author kuba6000
* @reason Optimize ShapedOreRecipe
*/
@Overwrite
public boolean matches(InventoryCrafting inv, World world) {
if (hodgepodge$activeSize == -1) {
hodgepodge$activeSize = 0;
for (int i = 0; i < width * height; i++) {
if (input[i] != null) {
hodgepodge$activeSize++;
break;
}
}
}

int invSize = inv.getSizeInventory();
int wsize = (int) Math.sqrt(invSize);

// some basic checks to avoid item comparing
if (height > wsize || width > wsize) {
return false;
}
int invActiveSize = 0;
for (int i = 0; i < invSize; i++) {
if (inv.getStackInSlot(i) != null) {
invActiveSize++;
if (invActiveSize > hodgepodge$activeSize) {
return false;
}
}
}

if (invActiveSize != hodgepodge$activeSize) {
return false;
}

for (int i = 0; i < wsize - width + 1; i++) {
for (int j = 0; j < wsize - height + 1; j++) {
if (checkMatch(inv, i, j, true)) {
return true;
}
if (mirrored && checkMatch(inv, i, j, false)) {
return true;
}
}
}

return false;
}

@Shadow(remap = false)
private boolean checkMatch(InventoryCrafting inv, int startX, int startY, boolean mirror) {
throw new RuntimeException("Mixin failed to apply");
}

}

0 comments on commit c7b8630

Please sign in to comment.