refactor: port microblock recipes to java

This commit is contained in:
Algent committed 2026-08-28 11:07:57 +03:00
1 parent df21e4041c
commit 984d3a2f61
8 files changed
+472 -268

No files matched your search

+21 -8
View File
@@ -24,8 +24,9 @@ No trustworthy tool will produce a maintainable Java port automatically. A decom
## Current status
The binary and source-level consumer audits, low-coupling Phase 3 queue, audit-derived immediate compatibility gate,
focused profile, first traversal optimization, and complete redstone interaction helper unit are complete. Phase 4
continues with `MicroRecipe`'s non-local-return category; see `JAVA_MIGRATION_PROFILE.md`.
focused profile, first traversal optimization, complete redstone interaction helper unit, and `MicroRecipe` port are
complete. The remaining measured hot paths are generated tile traits, so the next work starts the Phase 5 Java-trait
pilot; see `JAVA_MIGRATION_PROFILE.md`.
That audit found and this branch has now repaired one existing regression: Schematica 1.12.6 reflects the original private
`MultiPartRegistry$` field `codechicken$multipart$MultiPartRegistry$$typeMap` and casts it to a Scala mutable map. The
@@ -128,7 +129,8 @@ The source and bytecode audit identified allocation patterns that are more plaus
- `TRedstoneTile` power and connection queries create closures and `scala.runtime.IntRef` boxes in frequently queried
code.
- Several Scala 2.11 collection, range, and `JavaConversions` paths create closure objects, boxed primitives, iterators, or temporary collections.
- `TSlottedTile.canAddPart` and several `MicroRecipe` searches use exception-based non-local returns.
- `TSlottedTile.canAddPart` still uses an exception-based non-local return. `MicroRecipe`'s equivalent scans are now
ordinary Java loops.
- Rendering code in `TileMultipart` already uses cached arrays and `while` loops in important paths. It should be preserved as evidence of deliberate optimization, not mechanically rewritten into streams or higher-level collection pipelines.
The focused Forge/JFR baseline confirms both risks. With eight parts, `updateEntity` and `operate` allocate about 184
@@ -325,15 +327,18 @@ and the already-ported registry again supports Schematica.
- [x] Characterize result ordering, callback timing, early exits, and edge cases before changing traversal code.
- [x] Rewrite `TileMultipart` update/operate traversal without per-call collection allocation.
- [ ] Rewrite redstone queries without closure allocation or `IntRef` boxing.
- [ ] Replace non-local returns in slot/recipe scans with ordinary Java control flow.
- [ ] Use fastutil only where profiling and data shape justify it.
- [x] Replace non-local returns in recipe scans with ordinary Java control flow.
- [ ] Replace the remaining non-local return in the generated `TSlottedTile` slot scan.
- [x] Keep fastutil out of these paths because profiling did not justify another dependency or data structure.
- [x] Re-profile the same scenarios and record both improvements and regressions.
Status: active. `operate` now preserves its captured-sequence mutation semantics without collection materialization,
and the six-interface `IRedstonePart`/`RedstoneInteractions$` unit is ported with its class list and descriptors intact.
The comparison proves its allocation is owned by `scalatraits/TRedstoneTile.scala`, so that part waits for Phase 5.
Next characterize and port `MicroRecipe.scala`, replacing its closure-backed scans and non-local returns with ordinary
Java loops.
`MicroRecipe` is also Java: all five recipe forms and precedence are characterized, its published Scala façade remains,
and its closure-backed scans and non-local returns are gone. The remaining two hot-path items are generated traits and
will be handled through Phase 5. Start with `TPartialOcclusionTile` as the simple no-field Java-trait pilot before the
stateful slot and redstone traits.
Exit condition: identified steady-state Scala allocation sites are removed with compatible results.
@@ -402,7 +407,7 @@ The work should move from low to high risk while keeping a buildable mixed-langu
Extensive tests are both possible and recommended here. Their purpose is not to prove that the current Scala behavior is ideal; it is to make its observable behavior explicit before translation, so every later difference is intentional.
The repository now has a Java-8-compatible JUnit Jupiter suite and a small Forge integration runner. The current
baseline is 139 plain-JVM tests and 31 Forge server tests. Minecraft 1.7.10 does not provide the modern GameTest
baseline is 141 plain-JVM tests and 37 Forge server tests. Minecraft 1.7.10 does not provide the modern GameTest
framework, so game-dependent coverage continues through the existing narrow Forge harness rather than a second test
framework.
@@ -657,3 +662,11 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec
the Phase 5 generated-trait conversion, not this helper unit.
- Chose `MicroRecipe.scala` as the next independent Phase 4 unit because its nested collection scans compile early
exits into `NonLocalReturnControl`; characterize all five recipe paths and precedence before porting it.
- Characterized `MicroRecipe`'s complete 17-method façade/companion ABI, Scala `splitMap`, all five recipe forms,
class-specific gluing rules, saw ordering, exact material/tag lookup, and hollow-over-gluing precedence against the
original Scala implementation.
- Ported the recipe singleton to Java with ordinary loops. The published `MicroRecipe` static façade,
`MicroRecipe$.MODULE$`, `scala.Tuple3` return and immutable Scala map descriptors remain; the 12 private Scala
closure classes and their `NonLocalReturnControl` exits are gone.
- The recipe port passes all 141 plain-JVM and 37 Forge tests. The next step is the Phase 5
`TPartialOcclusionTile` Java-trait pilot, whose generated override dispatch is already covered by the Forge harness.
+50
View File
@@ -1311,3 +1311,53 @@ gain. Removing that allocation requires the later `registerJavaTrait` conversion
actual world routing, and a generated redstone tile.
- Clean `spotlessApply checkstyleTest build`: passing.
- Class list and public members diffed against the pre-port build; no characterization assertion changed.
## 2026-08-28 — MicroRecipe Java port
The complete recipe singleton moved together. Extra Utilities consumes this area through NEI recipe enumeration, and
the audited binary corpus calls `create`, `findMaterial`, and `getCraftingResult`; the port preserves the larger
published surface as well.
### Observable behavior
No known recipe divergence. Hollow construction, gluing, vertical thinning, horizontal splitting, and hollow filling
retain their exact slot shapes and early rejection rules. A ring of eight size-one covers is valid both as a hollow
recipe and as gluing back to a full block; `getCraftingResult` still chooses the hollow result first. Gluing retains
the separate cover/hollow, edge, and corner rules, and splitting keeps the `0 -> 3`, `1 -> 3`, `3 -> 2` map.
Saw discovery remains row-major. Cutting still accepts either sufficient strength or exactly the registry's maximum
saw strength. Full-size results still copy the material's source stack, while smaller results retain the material name
in an `ItemMicroPart`. `findMaterial` still returns the first exact item, damage, and NBT match. This means the missing
material placeholder's vanilla-stone stack can shadow a later stone entry; the Forge tests deliberately use glass when
they need an unambiguous raw-material round trip.
Java uses `Objects.equals` where Scala used null-safe `==`. Internal thinning and splitting locate the saw by slot
number to avoid constructing the published tuple for private work; direct `getSaw` calls still return the same boxed
`scala.Tuple3<Saw, Object, Object>`.
### Supported JVM API
`MicroRecipe` retains all 17 public static methods with their exact descriptors. `MicroRecipe$` still implements
`IRecipe`, exposes `MODULE$`, carries the implementation methods, and publishes the same
`scala.collection.immutable.Map<Object, Object>` by identity. The Java façade necessarily has a private constructor,
as with the other converted Scala forwarder classes.
The 12 private `MicroRecipe$$anonfun$...` compiler classes disappear; no consumer references them. The in-repo Scala
proxy now registers `MicroRecipe$.MODULE$` explicitly because deleting the Scala object removes the compiler symbol
that allowed `MicroRecipe` to be used as a value. Existing bytecode remains compatible through the retained companion.
### Performance boundary
Ordinary loops remove the range, filter, collection-search, captured-ref, and `NonLocalReturnControl` machinery from
recipe matching. No recipe workload was added to the focused tick/redstone profiler, so this is a structural result,
not a numeric performance claim. The public `getSaw` tuple allocation remains compatibility-required; internal recipe
paths no longer pay for it.
### Validation
- `MicroRecipeBinaryCompatibilityTest`: 2 tests covering the complete façade/companion ABI, `MODULE$`, and split map.
- `MicroRecipeFunctionalTest`: 6 Forge tests covering all recipe forms, precedence, class mappings, saw order,
material lookup, NBT sensitivity, metadata, creation, and rejection paths.
- Complete plain-JVM suite: 141 tests, 0 failures, 0 errors.
- Java 8 Forge dedicated-server suite: 37 tests, 0 failures, 0 errors.
- Class list and public members compared with the pre-port build; no characterization assertion changed.
+13 -10
View File
@@ -12,7 +12,7 @@ what breaks, and what is left. The other documents hold the reasoning.
| `JAVA_MIGRATION_MANUAL_CHECKS.md` | What no automated test can cover, and must be checked by hand in a client |
| `JAVA_MIGRATION_PROFILE.md` | The focused baseline, first measured result, findings, and exact rerun command |
Branch: `algent/java`. Base: `master`. 66 commits including the redstone interaction unit.
Branch: `algent/java`. Base: `master`. 68 commits including the `MicroRecipe` unit.
## The one rule that matters
@@ -66,7 +66,7 @@ awk -F'"' '/<testsuite /{t+=$4;f+=$8;e+=$10} END{print "tests="t" failures="f" e
grep -o 'tests="[0-9]*" skipped="[0-9]*" failures="[0-9]*" errors="[0-9]*"' run/server/junit-out/TEST-*.xml
```
Current baseline: **139 plain-JVM tests, 31 Forge server tests, all passing at their last completed runs.**
Current baseline: **141 plain-JVM tests, 37 Forge server tests, all passing at their last completed runs.**
### ABI diff against the reference
@@ -188,15 +188,15 @@ All eight load-bearing `$class` helpers from the inventory, both registries, and
`IDWriter`, `PartialOcclusionTest`/`JPartialOcclusion`, `TCuboidPart`/`JCuboidPart`, the `TNormalOcclusion` unit,
`TFacePart`, the `TIconHitEffects` unit, `TItemMultiPart`/`JItemMultiPart`, `TEdgePart`, `Saw`, `MicroMaterialRegistry`,
`MultiPartRegistry`, `TileMultipart`, `TMultiPart`, `TickScheduler`, `BlockMultipart`, and the complete
`IRedstonePart`/`RedstoneInteractions` unit.
`MultiPartRegistry`, `TileMultipart`, `TMultiPart`, `TickScheduler`, `BlockMultipart`, the complete
`IRedstonePart`/`RedstoneInteractions` unit, and `MicroRecipe`.
Plus the six marker interfaces: `TSlottedPart`, `IRandomDisplayTick`, `INeighborTileChange`, `TRandomUpdateTick`,
`ISidedHollowConnect`, `IMicroMaterialRender`, plus `MultipartHelper`, `TileCache`, `PacketScheduler` and the `ControlKeyModifer` pair.
Both `package.scala` objects are gone, removed rather than ported. `MultipartRenderer` is done.
80 Java files, 47 Scala files, ~6,103 Scala lines left (non-blank; that is the metric this figure has always used).
82 Java files, 46 Scala files, ~5,884 Scala lines left (non-blank; that is the metric this figure has always used).
## What is left, and in what order
@@ -233,17 +233,20 @@ implementation singleton. Pure masks, vanilla special cases, routing precedence,
redstone-tile selection are characterized. The comparison correctly found no allocation reduction: the measured
closures and `IntRef` boxes belong to `scalatraits/TRedstoneTile.scala`, which requires the Phase 5 Java-trait path.
Next characterize and port `MicroRecipe.scala`. It is an independent medium-risk unit and contains the recipe/slot
scan non-local returns still open in Phase 4; freeze all five recipe forms and their precedence before replacing them
with ordinary Java loops.
`MicroRecipe` is now Java. The complete 17-method static/companion surface, immutable Scala split map, exact material
and tag matching, all five recipe forms, class-specific gluing, saw position, and hollow-over-gluing precedence are
frozen. Its internal scans are ordinary loops, and only the published `getSaw` call still constructs its required
`scala.Tuple3`. The in-repo Scala registration now names `MicroRecipe$.MODULE$` explicitly.
**Medium.** The `handler` packages on both sides, `MicroRecipe`, `ItemMicroPart`,
**Medium.** The `handler` packages on both sides, `ItemMicroPart`,
`MicroblockPlacement`, `PlacementGrids`, `BlockMicroMaterial`, `MissingMicroMaterial`, `ConfigContent`,
`DefaultContent`.
**Phase 5, needs the `registerJavaTrait` path.** `TileMultipartClient` and everything in `multipart/scalatraits/`.
These are registered with the ASM generator; converting them is a different mechanism with documented restrictions.
Do the pilot deliberately, as its own piece of work.
Next characterize and port `TPartialOcclusionTile.scala` as the pilot. It has no fields, already takes the Java-trait
rewrite path because it is a concrete class, and the Forge harness already verifies generated override dispatch. Once
that proves direct Java source through `registerJavaTrait`, move to the stateful `TSlottedTile` pilot coverage.
**Phase 6/7, last.** `Microblock` and the microblock shape hierarchy, `MicroblockGenerator`, `MultipartGenerator`, and
all of `multipart/asm/`. The ASM subsystem should be last; freeze generated-class fixtures before touching it.
+7 -2
View File
@@ -118,5 +118,10 @@ That evidence corrects the earlier plan. `IRedstonePart.scala` owned the public
measured `IntRef`, iterator, and closure allocations are emitted by `scalatraits/TRedstoneTile.scala`. Removing them
requires the Phase 5 `registerJavaTrait` path and must not be smuggled into this otherwise descriptor-identical port.
The next independent Phase 4 unit is `MicroRecipe.scala`: characterize every recipe shape and precedence rule, then
replace its closure-backed scans and non-local returns with ordinary Java loops.
`MicroRecipe.scala` was the next independent Phase 4 unit and is now Java. Its five recipe forms and precedence are
characterized, and ordinary loops replaced its range/closure scans and exception-backed non-local returns. The focused
server workload does not craft recipes, so no timing claim is made for that structural removal.
The remaining measured redstone allocation and slot-scan non-local return live in generated traits. Start Phase 5 with
the simpler `TPartialOcclusionTile` Java-source pilot, then use the proven path for `TSlottedTile` and
`TRedstoneTile`; re-run this workload when the latter changes.
@@ -0,0 +1,298 @@
package codechicken.microblock;
import java.util.Objects;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.world.World;
import net.minecraftforge.oredict.RecipeSorter;
import codechicken.microblock.MicroMaterialRegistry.IMicroMaterial;
import codechicken.microblock.handler.MicroblockProxy;
import scala.Tuple2;
import scala.Tuple3;
import scala.collection.immutable.Map;
import scala.collection.immutable.Map$;
/** Scala-compatible singleton carrying the microblock recipe implementation. */
public final class MicroRecipe$ implements IRecipe {
public static final MicroRecipe$ MODULE$ = new MicroRecipe$();
private final Map<Object, Object> splitMap;
private MicroRecipe$() {
RecipeSorter.register("fmp:micro", getClass(), RecipeSorter.Category.SHAPED, "after:forge:shapelessore");
Map<Object, Object> splits = Map$.MODULE$.empty();
splits = splits.updated(0, 3);
splits = splits.updated(1, 3);
splitMap = splits.updated(3, 2);
}
@Override
public ItemStack getRecipeOutput() {
return ItemMicroPart.create(1, 1, "tile.stone");
}
@Override
public int getRecipeSize() {
return 9;
}
@Override
public boolean matches(InventoryCrafting crafting, World world) {
return getCraftingResult(crafting) != null;
}
@Override
public ItemStack getCraftingResult(InventoryCrafting crafting) {
ItemStack result = getHollowResult(crafting);
if (result != null) {
return result;
}
result = getGluingResult(crafting);
if (result != null) {
return result;
}
result = getThinningResult(crafting);
if (result != null) {
return result;
}
result = getSplittingResult(crafting);
return result != null ? result : getHollowFillResult(crafting);
}
public ItemStack create(int amount, int microClass, int size, int material) {
if (size == 8) {
ItemStack item = MicroMaterialRegistry.getMaterial(material).getItem().copy();
item.stackSize = amount;
return item;
}
return ItemMicroPart.create(amount, microClass << 8 | size, MicroMaterialRegistry.materialName(material));
}
public int microMaterial(ItemStack item) {
return Objects.equals(item.getItem(), MicroblockProxy.itemMicro()) ? ItemMicroPart.getMaterialID(item)
: findMaterial(item);
}
public int microClass(ItemStack item) {
return Objects.equals(item.getItem(), MicroblockProxy.itemMicro()) ? item.getItemDamage() >> 8 : 0;
}
public int microSize(ItemStack item) {
return Objects.equals(item.getItem(), MicroblockProxy.itemMicro()) ? item.getItemDamage() & 0xFF : 8;
}
public ItemStack getHollowResult(InventoryCrafting crafting) {
if (crafting.getStackInRowAndColumn(1, 1) != null) {
return null;
}
ItemStack first = crafting.getStackInRowAndColumn(0, 0);
if (first == null || !Objects.equals(first.getItem(), MicroblockProxy.itemMicro()) || microClass(first) != 0) {
return null;
}
int size = microSize(first);
int material = microMaterial(first);
for (int slot = 1; slot <= 8; slot++) {
if (slot == 4) {
continue;
}
ItemStack item = crafting.getStackInSlot(slot);
if (item == null || !Objects.equals(item.getItem(), MicroblockProxy.itemMicro())
|| microMaterial(item) != material
|| item.getItemDamage() != first.getItemDamage()) {
return null;
}
}
return create(8, 1, size, material);
}
public ItemStack getGluingResult(InventoryCrafting crafting) {
int size = 0;
int count = 0;
int smallest = 0;
int microClass = 0;
int material = 0;
for (int slot = 0; slot < 9; slot++) {
ItemStack item = crafting.getStackInSlot(slot);
if (item == null) {
continue;
}
if (!Objects.equals(item.getItem(), MicroblockProxy.itemMicro())) {
return null;
}
if (count == 0) {
size = microSize(item);
microClass = microClass(item);
material = microMaterial(item);
count = 1;
smallest = size;
} else if (microClass(item) != microClass || microMaterial(item) != material) {
return null;
} else if (microClass >= 2 && microSize(item) != smallest) {
return null;
} else {
smallest = Math.min(smallest, microSize(item));
count++;
size += microSize(item);
}
}
if (count <= 1) {
return null;
}
switch (microClass) {
case 3:
return count == 2 ? create(1, 0, smallest, material) : null;
case 2:
if (count == 2) {
return create(1, 3, smallest, material);
}
return count == 4 ? create(1, 0, smallest, material) : null;
case 1:
case 0:
int base = 0;
for (int candidate = 1; candidate <= 4; candidate <<= 1) {
if ((candidate & size) != 0) {
base = candidate;
break;
}
}
if (base == 0) {
return create(size / 8, 0, 8, material);
}
return base <= smallest ? null : create(size / base, microClass, base, material);
default:
return null;
}
}
public Tuple3<Saw, Object, Object> getSaw(InventoryCrafting crafting) {
int slot = findSawSlot(crafting);
if (slot < 0) {
return new Tuple3<>(null, 0, 0);
}
ItemStack item = crafting.getStackInRowAndColumn(slot % 3, slot / 3);
return new Tuple3<>((Saw) item.getItem(), slot / 3, slot % 3);
}
public boolean canCut(Saw saw, ItemStack sawItem, int material) {
int sawStrength = saw.getCuttingStrength(sawItem);
int materialStrength = MicroMaterialRegistry.getMaterial(material).getCutterStrength();
return sawStrength >= materialStrength || sawStrength == MicroMaterialRegistry.getMaxCuttingStrength();
}
public ItemStack getThinningResult(InventoryCrafting crafting) {
int sawSlot = findSawSlot(crafting);
if (sawSlot < 0) {
return null;
}
int row = sawSlot / 3;
int column = sawSlot % 3;
ItemStack sawItem = crafting.getStackInRowAndColumn(column, row);
Saw saw = (Saw) sawItem.getItem();
ItemStack item = crafting.getStackInRowAndColumn(column, row + 1);
if (item == null) {
return null;
}
int size = microSize(item);
int material = microMaterial(item);
int microClass = microClass(item);
if (size == 1 || material < 0 || !canCut(saw, sawItem, material)) {
return null;
}
for (int checkedRow = 0; checkedRow < 3; checkedRow++) {
for (int checkedColumn = 0; checkedColumn < 3; checkedColumn++) {
if ((checkedColumn != column || checkedRow != row && checkedRow != row + 1)
&& crafting.getStackInRowAndColumn(checkedColumn, checkedRow) != null) {
return null;
}
}
}
return create(2, microClass, size / 2, material);
}
public int findMaterial(ItemStack item) {
for (Tuple2<String, IMicroMaterial> entry : MicroMaterialRegistry.getIdMap()) {
ItemStack materialItem = entry._2().getItem();
if (Objects.equals(item.getItem(), materialItem.getItem())
&& item.getItemDamage() == materialItem.getItemDamage()
&& ItemStack.areItemStackTagsEqual(item, materialItem)) {
return MicroMaterialRegistry.materialID(entry._1());
}
}
return -1;
}
public Map<Object, Object> splitMap() {
return splitMap;
}
public ItemStack getSplittingResult(InventoryCrafting crafting) {
int sawSlot = findSawSlot(crafting);
if (sawSlot < 0) {
return null;
}
int row = sawSlot / 3;
int column = sawSlot % 3;
ItemStack sawItem = crafting.getStackInRowAndColumn(column, row);
Saw saw = (Saw) sawItem.getItem();
ItemStack item = crafting.getStackInRowAndColumn(column + 1, row);
if (item == null || !Objects.equals(item.getItem(), MicroblockProxy.itemMicro())) {
return null;
}
int microClass = microClass(item);
int material = microMaterial(item);
if (!canCut(saw, sawItem, material)) {
return null;
}
if (!splitMap.contains(microClass)) {
return null;
}
int splitClass = ((Number) splitMap.apply(microClass)).intValue();
for (int checkedRow = 0; checkedRow < 3; checkedRow++) {
for (int checkedColumn = 0; checkedColumn < 3; checkedColumn++) {
if ((checkedRow != row || checkedColumn != column && checkedColumn != column + 1)
&& crafting.getStackInRowAndColumn(checkedColumn, checkedRow) != null) {
return null;
}
}
}
return create(2, splitClass, microSize(item), material);
}
public ItemStack getHollowFillResult(InventoryCrafting crafting) {
ItemStack cover = null;
for (int slot = 0; slot < 9; slot++) {
ItemStack item = crafting.getStackInSlot(slot);
if (item == null) {
continue;
}
if (!Objects.equals(item.getItem(), MicroblockProxy.itemMicro()) || cover != null
|| microClass(item) != 1) {
return null;
}
cover = item;
}
return cover == null ? null : create(1, 0, microSize(cover), microMaterial(cover));
}
private int findSawSlot(InventoryCrafting crafting) {
for (int slot = 0; slot < 9; slot++) {
ItemStack item = crafting.getStackInRowAndColumn(slot % 3, slot / 3);
if (item != null && item.getItem() instanceof Saw) {
return slot;
}
}
return -1;
}
}
@@ -0,0 +1,82 @@
package codechicken.microblock;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import scala.Tuple3;
import scala.collection.immutable.Map;
/** Static forwarders for Java and existing bytecode; implementation remains on {@link MicroRecipe$}. */
public final class MicroRecipe {
private MicroRecipe() {}
public static ItemStack getRecipeOutput() {
return MicroRecipe$.MODULE$.getRecipeOutput();
}
public static int getRecipeSize() {
return MicroRecipe$.MODULE$.getRecipeSize();
}
public static boolean matches(InventoryCrafting crafting, World world) {
return MicroRecipe$.MODULE$.matches(crafting, world);
}
public static ItemStack getCraftingResult(InventoryCrafting crafting) {
return MicroRecipe$.MODULE$.getCraftingResult(crafting);
}
public static ItemStack create(int amount, int microClass, int size, int material) {
return MicroRecipe$.MODULE$.create(amount, microClass, size, material);
}
public static int microMaterial(ItemStack item) {
return MicroRecipe$.MODULE$.microMaterial(item);
}
public static int microClass(ItemStack item) {
return MicroRecipe$.MODULE$.microClass(item);
}
public static int microSize(ItemStack item) {
return MicroRecipe$.MODULE$.microSize(item);
}
public static ItemStack getHollowResult(InventoryCrafting crafting) {
return MicroRecipe$.MODULE$.getHollowResult(crafting);
}
public static ItemStack getGluingResult(InventoryCrafting crafting) {
return MicroRecipe$.MODULE$.getGluingResult(crafting);
}
public static Tuple3<Saw, Object, Object> getSaw(InventoryCrafting crafting) {
return MicroRecipe$.MODULE$.getSaw(crafting);
}
public static boolean canCut(Saw saw, ItemStack sawItem, int material) {
return MicroRecipe$.MODULE$.canCut(saw, sawItem, material);
}
public static ItemStack getThinningResult(InventoryCrafting crafting) {
return MicroRecipe$.MODULE$.getThinningResult(crafting);
}
public static int findMaterial(ItemStack item) {
return MicroRecipe$.MODULE$.findMaterial(item);
}
public static Map<Object, Object> splitMap() {
return MicroRecipe$.MODULE$.splitMap();
}
public static ItemStack getSplittingResult(InventoryCrafting crafting) {
return MicroRecipe$.MODULE$.getSplittingResult(crafting);
}
public static ItemStack getHollowFillResult(InventoryCrafting crafting) {
return MicroRecipe$.MODULE$.getHollowFillResult(crafting);
}
}
@@ -1,247 +0,0 @@
package codechicken.microblock
import net.minecraft.item.crafting.IRecipe
import net.minecraft.world.World
import net.minecraft.inventory.InventoryCrafting
import net.minecraft.item.ItemStack
import codechicken.microblock.handler.MicroblockProxy._
import net.minecraftforge.oredict.RecipeSorter
object MicroRecipe extends IRecipe {
RecipeSorter.register(
"fmp:micro",
getClass,
RecipeSorter.Category.SHAPED,
"after:forge:shapelessore"
)
def getRecipeOutput = ItemMicroPart.create(1, 1, "tile.stone")
def getRecipeSize = 9
def matches(icraft: InventoryCrafting, world: World) =
getCraftingResult(icraft) != null
def getCraftingResult(icraft: InventoryCrafting): ItemStack = {
var res = getHollowResult(icraft)
if (res != null) return res
res = getGluingResult(icraft)
if (res != null) return res
res = getThinningResult(icraft)
if (res != null) return res
res = getSplittingResult(icraft)
if (res != null) return res
res = getHollowFillResult(icraft)
return res
}
def create(
amount: Int,
mcrClass: Int,
size: Int,
material: Int
): ItemStack = {
if (size == 8) {
val item = MicroMaterialRegistry.getMaterial(material).getItem.copy
item.stackSize = amount
return item
}
return ItemMicroPart.create(
amount,
mcrClass << 8 | size,
MicroMaterialRegistry.materialName(material)
)
}
def microMaterial(item: ItemStack) =
if (item.getItem == itemMicro)
ItemMicroPart.getMaterialID(item)
else
findMaterial(item)
def microClass(item: ItemStack) =
if (item.getItem == itemMicro)
item.getItemDamage >> 8
else
0
def microSize(item: ItemStack) =
if (item.getItem == itemMicro)
item.getItemDamage & 0xff
else
8
def getHollowResult(icraft: InventoryCrafting): ItemStack = {
if (icraft.getStackInRowAndColumn(1, 1) != null) return null
val first = icraft.getStackInRowAndColumn(0, 0)
if (first == null || first.getItem != itemMicro || microClass(first) != 0)
return null
val size = microSize(first)
val material = microMaterial(first)
for (i <- 1 to 8 if i != 4) {
val item = icraft.getStackInSlot(i)
if (
item == null || item.getItem != itemMicro ||
microMaterial(
item
) != material || item.getItemDamage != first.getItemDamage
)
return null
}
return create(8, 1, size, material)
}
def getGluingResult(icraft: InventoryCrafting): ItemStack = {
var size = 0
var count = 0
var smallest = 0
var mcrClass = 0
var material = 0
for (i <- 0 until 9) {
val item = icraft.getStackInSlot(i)
if (item != null) {
if (item.getItem != itemMicro) return null
if (count == 0) {
size = microSize(item)
mcrClass = microClass(item)
material = microMaterial(item)
count = 1
smallest = size
} else if (
microClass(item) != mcrClass || microMaterial(item) != material
) return null
else if (mcrClass >= 2 && microSize(item) != smallest) return null
else {
smallest = Math.min(smallest, microSize(item))
count += 1
size += microSize(item)
}
}
}
if (count <= 1) return null
mcrClass match {
case 3 =>
count match {
case 2 => create(1, 0, smallest, material)
case _ => null
}
case 2 =>
count match {
case 2 => create(1, 3, smallest, material)
case 4 => create(1, 0, smallest, material)
case _ => null
}
case 1 | 0 =>
val base = Seq(1, 2, 4).find(s => (s & size) != 0)
if (base.isEmpty)
create(size / 8, 0, 8, material)
else if (base.get <= smallest)
null
else
create(size / base.get, mcrClass, base.get, material)
case _ => null
}
}
def getSaw(icraft: InventoryCrafting): (Saw, Int, Int) = {
for (r <- 0 until 3)
for (c <- 0 until 3) {
val item = icraft.getStackInRowAndColumn(c, r)
if (item != null && item.getItem.isInstanceOf[Saw])
return (item.getItem.asInstanceOf[Saw], r, c)
}
return (null, 0, 0)
}
def canCut(saw: Saw, sawItem: ItemStack, material: Int): Boolean = {
val sawStrength = saw.getCuttingStrength(sawItem)
val matStrength =
MicroMaterialRegistry.getMaterial(material).getCutterStrength
return sawStrength >= matStrength || sawStrength == MicroMaterialRegistry.getMaxCuttingStrength
}
def getThinningResult(icraft: InventoryCrafting): ItemStack = {
val (saw, row, col) = getSaw(icraft)
if (saw == null)
return null
val item = icraft.getStackInRowAndColumn(col, row + 1)
if (item == null)
return null
val size = microSize(item)
val material = microMaterial(item)
val mcrClass = microClass(item)
if (
size == 1 || material < 0 || !canCut(
saw,
icraft.getStackInRowAndColumn(col, row),
material
)
)
return null
for (r <- 0 until 3)
for (c <- 0 until 3)
if (
(c != col || r != row && r != row + 1) &&
icraft.getStackInRowAndColumn(c, r) != null
)
return null
return create(2, mcrClass, size / 2, material)
}
def findMaterial(item: ItemStack): Int =
MicroMaterialRegistry.getIdMap.find { m =>
val mitem = m._2.getItem
item.getItem == mitem.getItem &&
item.getItemDamage == mitem.getItemDamage &&
ItemStack.areItemStackTagsEqual(item, mitem)
} match {
case None => -1
case Some((name, m)) => MicroMaterialRegistry.materialID(name)
}
val splitMap = Map(0 -> 3, 1 -> 3, 3 -> 2)
def getSplittingResult(icraft: InventoryCrafting): ItemStack = {
val (saw, row, col) = getSaw(icraft)
if (saw == null) return null
val item = icraft.getStackInRowAndColumn(col + 1, row)
if (item == null || item.getItem != itemMicro) return null
val mcrClass = microClass(item)
val material = microMaterial(item)
if (!canCut(saw, icraft.getStackInRowAndColumn(col, row), material))
return null
val split = splitMap.get(mcrClass)
if (split.isEmpty) return null
for (r <- 0 until 3)
for (c <- 0 until 3)
if (
(r != row || c != col && c != col + 1) &&
icraft.getStackInRowAndColumn(c, r) != null
)
return null
return create(2, split.get, microSize(item), material)
}
def getHollowFillResult(icraft: InventoryCrafting): ItemStack = {
var cover: ItemStack = null
for (i <- 0 until 9) {
val item = icraft.getStackInSlot(i)
if (item != null) {
if (item.getItem != itemMicro || cover != null || microClass(item) != 1)
return null
else cover = item
}
}
if (cover == null) return null
return create(1, 0, microSize(cover), microMaterial(cover))
}
}
@@ -90,7 +90,7 @@ class MicroblockProxy_serverImpl {
def init() {
CraftingManager.getInstance.getRecipeList
.asInstanceOf[JList[IRecipe]]
.add(MicroRecipe)
.add(MicroRecipe$.MODULE$)
if (!Loader.isModLoaded("dreamcraft")) {
CraftingManager.getInstance.addRecipe(
new ItemStack(stoneRod, 4),