Skip to content

Instantly share code, notes, and snippets.

@JamiesWhiteShirt
Created June 7, 2019 18:53
Show Gist options
  • Save JamiesWhiteShirt/885fb6d9cf8d34f0dc93485630e174fa to your computer and use it in GitHub Desktop.
Save JamiesWhiteShirt/885fb6d9cf8d34f0dc93485630e174fa to your computer and use it in GitHub Desktop.
package com.jamieswhiteshirt.redox.common;
import com.jamieswhiteshirt.redox.common.block.RedoxFluidBlock;
import com.jamieswhiteshirt.redox.common.fluid.RedoxFluid;
import com.jamieswhiteshirt.redox.common.fluid.RedoxFluids;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.fabricmc.fabric.api.block.FabricBlockSettings;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Material;
import net.minecraft.fluid.FluidState;
import net.minecraft.item.BucketItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.Items;
import net.minecraft.particle.ParticleParameters;
import net.minecraft.state.property.IntegerProperty;
import java.util.function.BiFunction;
import java.util.function.Function;
public class RedoxFluidDef {
private static final Int2ObjectMap<IntegerProperty> LEVEL_PROPERTIES = new Int2ObjectOpenHashMap<>();
private static final IntegerProperty getLevelProperty(int maxLevel) {
return LEVEL_PROPERTIES.computeIfAbsent(maxLevel, key -> IntegerProperty.create("level", 1, key));
}
private final int maxLevel;
private final int barLevel;
private final int tickRate;
private final ParticleParameters particleParameters;
private final IntegerProperty levelProperty;
private final RedoxFluid fluid;
private final RedoxFluidBlock block;
private final Item bucket;
public RedoxFluidDef(Settings settings) {
this.maxLevel = settings.maxLevel;
this.barLevel = settings.barLevel;
this.tickRate = settings.tickRate;
this.particleParameters = settings.particleParameters;
this.levelProperty = getLevelProperty(settings.maxLevel);
fluid = settings.fluidFactory.apply(this);
Block.Settings blockSettings = FabricBlockSettings.of(Material.WATER).noCollision().strength(100.0F, 100.0F).dropsNothing().build();
block = settings.blockFactory.apply(this, blockSettings);
bucket = new BucketItem(RedoxFluids.TEST, new Item.Settings().recipeRemainder(Items.BUCKET).stackSize(1).itemGroup(ItemGroup.MISC));
}
public int getMaxLevel() {
return maxLevel;
}
public int getBarLevel() {
return barLevel;
}
public int getTickRate() {
return tickRate;
}
public ParticleParameters getParticleParameters() {
return particleParameters;
}
public IntegerProperty getLevelProperty() {
return levelProperty;
}
public RedoxFluid getFluid() {
return fluid;
}
public RedoxFluidBlock getBlock() {
return block;
}
public Item getBucket() {
return bucket;
}
public BlockState getBlockState(int level) {
return block.getDefaultState().with(levelProperty, level);
}
public BlockState getBlockState(FluidState state) {
return getBlockState(state.get(levelProperty));
}
public FluidState getFluidState(int level) {
return fluid.getDefaultState().with(levelProperty, level);
}
public FluidState getFluidState(BlockState state) {
return getFluidState(state.get(levelProperty));
}
public static class Settings {
private Function<RedoxFluidDef, RedoxFluid> fluidFactory = RedoxFluid::new;
private BiFunction<RedoxFluidDef, Block.Settings, RedoxFluidBlock> blockFactory = RedoxFluidBlock::new;
private int maxLevel = 16;
private int barLevel = 16;
private int tickRate = 5;
private ParticleParameters particleParameters = null;
public Settings maxLevel(int maxLevel) {
this.maxLevel = maxLevel;
return this;
}
public Settings barLevel(int barLevel) {
this.barLevel = barLevel;
return this;
}
public Settings fluid(Function<RedoxFluidDef, RedoxFluid> fluidFactory) {
this.fluidFactory = fluidFactory;
return this;
}
public Settings block(BiFunction<RedoxFluidDef, Block.Settings, RedoxFluidBlock> blockFactory) {
this.blockFactory = blockFactory;
return this;
}
public Settings tickRate(int tickRate) {
this.tickRate = tickRate;
return this;
}
public Settings particleParameters(ParticleParameters particleParameters) {
this.particleParameters = particleParameters;
return this;
}
}
}
package com.jamieswhiteshirt.redox.common;
import com.jamieswhiteshirt.redox.common.fluid.TestFluid;
public class RedoxFluidDefs {
public static final RedoxFluidDef TEST = new RedoxFluidDef(new RedoxFluidDef.Settings().maxLevel(32).barLevel(16).fluid(TestFluid::new));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment