Skip to content

Commit

Permalink
Json Things support for burning fluids
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed May 20, 2024
1 parent 2fa33b0 commit 4dd9249
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package slimeknights.tconstruct.plugin.jsonthings;

import dev.gigaherz.jsonthings.things.IFlexBlock;
import dev.gigaherz.jsonthings.things.serializers.FlexBlockType;
import dev.gigaherz.jsonthings.things.serializers.IBlockSerializer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition.Builder;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.FlowingFluid;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.common.util.Lazy;
import slimeknights.mantle.data.loadable.Loadables;
import slimeknights.mantle.data.loadable.primitive.ResourceLocationLoadable;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.plugin.jsonthings.block.FlexBurningLiquidBlock;
import slimeknights.tconstruct.plugin.jsonthings.block.FlexMobEffectLiquidBlock;

import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

/** Collection of custom block types added by Tinkers */
public class FlexBlockTypes {
/** Creates the supplier for a fluid in a fluid block */
private static Supplier<FlowingFluid> fluidSupplier(ResourceLocation name) {
return Lazy.of(() -> {
// TODO: make Mantle loadables resource location
if (((ResourceLocationLoadable<Fluid>)Loadables.FLUID).fromKey(name, "fluid") instanceof FlowingFluid flowing) {
return flowing;
} else {
throw new RuntimeException("LiquidBlock requires a flowing fluid");
}
});
}

/** Initializes the block types */
public static void init() {
register("burning_liquid", data -> {
ResourceLocation fluidField = Loadables.RESOURCE_LOCATION.getOrDefault(data, "fluid", null);
int burnTime = GsonHelper.getAsInt(data, "burn_time");
float damage = GsonHelper.getAsFloat(data, "damage");
return (props, builder) -> {
final List<Property<?>> _properties = builder.getProperties();
return new FlexBurningLiquidBlock(props, builder.getPropertyDefaultValues(), fluidSupplier(Objects.requireNonNullElse(fluidField, builder.getRegistryName())), burnTime, damage) {
@Override
protected void createBlockStateDefinition(Builder<Block,BlockState> stateBuilder) {
super.createBlockStateDefinition(stateBuilder);
_properties.forEach(stateBuilder::add);
}
};
};
}, Material.LAVA);
register("mob_effect_liquid", data -> {
ResourceLocation fluidField = Loadables.RESOURCE_LOCATION.getOrDefault(data, "fluid", null);
ResourceLocation effectName = Loadables.RESOURCE_LOCATION.getIfPresent(data, "effect");
int effectLevel = GsonHelper.getAsInt(data, "burn_time");
return (props, builder) -> {
final List<Property<?>> _properties = builder.getProperties();
Lazy<MobEffect> effect = Lazy.of(() -> ((ResourceLocationLoadable<MobEffect>)Loadables.MOB_EFFECT).fromKey(effectName, "effect"));
return new FlexMobEffectLiquidBlock(props, builder.getPropertyDefaultValues(), fluidSupplier(Objects.requireNonNullElse(fluidField, builder.getRegistryName())), () -> new MobEffectInstance(effect.get(), 5*20, effectLevel - 1)) {
@Override
protected void createBlockStateDefinition(Builder<Block,BlockState> stateBuilder) {
super.createBlockStateDefinition(stateBuilder);
_properties.forEach(stateBuilder::add);
}
};
};
}, Material.WATER);
}

/** Local helper to register our stuff */
private static <T extends Block & IFlexBlock> void register(String name, IBlockSerializer<T> factory, Material defaultMaterial) {
FlexBlockType.register(TConstruct.resourceString(name), factory, "translucent", true, defaultMaterial);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class JsonThingsPlugin {
/** Called by mod constructor to register JsonThings things */
public static void onConstruct() {
FlexBlockTypes.init();
FlexItemTypes.init();

if (FMLEnvironment.dist == Dist.CLIENT) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package slimeknights.tconstruct.plugin.jsonthings.block;

import dev.gigaherz.jsonthings.things.blocks.FlexLiquidBlock;
import net.minecraft.core.BlockPos;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.FlowingFluid;

import java.util.Map;
import java.util.function.Supplier;

/** Json Things version of {@link slimeknights.tconstruct.fluids.block.BurningLiquidBlock} */
public class FlexBurningLiquidBlock extends FlexLiquidBlock {
private final int burnTime;
private final float damage;
public FlexBurningLiquidBlock(Properties properties, Map<Property<?>,Comparable<?>> propertyDefaultValues, Supplier<FlowingFluid> fluidSupplier, int burnTime, float damage) {
super(properties, propertyDefaultValues, fluidSupplier);
this.burnTime = burnTime;
this.damage = damage;
}

@Override
public void entityInside(BlockState state, Level level, BlockPos pos, Entity entity) {
if (!entity.fireImmune() && entity.getFluidTypeHeight(getFluid().getFluidType()) > 0) {
entity.setSecondsOnFire(burnTime);
if (entity.hurt(DamageSource.LAVA, damage)) {
entity.playSound(SoundEvents.GENERIC_BURN, 0.4F, 2.0F + level.random.nextFloat() * 0.4F);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package slimeknights.tconstruct.plugin.jsonthings.block;

import dev.gigaherz.jsonthings.things.blocks.FlexLiquidBlock;
import net.minecraft.core.BlockPos;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.FlowingFluid;

import java.util.ArrayList;
import java.util.Map;
import java.util.function.Supplier;

/** Json Things version of {@link slimeknights.tconstruct.fluids.block.BurningLiquidBlock} */
public class FlexMobEffectLiquidBlock extends FlexLiquidBlock {
private final Supplier<MobEffectInstance> effect;
public FlexMobEffectLiquidBlock(Properties properties, Map<Property<?>,Comparable<?>> propertyDefaultValues, Supplier<FlowingFluid> fluidSupplier, Supplier<MobEffectInstance> effect) {
super(properties, propertyDefaultValues, fluidSupplier);
this.effect = effect;
}

@Override
public void entityInside(BlockState state, Level level, BlockPos pos, Entity entity) {
if (entity.getFluidTypeHeight(getFluid().getFluidType()) > 0 && entity instanceof LivingEntity living) {
MobEffectInstance effect = this.effect.get();
effect.setCurativeItems(new ArrayList<>());
living.addEffect(effect);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
package slimeknights.tconstruct.plugin.jsonthings.block;

import net.minecraft.MethodsReturnNonnullByDefault;

import javax.annotation.ParametersAreNonnullByDefault;

0 comments on commit 4dd9249

Please sign in to comment.