Skip to content

Commit 6beec97

Browse files
committed
Add Mixins to skip Forestry fluid registration and redirect to GregTech fluids
1 parent d4403f7 commit 6beec97

6 files changed

Lines changed: 121 additions & 8 deletions

File tree

src/main/java/com/github/gtexpert/gtbm/integration/forestry/loaders/FFMFarmingLoader.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ public static void init() {
2424
IFarmProperties ceuFarm = ForestryFarmHelper.registerFarmType("farmCEu", FarmLogicCEu::new,
2525
EnumElectronTube.TIN);
2626

27-
String rubberId = "farmCEu.rubber";
28-
ceuFarm.registerFarmables(rubberId);
29-
farmRegistry.registerFarmables(rubberId,
30-
new FarmableGTCEuSapling(MetaBlocks.RUBBER_SAPLING,
31-
new ItemStack[] { MetaItems.STICKY_RESIN.getStackForm() }));
27+
if (ceuFarm != null) {
28+
String rubberId = "farmCEu.rubber";
29+
ceuFarm.registerFarmables(rubberId);
30+
farmRegistry.registerFarmables(rubberId,
31+
new FarmableGTCEuSapling(MetaBlocks.RUBBER_SAPLING,
32+
new ItemStack[] { MetaItems.STICKY_RESIN.getStackForm() }));
33+
}
3234

3335
// GregTech Fertilizer
3436
farmRegistry.registerFertilizer(MetaItems.FERTILIZER.getStackForm(), 500);

src/main/java/com/github/gtexpert/gtbm/integration/forestry/util/ForestryFarmHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static IFarmProperties registerFarmType(String farmId,
2424
EnumElectronTube tube) {
2525
if (ForestryAPI.farmRegistry == null) return null;
2626
IFarmProperties farm = ForestryAPI.farmRegistry.registerLogic(farmId, logicFactory);
27+
if (farm == null) return null;
2728
farm.registerSoil(new ItemStack(Blocks.DIRT),
2829
Block.getBlockFromItem(Mods.Forestry.getItem("humus").getItem()).getDefaultState());
2930

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.github.gtexpert.gtbm.mixins.forestry;
2+
3+
import java.util.Map;
4+
5+
import net.minecraftforge.fluids.Fluid;
6+
import net.minecraftforge.fluids.FluidRegistry;
7+
import net.minecraftforge.fluids.FluidStack;
8+
9+
import org.spongepowered.asm.mixin.Final;
10+
import org.spongepowered.asm.mixin.Mixin;
11+
import org.spongepowered.asm.mixin.Shadow;
12+
import org.spongepowered.asm.mixin.injection.At;
13+
import org.spongepowered.asm.mixin.injection.Inject;
14+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
15+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
16+
17+
import forestry.core.fluids.Fluids;
18+
19+
/**
20+
* Redirects Forestry's {@link Fluids} enum to return GregTech fluid objects
21+
* for fluids whose registration was skipped by {@link ModuleFluidsMixin}.
22+
* <p>
23+
* BIO_ETHANOL (tag "bio.ethanol") maps to GT's "ethanol",
24+
* SEED_OIL (tag "seed.oil") maps to GT's "seed_oil".
25+
* Other skipped fluids (biomass, glass, ice, milk) share the same name
26+
* and are resolved correctly without redirection.
27+
* <p>
28+
* Also extends the {@code tagToFluid} map so that
29+
* {@link Fluids#getFluidDefinition(FluidStack)} works with GT fluid names.
30+
*/
31+
@Mixin(value = Fluids.class, remap = false)
32+
public class FluidsMixin {
33+
34+
@Shadow
35+
@Final
36+
private static Map<String, Fluids> tagToFluid;
37+
38+
/**
39+
* Returns the GregTech fluid name for fluids whose Forestry tag differs
40+
* from the GregTech registration name, or {@code null} if no redirect is needed.
41+
*/
42+
private static String gtbm$getRedirectedName(Fluids self) {
43+
if (self == Fluids.SEED_OIL) {
44+
return "seed_oil";
45+
}
46+
if (self == Fluids.BIO_ETHANOL) {
47+
return "ethanol";
48+
}
49+
return null;
50+
}
51+
52+
@Inject(method = "getFluid()Lnet/minecraftforge/fluids/Fluid;", at = @At("HEAD"), cancellable = true)
53+
private void gtbm$redirectGetFluid(CallbackInfoReturnable<Fluid> cir) {
54+
String redirected = gtbm$getRedirectedName((Fluids) (Object) this);
55+
if (redirected != null) {
56+
cir.setReturnValue(FluidRegistry.getFluid(redirected));
57+
}
58+
}
59+
60+
@Inject(method = "getFluid(I)Lnet/minecraftforge/fluids/FluidStack;", at = @At("HEAD"), cancellable = true)
61+
private void gtbm$redirectGetFluidStack(int mb, CallbackInfoReturnable<FluidStack> cir) {
62+
String redirected = gtbm$getRedirectedName((Fluids) (Object) this);
63+
if (redirected != null) {
64+
cir.setReturnValue(FluidRegistry.getFluidStack(redirected, mb));
65+
}
66+
}
67+
68+
@Inject(method = "<clinit>", at = @At("TAIL"))
69+
private static void gtbm$extendTagToFluid(CallbackInfo ci) {
70+
tagToFluid.put("seed_oil", Fluids.SEED_OIL);
71+
tagToFluid.put("ethanol", Fluids.BIO_ETHANOL);
72+
}
73+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.gtexpert.gtbm.mixins.forestry;
2+
3+
import java.util.EnumSet;
4+
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.injection.At;
7+
import org.spongepowered.asm.mixin.injection.Inject;
8+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
9+
10+
import forestry.core.ModuleFluids;
11+
import forestry.core.fluids.Fluids;
12+
13+
/**
14+
* Prevents Forestry from registering fluids that GregTech will register,
15+
* so that GregTech's Fluid objects are used instead of Forestry's.
16+
* This avoids NPE crashes caused by Forestry fluid item model issues in JEI.
17+
*/
18+
@Mixin(value = ModuleFluids.class, remap = false)
19+
public class ModuleFluidsMixin {
20+
21+
private static final EnumSet<Fluids> SKIPPED_FLUIDS = EnumSet.of(
22+
Fluids.BIO_ETHANOL,
23+
Fluids.BIOMASS,
24+
Fluids.GLASS,
25+
Fluids.ICE,
26+
Fluids.MILK,
27+
Fluids.SEED_OIL);
28+
29+
@Inject(method = "createFluid", at = @At("HEAD"), cancellable = true)
30+
private static void gtbm$skipGregTechFluids(Fluids fluidDefinition, CallbackInfo ci) {
31+
if (SKIPPED_FLUIDS.contains(fluidDefinition)) {
32+
ci.cancel();
33+
}
34+
}
35+
}

src/main/resources/assets/gtbm/lang/ja_jp.lang

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ gtbm.farm.ceu=ゴムの木農園
3737
gtbm.farm.gtfo=果樹農園
3838

3939
# GUI
40-
gtbm.gui.auto_breeding.tooltip.enabled=自動交配: ON/nクイーン死亡後にプリンセスを自動的に入力スロットに戻します
41-
gtbm.gui.auto_breeding.tooltip.disabled=自動交配: OFF/nプリンセスはアウトプットに残ります
40+
gtbm.gui.auto_breeding.tooltip.enabled=自動交配を有効化 (現在は無効)/nクイーン死亡後にプリンセスを自動的に入力スロットに戻します
41+
gtbm.gui.auto_breeding.tooltip.disabled=自動交配を無効化 (現在は有効)/nプリンセスはアウトプットに残ります
4242

4343
# Multiblock Display
4444
gtbm.multiblock.progress=進捗: %s / %s (%s%%)

src/main/resources/mixins.gtbm.forestry.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"minVersion": "0.8",
66
"compatibilityLevel": "JAVA_8",
77
"mixins": [
8-
"FlowerRegistryAccessor"
8+
"FlowerRegistryAccessor",
9+
"FluidsMixin",
10+
"ModuleFluidsMixin"
911
],
1012
"client": [
1113
"ItemBeeGEMixin",

0 commit comments

Comments
 (0)