Skip to content

Commit

Permalink
Adjusted determination of passable blocks for temperature filling
Browse files Browse the repository at this point in the history
  • Loading branch information
Adubbz committed Jan 7, 2024
1 parent 6bc81ea commit 8a92d2b
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public class TANBlockEntityTypes
{
public static BlockEntityType<?> WATER_PURIFIER;
public static BlockEntityType<?> TEMPERATURE_GAUGE;
public static BlockEntityType<?> THERMOREGULATOR;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down
54 changes: 49 additions & 5 deletions common/src/main/java/toughasnails/block/ThermoregulatorBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<WaterPurifierBlock> 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");
Expand All @@ -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<? extends BaseEntityBlock> codec()
{
return CODEC;
}

@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state)
{
return new ThermoregulatorBlockEntity(pos, state);
}

@Override
@Nullable
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type)
{
return level.isClientSide ? null : createTickerHelper(type, (BlockEntityType<ThermoregulatorBlockEntity>) TANBlockEntityTypes.THERMOREGULATOR, ThermoregulatorBlockEntity::serverTick);
}

public static ToIntFunction<BlockState> lightLevel(int level)
{
return (state) -> {
Expand Down Expand Up @@ -59,4 +86,21 @@ protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockSt
{
builder.add(FACING, COOLING, HEATING);
}

public static Effect getEffect(BlockState state)
{
boolean heating = state.getValue(HEATING);
boolean cooling = state.getValue(COOLING);

if (heating && cooling) return Effect.NEUTRALIZING;
else if (heating) return Effect.HEATING;
else return Effect.COOLING;
}

public enum Effect
{
HEATING,
COOLING,
NEUTRALIZING
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*******************************************************************************
* Copyright 2023, the Glitchfiend Team.
* All rights reserved.
******************************************************************************/
package toughasnails.block.entity;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import toughasnails.api.blockentity.TANBlockEntityTypes;

public class ThermoregulatorBlockEntity extends BlockEntity
{
public ThermoregulatorBlockEntity(BlockPos pos, BlockState state)
{
super(TANBlockEntityTypes.THERMOREGULATOR, pos, state);
}

public static void serverTick(Level level, BlockPos pos, BlockState state, ThermoregulatorBlockEntity blockEntity)
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.core.Direction;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
Expand Down Expand Up @@ -81,11 +82,13 @@ private static void populateConnectedBlocks(Player player)
@Override
public void onSolid(Level level, AreaFill.PosAndDepth pos)
{
if (TemperatureHelper.isHeating(level, pos.pos()))
BlockState state = level.getBlockState(pos.pos());

if (TemperatureHelper.isHeating(state))
{
heatingPositions.add(pos.pos());
}
else if (TemperatureHelper.isCooling(level, pos.pos()))
else if (TemperatureHelper.isCooling(state))
{
coolingPositions.add(pos.pos());
}
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/toughasnails/init/ModBlockEntities.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import toughasnails.api.block.TANBlocks;
import toughasnails.api.blockentity.TANBlockEntityTypes;
import toughasnails.block.entity.TemperatureGaugeBlockEntity;
import toughasnails.block.entity.ThermoregulatorBlockEntity;
import toughasnails.block.entity.WaterPurifierBlockEntity;

import java.util.function.BiConsumer;
Expand All @@ -26,6 +27,7 @@ public static void registerBlockEntities(BiConsumer<ResourceLocation, BlockEntit
{
TANBlockEntityTypes.WATER_PURIFIER = register(func, "water_purifier", BlockEntityType.Builder.of(WaterPurifierBlockEntity::new, TANBlocks.WATER_PURIFIER));
TANBlockEntityTypes.TEMPERATURE_GAUGE = register(func, "temperature_gauge", BlockEntityType.Builder.of(TemperatureGaugeBlockEntity::new, TANBlocks.TEMPERATURE_GAUGE));
TANBlockEntityTypes.THERMOREGULATOR = register(func, "thermoregulator", BlockEntityType.Builder.of(ThermoregulatorBlockEntity::new, TANBlocks.THERMOREGULATOR));
}

private static <T extends BlockEntity> BlockEntityType<?> register(BiConsumer<ResourceLocation, BlockEntityType<?>> func, String name, BlockEntityType.Builder<T> builder)
Expand Down
24 changes: 20 additions & 4 deletions common/src/main/java/toughasnails/temperature/AreaFill.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,15 +101,28 @@ private static void checkSolid(Set<PosAndDepth> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -224,11 +222,11 @@ private static void addHeatingOrCooling(Set<BlockPos> heating, Set<BlockPos> 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);
}
Expand Down

0 comments on commit 8a92d2b

Please sign in to comment.