Skip to content

Commit 11ca8f6

Browse files
committed
change: provide more flexibility with recipe containers
1 parent 100a489 commit 11ca8f6

File tree

7 files changed

+145
-27
lines changed

7 files changed

+145
-27
lines changed

.github/workflows/codeql.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ jobs:
2323
java-version: 17
2424
distribution: temurin
2525

26-
- name: Cache gradle dependencies
26+
- name: Gradle cache
2727
uses: actions/cache/restore@v4
2828
with:
2929
fail-on-cache-miss: true
3030
path: |
3131
~/.gradle/caches
3232
~/.gradle/wrapper
3333
.gradle
34-
key: ${{ runner.os }}-gradle-${{ hashFiles('gradle/gradle-wrapper.properties', 'settings.gradle.kts', 'build.gradle.kts', 'gradle.properties') }}-${{github.ref_name}}
34+
key: ${{ runner.os }}-gradle-${{ hashFiles('gradle/gradle-wrapper.properties', 'settings.gradle.kts', 'build.gradle.kts', 'gradle.properties') }}
3535

3636
- name: Initialize build environment
3737
run: ./gradlew

.github/workflows/commit.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,21 @@ jobs:
2020
java-version: 17
2121
distribution: temurin
2222

23-
- name: Cache Gradle
23+
- name: Gradle cache
2424
uses: actions/cache@v4
2525
with:
2626
path: |
2727
~/.gradle/caches
2828
~/.gradle/wrapper
2929
.gradle
30-
key: ${{ runner.os }}-gradle-${{ hashFiles('gradle/gradle-wrapper.properties', 'settings.gradle.kts', 'build.gradle.kts', 'gradle.properties') }}-${{github.ref_name}}
30+
key: ${{ runner.os }}-gradle-${{ hashFiles('gradle/gradle-wrapper.properties', 'settings.gradle.kts', 'build.gradle.kts', 'gradle.properties') }}
3131
restore-keys: |
32-
${{ runner.os }}-gradle-${{ hashFiles('gradle/gradle-wrapper.properties', 'settings.gradle.kts', 'build.gradle.kts', 'gradle.properties') }}
3332
${{ runner.os }}-gradle-
3433
3534
- name: Initialize build environment
3635
run: ./gradlew
3736

38-
- name: Check license headers
37+
- name: License headers
3938
run: ./gradlew checkLicenses
4039

4140
- name: Build

.github/workflows/pr_check.yml

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,23 @@ jobs:
2121
java-version: 17
2222
distribution: temurin
2323

24-
- name: Cache Gradle
24+
- name: Gradle cache
2525
uses: actions/cache@v4
2626
with:
2727
path: |
2828
~/.gradle/caches
2929
~/.gradle/wrapper
3030
.gradle
31-
key: ${{ runner.os }}-gradle-PR${{github.event.number}}-${{ hashFiles('gradle/gradle-wrapper.properties', 'settings.gradle.kts', '**/build.gradle.kts', '**/gradle.properties') }}-${{github.ref_name}}
31+
key: ${{ runner.os }}-gradle-PR${{github.event.number}}-${{ hashFiles('gradle/gradle-wrapper.properties', 'settings.gradle.kts', '**/build.gradle.kts', '**/gradle.properties') }}
3232
restore-keys: |
33-
${{ runner.os }}-gradle-PR${{github.event.number}}-${{ hashFiles('gradle/gradle-wrapper.properties', 'settings.gradle.kts', '**/build.gradle.kts', '**/gradle.properties') }}
34-
${{ runner.os }}-gradle-PR${{github.event.number}}-
3533
${{ runner.os }}-gradle-${{ hashFiles('gradle/gradle-wrapper.properties', 'settings.gradle.kts', '**/build.gradle.kts', '**/gradle.properties') }}
34+
${{ runner.os }}-gradle-PR${{github.event.number}}-
3635
${{ runner.os }}-gradle-
3736
3837
- name: Initialize build environment
3938
run: ./gradlew
4039

41-
- name: Check license headers
40+
- name: License headers
4241
run: ./gradlew checkLicenses
4342

4443
- name: Build
@@ -48,4 +47,4 @@ jobs:
4847

4948
- name: Test
5049
if: ${{ steps.build.outcome == 'success' && !cancelled() }}
51-
run: ./gradlew test --no-daemon
50+
run: ./gradlew test

build.gradle.kts

+4-9
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,12 @@ dependencies {
189189
modRuntimeOnly("net.fabricmc.fabric-api:fabric-api:$fabric")
190190

191191
modCompileOnly("mcp.mobius.waila:wthit-api:fabric-$wthit")
192-
modCompileOnly("me.shedaniel:RoughlyEnoughItems-api-fabric:$rei") { excludeFabric() }
192+
modCompileOnly("me.shedaniel:RoughlyEnoughItems-api-fabric:$rei")
193193
modCompileOnly("dev.architectury:architectury-fabric:$architectury")
194194

195-
modImplementation("me.shedaniel.cloth:cloth-config-fabric:$clothConfig") { excludeFabric() }
196-
modImplementation("com.terraformersmc:modmenu:$modmenu") { excludeFabric() }
197-
modImplementation("lol.bai:badpackets:fabric-$badpackets") { excludeFabric() }
195+
modImplementation("me.shedaniel.cloth:cloth-config-fabric:$clothConfig")
196+
modImplementation("com.terraformersmc:modmenu:$modmenu")
197+
modImplementation("lol.bai:badpackets:fabric-$badpackets")
198198

199199
"testmodImplementation"(sourceSets.main.get().output)
200200
"modTestmodImplementation"("net.fabricmc.fabric-api:fabric-api:$fabric")
@@ -203,11 +203,6 @@ dependencies {
203203
modRuntimeOnly("mcp.mobius.waila:wthit:fabric-$wthit")
204204
}
205205

206-
fun ModuleDependency.excludeFabric() {
207-
exclude(group = "net.fabricmc")
208-
exclude(group = "net.fabricmc.fabric-api")
209-
}
210-
211206
tasks.withType<ProcessResources> {
212207
val properties = mapOf(
213208
"version" to project.version,

src/main/java/dev/galacticraft/machinelib/api/block/entity/BasicRecipeMachineBlockEntity.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
package dev.galacticraft.machinelib.api.block.entity;
2424

25-
import dev.galacticraft.machinelib.api.compat.vanilla.RecipeTestContainer;
2625
import dev.galacticraft.machinelib.api.machine.MachineType;
2726
import dev.galacticraft.machinelib.api.menu.RecipeMachineMenu;
2827
import net.minecraft.core.BlockPos;
@@ -45,7 +44,7 @@ public abstract class BasicRecipeMachineBlockEntity<C extends Container, R exten
4544
/**
4645
* An inventory for use in finding vanilla recipes for this machine.
4746
*/
48-
protected final @NotNull Container craftingInv;
47+
protected final @NotNull C craftingInv;
4948

5049
protected final int inputSlots;
5150
protected final int inputSlotsLen;
@@ -104,9 +103,11 @@ protected BasicRecipeMachineBlockEntity(@NotNull MachineType<? extends BasicReci
104103
this.outputSlots = outputSlots;
105104
this.outputSlotsLen = outputSlotsLen;
106105

107-
this.craftingInv = RecipeTestContainer.create(this.itemStorage(), this.inputSlots, this.inputSlotsLen);
106+
this.craftingInv = this.createCraftingInv();
108107
}
109108

109+
protected abstract C createCraftingInv();
110+
110111
/**
111112
* Creates an inventory for use in finding vanilla recipes for this machine.
112113
* NOTE: This inventory can assume that it is never modified - do not modify it!
@@ -116,7 +117,7 @@ protected BasicRecipeMachineBlockEntity(@NotNull MachineType<? extends BasicReci
116117
@Override
117118
@Contract(pure = true)
118119
protected @NotNull C craftingInv() {
119-
return (C) this.craftingInv;
120+
return this.craftingInv;
120121
}
121122

122123
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
2+
/*
3+
* Copyright (c) 2021-2024 Team Galacticraft
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
24+
package dev.galacticraft.machinelib.api.compat.vanilla;
25+
26+
import com.google.common.collect.Iterators;
27+
import dev.galacticraft.machinelib.api.storage.SlottedStorageAccess;
28+
import dev.galacticraft.machinelib.api.storage.slot.ItemResourceSlot;
29+
import dev.galacticraft.machinelib.api.util.ItemStackUtil;
30+
import net.minecraft.world.entity.player.StackedContents;
31+
import net.minecraft.world.inventory.CraftingContainer;
32+
import net.minecraft.world.item.Item;
33+
import net.minecraft.world.item.ItemStack;
34+
import org.jetbrains.annotations.Contract;
35+
import org.jetbrains.annotations.NotNull;
36+
37+
import java.util.ArrayList;
38+
import java.util.Iterator;
39+
import java.util.List;
40+
41+
/**
42+
* A container for testing recipes using the vanilla recipe system.
43+
*
44+
* @see RecipeTestContainer
45+
* @see CraftingContainer
46+
*/
47+
public class CraftingRecipeTestContainer extends RecipeTestContainer implements CraftingContainer {
48+
/**
49+
* The width of the crafting grid
50+
*/
51+
private final int width;
52+
/**
53+
* The height of the crafting grid
54+
*/
55+
private final int height;
56+
57+
/**
58+
* Creates a new container with the given slots.
59+
*
60+
* @param slots the slots to use
61+
* @return a new test container
62+
*/
63+
@Contract(value = "_, _, _ -> new", pure = true)
64+
public static @NotNull CraftingRecipeTestContainer create(int width, int height, ItemResourceSlot @NotNull ... slots) {
65+
assert slots.length > 0 && slots.length == width * height;
66+
return new CraftingRecipeTestContainer(width, height, slots);
67+
}
68+
69+
/**
70+
* Creates a new container with the slots specified by a slice of the given storage access
71+
*
72+
* @param access the storage access providing the slots
73+
* @param start the index of the first slot to include in the container
74+
* @param len the number of slots to include in the container
75+
* @return a new test container
76+
*/
77+
@Contract(value = "_, _, _, _, _-> new", pure = true)
78+
public static @NotNull CraftingRecipeTestContainer create(int width, int height, SlottedStorageAccess<Item, ItemResourceSlot> access, int start, int len) {
79+
assert len == width * height;
80+
Iterator<ItemResourceSlot> iterator = access.iterator();
81+
Iterators.advance(iterator, start);
82+
ItemResourceSlot[] slots = new ItemResourceSlot[len];
83+
for (int i = 0; i < len; i++) {
84+
slots[i] = iterator.next();
85+
}
86+
return new CraftingRecipeTestContainer(width, height, slots);
87+
}
88+
89+
/**
90+
* Constructs a new RecipeTestContainer with the provided slots.
91+
*
92+
* @param width the width of the crafting grid
93+
* @param height the height of the crafting grid
94+
* @param slots the slots to be included in the container
95+
*/
96+
private CraftingRecipeTestContainer(int width, int height, ItemResourceSlot[] slots) {
97+
super(slots);
98+
this.width = width;
99+
this.height = height;
100+
}
101+
102+
@Override
103+
public int getWidth() {
104+
return this.width;
105+
}
106+
107+
@Override
108+
public int getHeight() {
109+
return this.height;
110+
}
111+
112+
@Override
113+
public @NotNull List<ItemStack> getItems() {
114+
List<ItemStack> list = new ArrayList<>(this.slots.length);
115+
for (ItemResourceSlot slot : this.slots) {
116+
list.add(ItemStackUtil.create(slot));
117+
}
118+
return list;
119+
}
120+
121+
@Override
122+
public void fillStackedContents(StackedContents finder) {
123+
}
124+
}

src/main/java/dev/galacticraft/machinelib/api/compat/vanilla/RecipeTestContainer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class RecipeTestContainer implements Container {
4444
/**
4545
* The slots contained in this container
4646
*/
47-
private final ItemResourceSlot[] slots;
47+
protected final ItemResourceSlot[] slots;
4848

4949
/**
5050
* Creates a new container with the given slots.
@@ -82,7 +82,7 @@ public class RecipeTestContainer implements Container {
8282
*
8383
* @param slots the slots to be included in the container
8484
*/
85-
private RecipeTestContainer(ItemResourceSlot[] slots) {
85+
RecipeTestContainer(ItemResourceSlot[] slots) {
8686
this.slots = slots;
8787
}
8888

0 commit comments

Comments
 (0)