Skip to content

Commit

Permalink
Partially implement custom fluids for Fabric 1.20.1
Browse files Browse the repository at this point in the history
  • Loading branch information
beckadamtheinventor committed Jul 9, 2024
1 parent 838f8c8 commit f66cdf3
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 6 deletions.
4 changes: 4 additions & 0 deletions _m3ec/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,10 @@ def readf(data, d):
w = w.capitalize()
elif fn.lower() == "title":
w = w.title()
elif fn.lower() == "class":
w = "".join([s.capitalize() for s in w.replace("_", " ").split(" ")])
elif fn.lower() == "bool":
w = "true" if w else "false"
elif fn.lower() == "float":
if type(w) is str:
if not w.endswith("f"):
Expand Down
26 changes: 26 additions & 0 deletions data/default_content_files/fluid.m3ec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@: fluid
fluid: SimpleFluid
contentid:
title:
texture:

# block to copy properties from
copyblock: Blocks.WATER

# update speed of the fluid?
# probably measured in ticks
tickrate: 5

# fluid level decrease per block spread
decreaseperblock: 1

# flow speed of the fluid.
# probably measured in ticks
flowspeed: 4

# Whether the fluid creates source blocks like water
isinfinite: false

# Hex color of fluid.
# 2 digits for each Red, Green, Blue, Alpha
color: 0x808080D0
10 changes: 6 additions & 4 deletions data/fabric1.20.1/MainClass.m3ecjava
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package ${mod.package};

import ${mod.package}.registry.ModBlocks;
import ${mod.package}.registry.ModItems;
---iter mod.registry.recipetype.names
import ${mod.package}.recipetype.${mod.recipetype.$%v.class}Serializer;
import ${mod.package}.recipetype.${mod.recipetype.$%v.class}Recipe;---end

import ${mod.package}.registry.ModFluids;
---if mod.registry.recipetype #length nonzero
import ${mod.package}.recipetype.*;---fi
---iter mainclass.imports
import $%v;---end
import net.fabricmc.loader.api.FabricLoader;
Expand Down Expand Up @@ -67,6 +66,7 @@ public class ${mod.class} implements ModInitializer {
$%v---end
ModItems.RegisterItems();
ModBlocks.RegisterBlocks();
ModFluids.RegisterFluids();
---iter mod.registry.recipetype.names
Registry.register(Registry.RECIPE_SERIALIZER, ${mod.recipetype.$%v.class}Serializer.ID, ${mod.recipetype.$%v.class}Serializer.INSTANCE);
Registry.register(Registry.RECIPE_TYPE, new Identifier(${mod.class}.MOD_ID, "$%v"), ${mod.recipetype.$%v.class}Recipe.Type.INSTANCE);---end
Expand All @@ -87,6 +87,8 @@ public class ${mod.class} implements ModInitializer {
entries.add(ModItems.${mod.tool.$%v^upper});---end
---iter mod.registry.sapling.names
entries.add(ModItems.${mod.sapling.$%v^upper});---end
---iter mod.registry.fluid.names
entries.add(ModFluids.${mod.fluid.$%v^upper}_BUCKET);---end
})
.build()
);---fi
Expand Down
23 changes: 23 additions & 0 deletions data/fabric1.20.1/MainClassClient.m3ecjava
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ${mod.package};

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandlerRegistry;
import net.fabricmc.fabric.api.client.render.fluid.v1.SimpleFluidRenderHandler;
import ${mod.package}.registry.ModBlocks;
import ${mod.package}.registry.ModFluids;
import net.minecraft.client.render.RenderLayer;

public class ${mod.class}Client implements ClientModInitializer {
@Override
public void onInitializeClient() {

---iter mod.registry.fluid.names
FluidRenderHandlerRegistry.INSTANCE.register(ModFluids.STILL_${mod.fluid.$%v^upper}, ModFluids.FLOWING_${mod.fluid.$%v^upper},
SimpleFluidRenderHandler.coloredWater(${mod.fluid.$%v.color}));
BlockRenderLayerMap.INSTANCE.putFluids(RenderLayer.getTranslucent(),
ModFluids.STILL_${mod.fluid.$%v^upper}, ModFluids.FLOWING_${mod.fluid.$%v^upper});
---end
}
}
14 changes: 14 additions & 0 deletions data/fabric1.20.1/m3ec_build.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"action": "makedir",
"value": [
"src/main/java/${mod.packagedir}/blocks",
"src/main/java/${mod.packagedir}/fluids",
"src/main/java/${mod.packagedir}/features",
"src/main/java/${mod.packagedir}/recipetype",
"src/main/resources/assets/minecraft",
Expand Down Expand Up @@ -119,6 +120,10 @@
"source": "MainClass.m3ecjava",
"dest": "src/main/java/${mod.packagedir}/${mod.class}.java"
},
{
"source": "MainClassClient.m3ecjava",
"dest": "src/main/java/${mod.packagedir}/${mod.class}Client.java"
},
{
"source": "registry/ModBlocks.m3ecjava",
"dest": "src/main/java/${mod.packagedir}/registry/ModBlocks.java"
Expand All @@ -127,6 +132,10 @@
"source": "registry/ModItems.m3ecjava",
"dest": "src/main/java/${mod.packagedir}/registry/ModItems.java"
},
{
"source": "registry/ModFluids.m3ecjava",
"dest": "src/main/java/${mod.packagedir}/registry/ModFluids.java"
},
{
"iterate": "mod.registry.toolmaterial.names",
"source": "../fabric_common/ToolMaterial.m3ecjava",
Expand Down Expand Up @@ -157,6 +166,11 @@
"source": "recipetype/RecipeSerializer.m3ecjava",
"dest": "src/main/java/${mod.packagedir}/recipetype/${mod.recipetype.${%v}.class}Serializer.java"
},
{
"iterate": "mod.registry.fluid.names",
"source": "registry/Fluid.m3ecjava",
"dest": "src/main/java/${mod.packagedir}/fluids/${mod.fluid.${%v}.class}Fluid.java"
},
{
"iterate": "mod.registry.sapling.names",
"source": "registry/SaplingBlock.m3ecjava",
Expand Down
121 changes: 121 additions & 0 deletions data/fabric1.20.1/registry/Fluid.m3ecjava
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package ${mod.package}.fluids;

import ${mod.package}.registry.ModFluids;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.fluid.FlowableFluid;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.FluidState;
import net.minecraft.item.Item;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.Properties;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import net.minecraft.world.WorldView;

public abstract class ${mod.fluid.${%v}^class}Fluid extends FlowableFluid {
@Override
public Fluid getFlowing() {
return ModFluids.FLOWING_${mod.fluid.${%v}^upper};
}

@Override
public Fluid getStill() {
return ModFluids.STILL_${mod.fluid.${%v}^upper};
}

@Override
protected boolean isInfinite(World world) {
return ${mod.fluid.${%v}.isinfinite^bool};
}

@Override
protected void beforeBreakingBlock(WorldAccess world, BlockPos pos, BlockState state) {
final BlockEntity blockEntity = state.hasBlockEntity() ? world.getBlockEntity(pos) : null;
Block.dropStacks(state, world, pos, blockEntity);
}

@Override
protected int getFlowSpeed(WorldView world) {
return ${mod.fluid.${%v}.flowspeed^int};
}

@Override
protected int getLevelDecreasePerBlock(WorldView world) {
return ${mod.fluid.${%v}.decreaseperblock^int};
}

@Override
public Item getBucketItem() {
return ModFluids.${mod.fluid.${%v}^upper}_BUCKET;
}

@Override
protected boolean canBeReplacedWith(FluidState state, BlockView world, BlockPos pos, Fluid fluid, Direction direction) {
return false;
}

@Override
public int getTickRate(WorldView world) {
return ${mod.fluid.${%v}.tickrate};
}

@Override
protected float getBlastResistance() {
return 100f;
}

@Override
public boolean matchesType(Fluid fluid) {
return fluid == getStill() || fluid == getFlowing();
}

@Override
protected BlockState toBlockState(FluidState state) {
return ModFluids.${mod.fluid.${%v}^upper}_FLUID_BLOCK.getDefaultState().with(Properties.LEVEL_15, getBlockStateLevel(state));
}

@Override
public boolean isStill(FluidState state) {
return false;
}

@Override
public int getLevel(FluidState state) {
return 0;
}

public static class Flowing extends ${mod.fluid.${%v}^class}Fluid {
@Override
protected void appendProperties(StateManager.Builder<Fluid, FluidState> builder) {
super.appendProperties(builder);
builder.add(LEVEL);
}

@Override
public int getLevel(FluidState state) {
return state.get(LEVEL);
}

@Override
public boolean isStill(FluidState state) {
return false;
}
}

public static class Still extends ${mod.fluid.${%v}^class}Fluid {
@Override
public int getLevel(FluidState state) {
return 8;
}

@Override
public boolean isStill(FluidState state) {
return true;
}
}
}
36 changes: 36 additions & 0 deletions data/fabric1.20.1/registry/ModFluids.m3ecjava
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ${mod.package}.registry;

import ${mod.package}.${mod.class};
import ${mod.package}.fluids.*;
import net.fabricmc.fabric.api.block.v1.FabricBlock;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.block.FluidBlock;
import net.minecraft.fluid.FlowableFluid;
import net.minecraft.item.BucketItem;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;

public class ModFluids {
---iter mod.registry.fluid.names
public static final FlowableFluid STILL_${mod.fluid.$%v^upper} = Registry.register(Registries.FLUID,
new Identifier(TestRubyMod.MOD_ID, "${mod.fluid.$%v}"),
new ${mod.fluid.$%v^class}Fluid.Still());
public static final FlowableFluid FLOWING_${mod.fluid.$%v^upper} = Registry.register(Registries.FLUID,
new Identifier(TestRubyMod.MOD_ID, "flowing_${mod.fluid.$%v}"),
new ${mod.fluid.$%v^class}Fluid.Flowing());
public static final Block ${mod.fluid.$%v^upper}_FLUID_BLOCK = Registry.register(Registries.BLOCK,
new Identifier(TestRubyMod.MOD_ID, "${mod.fluid.$%v}_block"),
new FluidBlock(ModFluids.STILL_${mod.fluid.$%v^upper}, FabricBlockSettings.copyOf(${mod.fluid.$%v.copyblock})));
public static final Item ${mod.fluid.$%v^upper}_BUCKET = Registry.register(Registries.ITEM,
new Identifier(TestRubyMod.MOD_ID, "${mod.fluid.$%v}_bucket"),
new BucketItem(ModFluids.STILL_${mod.fluid.$%v^upper}, new FabricItemSettings().recipeRemainder(Items.BUCKET).maxCount(1)));
---end

public static void RegisterFluids() {}
}
4 changes: 2 additions & 2 deletions m3ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def interpret_args(argv):
if argv[0].lower() == "help":
print("""Usage:
project_path all|fabric|forge[gameversion]|fabric[gameversion]
gen|generate manifest|item|food|fuel|block|sapling|ore|(shaped|shapeless|smelting|stonecutting|smithing)recipe|armor[material]|tool[material] [output_file]
gen|generate manifest|item|food|fuel|block|fluid|sapling|ore|(shaped|shapeless|smelting|stonecutting|smithing)recipe|armor[material]|tool[material] [output_file]
""")
elif len(argv) > 1:
if argv[0].lower() in ("generate", "gen"):
Expand Down Expand Up @@ -145,7 +145,7 @@ def build(project_path, modenv):


def build_mod(modloader, version, modenv, manifest_dict):
content_types_list = ["item", "food", "fuel", "block", "ore", "recipe", "armor", "tool",
content_types_list = ["item", "food", "fuel", "block", "fluid", "ore", "recipe", "armor", "tool",
"armormaterial", "toolmaterial", "enchantment", "recipetype", "sapling"]
print(f"\n\
\n\
Expand Down
26 changes: 26 additions & 0 deletions testrubymod/fluids/molten_ruby.m3ec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@: fluid
fluid: SimpleFluid
contentid: molten_ruby
title: Molten Ruby
texture: molten_ruby.png

# block to copy properties from
copyblock: Blocks.LAVA

# update speed of the fluid?
# probably measured in ticks
tickrate: 5

# fluid level decrease per block spread
decreaseperblock: 1

# flow speed of the fluid.
# probably measured in ticks
flowspeed: 4

# Whether the fluid creates source blocks like water
isinfinite: false

# Hex color of fluid.
# 2 digits for each Red, Green, Blue, Alpha
color: 0xE02020D0
1 change: 1 addition & 0 deletions testrubymod/manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod:
#paths to mod items, blocks, recipes, etc
+.paths: items
+.paths: blocks
+.paths: fluids
+.paths: recipes
+.paths: foods
+.paths: armor
Expand Down
Binary file added testrubymod/textures/molten_ruby.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f66cdf3

Please sign in to comment.