From 8a92d2b9baa2eb3572513c374568af02f71118ce Mon Sep 17 00:00:00 2001 From: Adubbz Date: Sun, 7 Jan 2024 12:29:24 +1100 Subject: [PATCH] Adjusted determination of passable blocks for temperature filling --- .../api/blockentity/TANBlockEntityTypes.java | 1 + .../api/temperature/TemperatureHelper.java | 23 ++++---- .../block/ThermoregulatorBlock.java | 54 +++++++++++++++++-- .../entity/ThermoregulatorBlockEntity.java | 24 +++++++++ .../client/handler/LevelRenderHandler.java | 7 ++- .../toughasnails/init/ModBlockEntities.java | 2 + .../toughasnails/temperature/AreaFill.java | 24 +++++++-- .../temperature/TemperatureHelperImpl.java | 10 ++-- 8 files changed, 116 insertions(+), 29 deletions(-) create mode 100644 common/src/main/java/toughasnails/block/entity/ThermoregulatorBlockEntity.java diff --git a/common/src/main/java/toughasnails/api/blockentity/TANBlockEntityTypes.java b/common/src/main/java/toughasnails/api/blockentity/TANBlockEntityTypes.java index d8299061..73c7fa98 100644 --- a/common/src/main/java/toughasnails/api/blockentity/TANBlockEntityTypes.java +++ b/common/src/main/java/toughasnails/api/blockentity/TANBlockEntityTypes.java @@ -10,4 +10,5 @@ public class TANBlockEntityTypes { public static BlockEntityType WATER_PURIFIER; public static BlockEntityType TEMPERATURE_GAUGE; + public static BlockEntityType THERMOREGULATOR; } diff --git a/common/src/main/java/toughasnails/api/temperature/TemperatureHelper.java b/common/src/main/java/toughasnails/api/temperature/TemperatureHelper.java index 58497875..ec466a04 100644 --- a/common/src/main/java/toughasnails/api/temperature/TemperatureHelper.java +++ b/common/src/main/java/toughasnails/api/temperature/TemperatureHelper.java @@ -7,6 +7,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.state.BlockState; public class TemperatureHelper { @@ -91,25 +92,23 @@ public static int getTicksHyperthermic(Player player) } /** - * Gets whether a position is heating. - * @param level the level. - * @param pos the position to check. + * Gets whether a state is heating. + * @param state the state to check. * @return is heating. */ - public static boolean isHeating(Level level, BlockPos pos) + public static boolean isHeating(BlockState state) { - return Impl.INSTANCE.isHeating(level, pos); + return Impl.INSTANCE.isHeating(state); } /** - * Gets whether a position is cooling. - * @param level the level. - * @param pos the position to check. + * Gets whether a state is cooling. + * @param state the state to check. * @return is cooling. */ - public static boolean isCooling(Level level, BlockPos pos) + public static boolean isCooling(BlockState state) { - return Impl.INSTANCE.isCooling(level, pos); + return Impl.INSTANCE.isCooling(state); } /** @@ -162,8 +161,8 @@ public interface ITemperatureHelper boolean isFullyHyperthermic(Player player); int getTicksRequiredForHyperthermia(); int getTicksHyperthermic(Player player); - boolean isHeating(Level level, BlockPos pos); - boolean isCooling(Level level, BlockPos pos); + boolean isHeating(BlockState state); + boolean isCooling(BlockState state); void registerPlayerTemperatureModifier(IPlayerTemperatureModifier modifier); void registerPositionalTemperatureModifier(IPositionalTemperatureModifier modifier); diff --git a/common/src/main/java/toughasnails/block/ThermoregulatorBlock.java b/common/src/main/java/toughasnails/block/ThermoregulatorBlock.java index f9514737..5c565cf8 100644 --- a/common/src/main/java/toughasnails/block/ThermoregulatorBlock.java +++ b/common/src/main/java/toughasnails/block/ThermoregulatorBlock.java @@ -4,21 +4,29 @@ ******************************************************************************/ package toughasnails.block; +import com.mojang.serialization.MapCodec; +import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.world.item.context.BlockPlaceContext; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.HorizontalDirectionalBlock; -import net.minecraft.world.level.block.Mirror; -import net.minecraft.world.level.block.Rotation; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.*; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityTicker; +import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.level.block.state.properties.DirectionProperty; +import toughasnails.api.blockentity.TANBlockEntityTypes; +import toughasnails.block.entity.ThermoregulatorBlockEntity; +import toughasnails.block.entity.WaterPurifierBlockEntity; +import javax.annotation.Nullable; import java.util.function.ToIntFunction; -public class ThermoregulatorBlock extends Block +public class ThermoregulatorBlock extends BaseEntityBlock { + public static final MapCodec CODEC = simpleCodec(WaterPurifierBlock::new); public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING; public static final BooleanProperty COOLING = BooleanProperty.create("cooling"); public static final BooleanProperty HEATING = BooleanProperty.create("heating"); @@ -29,6 +37,25 @@ public ThermoregulatorBlock(Properties properties) this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(COOLING, false).setValue(HEATING, false)); } + @Override + protected MapCodec codec() + { + return CODEC; + } + + @Override + public BlockEntity newBlockEntity(BlockPos pos, BlockState state) + { + return new ThermoregulatorBlockEntity(pos, state); + } + + @Override + @Nullable + public BlockEntityTicker getTicker(Level level, BlockState state, BlockEntityType type) + { + return level.isClientSide ? null : createTickerHelper(type, (BlockEntityType) TANBlockEntityTypes.THERMOREGULATOR, ThermoregulatorBlockEntity::serverTick); + } + public static ToIntFunction lightLevel(int level) { return (state) -> { @@ -59,4 +86,21 @@ protected void createBlockStateDefinition(StateDefinition.Builder BlockEntityType register(BiConsumer> func, String name, BlockEntityType.Builder builder) diff --git a/common/src/main/java/toughasnails/temperature/AreaFill.java b/common/src/main/java/toughasnails/temperature/AreaFill.java index 4e12d5bf..631026da 100644 --- a/common/src/main/java/toughasnails/temperature/AreaFill.java +++ b/common/src/main/java/toughasnails/temperature/AreaFill.java @@ -6,11 +6,14 @@ import com.google.common.collect.Sets; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.state.BlockState; +import toughasnails.api.temperature.TemperatureHelper; import toughasnails.core.ToughAsNails; import toughasnails.init.ModConfig; +import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.Set; @@ -98,15 +101,28 @@ private static void checkSolid(Set checked, PositionChecker checker private static boolean checkPassable(PositionChecker checker, Level level, PosAndDepth pos) { BlockState state = level.getBlockState(pos.pos()); - boolean passable = state.isAir() || (!state.isSolid() && !state.liquid()); + boolean passable = checker.isPassable(level, pos.pos()); if (passable) checker.onPassable(level, pos); return passable; } - public interface PositionChecker - { + public interface PositionChecker { void onSolid(Level level, PosAndDepth pos); - default void onPassable(Level level, PosAndDepth pos) {} + + default void onPassable(Level level, PosAndDepth pos) { + } + + default boolean isPassable(Level level, BlockPos pos) + { + BlockState state = level.getBlockState(pos); + return state.isAir() || (!isFullySolid(level, pos) && !TemperatureHelper.isHeating(state) && !TemperatureHelper.isCooling(state)); + } + + default boolean isFullySolid(Level level, BlockPos pos) + { + BlockState state = level.getBlockState(pos); + return Arrays.stream(Direction.values()).allMatch(dir -> state.isFaceSturdy(level, pos, dir)); + } } public record PosAndDepth(BlockPos pos, int depth) diff --git a/common/src/main/java/toughasnails/temperature/TemperatureHelperImpl.java b/common/src/main/java/toughasnails/temperature/TemperatureHelperImpl.java index 211bb515..9e6ce135 100644 --- a/common/src/main/java/toughasnails/temperature/TemperatureHelperImpl.java +++ b/common/src/main/java/toughasnails/temperature/TemperatureHelperImpl.java @@ -109,16 +109,14 @@ public int getTicksHyperthermic(Player player) } @Override - public boolean isHeating(Level level, BlockPos pos) + public boolean isHeating(BlockState state) { - BlockState state = level.getBlockState(pos); return state.is(ModTags.Blocks.HEATING_BLOCKS) && (!state.hasProperty(CampfireBlock.LIT) || state.getValue(CampfireBlock.LIT) || !state.hasProperty(CopperBulbBlock.POWERED) || state.getValue(CopperBulbBlock.POWERED)); } @Override - public boolean isCooling(Level level, BlockPos pos) + public boolean isCooling(BlockState state) { - BlockState state = level.getBlockState(pos); return state.is(ModTags.Blocks.COOLING_BLOCKS) && (!state.hasProperty(CampfireBlock.LIT) || state.getValue(CampfireBlock.LIT) || !state.hasProperty(CopperBulbBlock.POWERED) || state.getValue(CopperBulbBlock.POWERED)); } @@ -224,11 +222,11 @@ private static void addHeatingOrCooling(Set heating, Set coo { BlockState state = level.getBlockState(pos); - if (TemperatureHelper.isHeating(level, pos)) + if (TemperatureHelper.isHeating(state)) { heating.add(pos); } - else if (TemperatureHelper.isCooling(level, pos)) + else if (TemperatureHelper.isCooling(state)) { cooling.add(pos); }