Skip to content

Commit d3c8f74

Browse files
committed
Update Neo and NG and make use of FML initialization for unit tests to remove the need for lazy codecs in ingredients
1 parent b275496 commit d3c8f74

File tree

6 files changed

+67
-62
lines changed

6 files changed

+67
-62
lines changed

build.gradle

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ plugins {
1818
id('eclipse')
1919
id('idea')
2020
id('maven-publish')
21-
id('net.neoforged.gradle.userdev') version('7.0.134')//https://projects.neoforged.net/neoforged/neogradle
21+
id('net.neoforged.gradle.userdev') version('7.0.136')//https://projects.neoforged.net/neoforged/neogradle
2222
}
2323

2424
tasks.named('wrapper', Wrapper).configure {
@@ -63,46 +63,35 @@ sourceSets {
6363
srcDirs += ['src/datagen/generated/mekanism']
6464
}
6565
compileClasspath += api.output
66-
runtimeClasspath += api.output
6766
}
6867
test {
6968
//The test module has no resources
7069
resources.srcDirs = []
7170
//Add the api to the output, all other ones that need to will get added via setupExtraSourceSets
7271
compileClasspath += api.output
73-
runtimeClasspath += api.output
7472
}
7573
gameTest {
7674
runs {
7775
modIdentifier = 'mekanismtests'
7876
}
7977
compileClasspath += api.output
80-
runtimeClasspath += api.output
8178
}
8279
}
8380

8481
configurations {
85-
// Sets up a dependency configuration called 'localRuntime'.
86-
// This configuration should be used instead of 'runtimeOnly' to declare
87-
// a dependency that will be present for runtime testing but that is
88-
// "optional", meaning it will not be pulled by dependents of this mod.
89-
runtimeClasspath.extendsFrom(localRuntime)
90-
82+
//Make sure all our sub source set stuff extends the proper base methods so that
83+
// they can see all the dependencies we have in dependencies including neo
84+
extendConfigurations(implementation, apiImplementation, testImplementation)
85+
extendConfigurations(compileOnly, apiCompileOnly, testCompileOnly)
86+
extendConfigurations(runtimeOnly, apiRuntimeOnly)
87+
extendConfigurations(localRuntime, apiLocalRuntime)
9188
datagenNonMod
9289
}
9390

9491
//Add all extra source sets that the main sourceSet should have
9592
setupExtraSourceSets(sourceSets.main)
9693
setupExtraSourceSets(sourceSets.gameTest, false)
9794

98-
configurations {
99-
//Make sure all our sub source set stuff extends the proper base methods so that
100-
// they can see all the dependencies we have in dependencies including forge
101-
extendConfigurations(implementation, apiImplementation, testImplementation)
102-
extendConfigurations(compileOnly, apiCompileOnly, testCompileOnly)
103-
extendConfigurations(runtimeOnly, apiRuntimeOnly)
104-
}
105-
10695
//Create sourceSets and configurations for each of the additional modules in src/$name and adds a reference to
10796
// the corresponding data gen's resource directory excluding the cache. It also adds the api and main mekanism
10897
// module to the dependencies of the source set we are setting up, and sets up all extra source sets that are
@@ -127,21 +116,21 @@ for (String name : secondaryModules) {
127116
def setupExtraSourceSets(SourceSet base, boolean includeExtra = true) {
128117
//Expose the base module to junit
129118
project.sourceSets.test.compileClasspath += base.output
130-
project.sourceSets.test.runtimeClasspath += base.output
131119
if (base != project.sourceSets.gameTest) {
132120
project.sourceSets.gameTest.compileClasspath += base.output
133-
project.sourceSets.gameTest.runtimeClasspath += base.output
134121
}
135122
//Setup and extend configurations for alternate modules. First by making the implementation, compileOnly, runtimeOnly equivalents
136123
// for those modules extend the main ones
137124
def baseImplementation = project.configurations.maybeCreate(base.getTaskName(null, 'implementation'))
138125
def baseCompileOnly = project.configurations.maybeCreate(base.getTaskName(null, 'compileOnly'))
139126
def baseRuntimeOnly = project.configurations.maybeCreate(base.getTaskName(null, 'runtimeOnly'))
127+
def baseLocalRuntime = project.configurations.maybeCreate(base.getTaskName(null, 'localRuntime'))
140128
if (base != project.sourceSets.main) {
141129
// If this is a secondary module then make the base tasks extend the builtin ones
142130
baseImplementation.extendsFrom(project.configurations.getByName('implementation'))
143131
baseCompileOnly.extendsFrom(project.configurations.getByName('compileOnly'))
144132
baseRuntimeOnly.extendsFrom(project.configurations.getByName('runtimeOnly'))
133+
baseLocalRuntime.extendsFrom(project.configurations.getByName('localRuntime'))
145134
}
146135
if (includeExtra) {
147136
//And then setup and have all the extra sourceSets have their configurations extend the ones for the base module so that they can
@@ -157,6 +146,7 @@ def setupExtraSourceSets(SourceSet base, boolean includeExtra = true) {
157146
project.configurations.maybeCreate(extraSourceSet.getTaskName(null, 'implementation')).extendsFrom(*implExtends)
158147
project.configurations.maybeCreate(extraSourceSet.getTaskName(null, 'compileOnly')).extendsFrom(baseCompileOnly)
159148
project.configurations.maybeCreate(extraSourceSet.getTaskName(null, 'runtimeOnly')).extendsFrom(baseRuntimeOnly)
149+
project.configurations.maybeCreate(extraSourceSet.getTaskName(null, 'localRuntime')).extendsFrom(baseLocalRuntime)
160150
}
161151
}
162152
}
@@ -348,6 +338,9 @@ runs {
348338
setupClientAcc(run)
349339
modSources.add((SourceSet) sourceSets.gameTest)
350340
}
341+
junit {
342+
unitTestSources.add((SourceSet) sourceSets.test)
343+
}
351344
data {
352345
programArguments.addAll((String[]) ['--all', '--output', file('src/datagen/generated/').getAbsolutePath(),
353346
'--mod', 'mekanism', '--existing', file('src/main/resources/').getAbsolutePath()])
@@ -402,8 +395,9 @@ repositories { RepositoryHandler handler ->
402395
exclusiveRepo(handler, 'https://maven.parchmentmc.org/', 'org.parchmentmc.data')
403396
}
404397

405-
tasks.named('test', Test) {
406-
useJUnitPlatform()
398+
tasks.named('test').configure {
399+
//Disable builtin test task as we use and build uses testJunit so there is no point in having it also attempt to run an empty test task
400+
enabled(false)
407401
}
408402

409403
dependencies {
@@ -413,7 +407,9 @@ dependencies {
413407
localRuntime(project(':annotation-processor'))
414408
annotationProcessor(project(':annotation-processor'))
415409

416-
gameTestImplementation("net.neoforged:testframework:${forge_version}")
410+
def testFrameWork = "net.neoforged:testframework:${forge_version}"
411+
gameTestImplementation(testFrameWork)
412+
testImplementation(testFrameWork)
417413
testImplementation("org.junit.jupiter:junit-jupiter-api:${junit_version}")
418414
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junit_version}")
419415
//We use https://github.com/quicktheories/QuickTheories to allow for implementing property based testing

gradle.properties

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ minecraft_version=1.20.6
88
previous_minecraft_version=1.20.1
99
previous_minor_minecraft_version=1.20.4
1010
loader_version_range=[3,)
11-
forge_version=20.6.78-beta
11+
forge_version=20.6.98-beta
1212
mod_version=10.5.20
1313
#This determines the minimum version of forge required to use Mekanism
1414
# Only bump it whenever we need access to a feature in forge that is not available in earlier versions
15-
forge_version_range=[20.6.78-beta,)
15+
forge_version_range=[20.6.94-beta,)
1616
minecraft_version_range=[1.20.6]
1717
#This specifies what type of release it will be uploaded to CurseForge and Modrinth as
1818
# options are: alpha, beta, release
@@ -22,9 +22,10 @@ release_type=beta
2222
junit_version=5.10.2
2323
quicktheories_version=0.26
2424

25-
#Parchment settings
25+
#NeoGradle Settings
2626
neogradle.subsystems.parchment.minecraftVersion=1.20.6
2727
neogradle.subsystems.parchment.mappingsVersion=2024.05.01
28+
neogradle.subsystems.conventions.sourcesets.enabled=false
2829

2930
#misc settings
3031
# recipe viewer, currently accepts: jei, emi, hybrid

src/api/java/mekanism/api/recipes/ingredients/FluidStackIngredient.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import net.minecraft.network.codec.StreamCodec;
99
import net.neoforged.neoforge.fluids.FluidStack;
1010
import net.neoforged.neoforge.fluids.crafting.SizedFluidIngredient;
11-
import net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs;
1211
import org.jetbrains.annotations.ApiStatus.Internal;
1312
import org.jetbrains.annotations.NotNull;
1413
import org.jetbrains.annotations.Nullable;
@@ -26,21 +25,16 @@ public final class FluidStackIngredient implements InputIngredient<@NotNull Flui
2625
/**
2726
* A codec which can (de)encode fluid stack ingredients.
2827
*
29-
* @implNote This must be a lazily initialized so that this class can be loaded in tests
3028
* @since 10.6.0
3129
*/
32-
public static final Codec<FluidStackIngredient> CODEC = Codec.lazyInitialized(() -> SizedFluidIngredient.FLAT_CODEC.xmap(
33-
FluidStackIngredient::new, FluidStackIngredient::ingredient
34-
));
30+
public static final Codec<FluidStackIngredient> CODEC = SizedFluidIngredient.FLAT_CODEC.xmap(FluidStackIngredient::new, FluidStackIngredient::ingredient);
3531
/**
3632
* A stream codec which can be used to encode and decode fluid stack ingredients over the network.
3733
*
38-
* @implNote This must be a lazily initialized so that this class can be loaded in tests
3934
* @since 10.6.0
4035
*/
41-
public static final StreamCodec<RegistryFriendlyByteBuf, FluidStackIngredient> STREAM_CODEC = NeoForgeStreamCodecs.lazy(() ->
42-
SizedFluidIngredient.STREAM_CODEC.map(FluidStackIngredient::new, FluidStackIngredient::ingredient)
43-
);
36+
public static final StreamCodec<RegistryFriendlyByteBuf, FluidStackIngredient> STREAM_CODEC = SizedFluidIngredient.STREAM_CODEC
37+
.map(FluidStackIngredient::new, FluidStackIngredient::ingredient);
4438

4539
/**
4640
* Creates a Fluid Stack Ingredient that matches a given ingredient and amount. Prefer calling via

src/api/java/mekanism/api/recipes/ingredients/ItemStackIngredient.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import net.minecraft.network.codec.StreamCodec;
99
import net.minecraft.world.item.ItemStack;
1010
import net.neoforged.neoforge.common.crafting.SizedIngredient;
11-
import net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs;
1211
import org.jetbrains.annotations.ApiStatus.Internal;
1312
import org.jetbrains.annotations.NotNull;
1413
import org.jetbrains.annotations.Nullable;
@@ -26,21 +25,16 @@ public final class ItemStackIngredient implements InputIngredient<@NotNull ItemS
2625
/**
2726
* A codec which can (de)encode item stack ingredients.
2827
*
29-
* @implNote This must be a lazily initialized so that this class can be loaded in tests
3028
* @since 10.6.0
3129
*/
32-
public static final Codec<ItemStackIngredient> CODEC = Codec.lazyInitialized(() -> SizedIngredient.FLAT_CODEC.xmap(
33-
ItemStackIngredient::new, ItemStackIngredient::ingredient
34-
));
30+
public static final Codec<ItemStackIngredient> CODEC = SizedIngredient.FLAT_CODEC.xmap(ItemStackIngredient::new, ItemStackIngredient::ingredient);
3531
/**
3632
* A stream codec which can be used to encode and decode item stack ingredients over the network.
3733
*
38-
* @implNote This must be a lazily initialized so that this class can be loaded in tests
3934
* @since 10.6.0
4035
*/
41-
public static final StreamCodec<RegistryFriendlyByteBuf, ItemStackIngredient> STREAM_CODEC = NeoForgeStreamCodecs.lazy(() ->
42-
SizedIngredient.STREAM_CODEC.map(ItemStackIngredient::new, ItemStackIngredient::ingredient)
43-
);
36+
public static final StreamCodec<RegistryFriendlyByteBuf, ItemStackIngredient> STREAM_CODEC = SizedIngredient.STREAM_CODEC
37+
.map(ItemStackIngredient::new, ItemStackIngredient::ingredient);
4438

4539
/**
4640
* Creates an Item Stack Ingredient that matches a given ingredient and amount. Prefer calling via
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package mekanism.api;
2+
3+
import mekanism.api.energy.IEnergyConversionHelper;
4+
import mekanism.api.gear.IModuleHelper;
5+
import mekanism.api.radial.IRadialDataHelper;
6+
import mekanism.api.radiation.IRadiationManager;
7+
import mekanism.api.security.IBlockSecurityUtils;
8+
import mekanism.api.security.IEntitySecurityUtils;
9+
import mekanism.api.security.IItemSecurityUtils;
10+
import mekanism.api.security.ISecurityUtils;
11+
import mekanism.api.text.ITooltipHelper;
12+
import mekanism.common.Mekanism;
13+
import net.neoforged.fml.common.Mod;
14+
15+
/**
16+
* Add an extra injection point for all our service loaders to force load them while we know for a fact we are using the transforming class loader, as there seems to be
17+
* some issue with {@link net.neoforged.fml.junit.JUnitService}, and presumably the fact it doesn't use {@link org.junit.platform.launcher.LauncherInterceptor} which
18+
* causes all the test methods to actually be executed using the app class loader.
19+
*/
20+
@Mod(Mekanism.MODID)
21+
public class MekanismClassInit {//TODO: Remove this as soon as possible
22+
23+
public MekanismClassInit() {
24+
forceInit(IMekanismAccess.INSTANCE);
25+
forceInit(IModuleHelper.INSTANCE);
26+
forceInit(IRadialDataHelper.INSTANCE);
27+
forceInit(IRadiationManager.INSTANCE);
28+
forceInit(ISecurityUtils.INSTANCE);
29+
forceInit(IBlockSecurityUtils.INSTANCE);
30+
forceInit(IItemSecurityUtils.INSTANCE);
31+
forceInit(IEntitySecurityUtils.INSTANCE);
32+
forceInit(ITooltipHelper.INSTANCE);
33+
forceInit(IEnergyConversionHelper.INSTANCE);
34+
}
35+
36+
private void forceInit(Object obj) {
37+
}
38+
}

src/tools/java/mekanism/tools/common/item/ItemMekanismSword.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@
44
import mekanism.tools.common.material.MaterialCreator;
55
import mekanism.tools.common.util.ToolsUtils;
66
import net.minecraft.network.chat.Component;
7-
import net.minecraft.world.entity.EquipmentSlotGroup;
8-
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
9-
import net.minecraft.world.entity.ai.attributes.Attributes;
107
import net.minecraft.world.item.Item;
118
import net.minecraft.world.item.ItemStack;
129
import net.minecraft.world.item.SwordItem;
13-
import net.minecraft.world.item.Tier;
1410
import net.minecraft.world.item.TooltipFlag;
15-
import net.minecraft.world.item.component.ItemAttributeModifiers;
1611
import org.jetbrains.annotations.NotNull;
1712

1813
public class ItemMekanismSword extends SwordItem {
@@ -26,17 +21,4 @@ public void appendHoverText(@NotNull ItemStack stack, @NotNull Item.TooltipConte
2621
super.appendHoverText(stack, context, tooltip, flag);
2722
ToolsUtils.addDurability(tooltip, stack);
2823
}
29-
30-
/**
31-
* Like {@link SwordItem#createAttributes(Tier, int, float)} but supports float based attack damages.
32-
*/
33-
@NotNull
34-
private static ItemAttributeModifiers createAttributes(Tier tier, float attackDamage, float attackSpeed) {
35-
return ItemAttributeModifiers.builder()
36-
.add(Attributes.ATTACK_DAMAGE, new AttributeModifier(BASE_ATTACK_DAMAGE_UUID, "Weapon modifier", attackDamage + tier.getAttackDamageBonus(),
37-
AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND)
38-
.add(Attributes.ATTACK_SPEED, new AttributeModifier(BASE_ATTACK_SPEED_UUID, "Weapon modifier", attackSpeed, AttributeModifier.Operation.ADD_VALUE),
39-
EquipmentSlotGroup.MAINHAND
40-
).build();
41-
}
4224
}

0 commit comments

Comments
 (0)