|
| 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 | +} |
0 commit comments