Skip to content

Commit

Permalink
Temperature gauge is now functional
Browse files Browse the repository at this point in the history
  • Loading branch information
Adubbz committed Jan 4, 2024
1 parent be8a3a8 commit 1ca0037
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
public class TANBlockEntityTypes
{
public static BlockEntityType<?> WATER_PURIFIER;
public static BlockEntityType<?> TEMPERATURE_GAUGE;
}
90 changes: 85 additions & 5 deletions common/src/main/java/toughasnails/block/TemperatureGaugeBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,58 @@
******************************************************************************/
package toughasnails.block;

import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.DaylightDetectorBlock;
import net.minecraft.world.level.block.RenderShape;
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.entity.DaylightDetectorBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import toughasnails.api.blockentity.TANBlockEntityTypes;
import toughasnails.api.temperature.TemperatureHelper;
import toughasnails.api.temperature.TemperatureLevel;
import toughasnails.block.entity.TemperatureGaugeBlockEntity;
import toughasnails.temperature.TemperatureHelperImpl;

public class TemperatureGaugeBlock extends Block
import javax.annotation.Nullable;

public class TemperatureGaugeBlock extends BaseEntityBlock
{
public static final MapCodec<TemperatureGaugeBlock> CODEC = simpleCodec(TemperatureGaugeBlock::new);
protected static final VoxelShape SHAPE = Block.box(0.0D, 0.0D, 0.0D, 16.0D, 6.0D, 16.0D);
public static final IntegerProperty POWER = BlockStateProperties.POWER;
public static final BooleanProperty INVERTED = BlockStateProperties.INVERTED;

@Override
public MapCodec<TemperatureGaugeBlock> codec()
{
return CODEC;
}

public TemperatureGaugeBlock(Properties properties)
{
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(INVERTED, false));
this.registerDefaultState(this.stateDefinition.any().setValue(POWER, Integer.valueOf(0)).setValue(INVERTED, false));
}

@Override
Expand All @@ -45,8 +72,7 @@ public InteractionResult use(BlockState state, Level world, BlockPos pos, Player
BlockState blockstate = (BlockState)state.cycle(INVERTED);
world.setBlock(pos, blockstate, 2);
world.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(player, blockstate));
// TODO: REPLACE WITH SIGNAL STRENGTH UPDATE
// updateSignalStrength(blockstate, world, pos);
updateSignalStrength(blockstate, world, pos);
return InteractionResult.CONSUME;
}
}
Expand All @@ -62,15 +88,69 @@ public VoxelShape getShape(BlockState state, BlockGetter reader, BlockPos pos, C
return SHAPE;
}

@Override
public RenderShape getRenderShape(BlockState state)
{
return RenderShape.MODEL;
}

@Override
public boolean useShapeForLightOcclusion(BlockState state)
{
return true;
}

@Override
public boolean isSignalSource(BlockState state)
{
return true;
}

@Override
public int getSignal(BlockState state, BlockGetter getter, BlockPos pos, Direction direction)
{
return state.getValue(POWER);
}

@Override
public BlockEntity newBlockEntity(BlockPos $$0, BlockState $$1) {
return new TemperatureGaugeBlockEntity($$0, $$1);
}

@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type)
{
return !level.isClientSide
? createTickerHelper(type, (BlockEntityType<TemperatureGaugeBlockEntity>)TANBlockEntityTypes.TEMPERATURE_GAUGE, TemperatureGaugeBlock::tickEntity)
: null;
}

private static void tickEntity(Level level, BlockPos pos, BlockState state, TemperatureGaugeBlockEntity blockEntity)
{
if (level.getGameTime() % 20L == 0L) {
updateSignalStrength(state, level, pos);
}
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder)
{
builder.add(INVERTED);
builder.add(POWER, INVERTED);
}

private static void updateSignalStrength(BlockState state, Level level, BlockPos pos)
{
TemperatureLevel temperatureLevel = TemperatureHelperImpl.getTemperatureAtPosWithoutProximity(level, pos);
int strength = Mth.floor(15F * (float)temperatureLevel.ordinal() / (float)(TemperatureLevel.values().length - 1));

if (!state.getValue(INVERTED)) {
strength = 15 - strength;
}

strength = Mth.clamp(strength, 0, 15);
if (state.getValue(POWER) != strength) {
level.setBlock(pos, state.setValue(POWER, strength), 3);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*******************************************************************************
* Copyright 2023, the Glitchfiend Team.
* All rights reserved.
******************************************************************************/
package toughasnails.block.entity;

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

public class TemperatureGaugeBlockEntity extends BlockEntity
{
public TemperatureGaugeBlockEntity(BlockPos pos, BlockState state) {
super(TANBlockEntityTypes.TEMPERATURE_GAUGE, pos, state);
}
}
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 @@ -15,6 +15,7 @@
import toughasnails.api.TANAPI;
import toughasnails.api.block.TANBlocks;
import toughasnails.api.blockentity.TANBlockEntityTypes;
import toughasnails.block.entity.TemperatureGaugeBlockEntity;
import toughasnails.block.entity.WaterPurifierBlockEntity;

import java.util.function.BiConsumer;
Expand All @@ -24,6 +25,7 @@ public class ModBlockEntities
public static void registerBlockEntities(BiConsumer<ResourceLocation, BlockEntityType<?>> func)
{
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));
}

private static <T extends BlockEntity> BlockEntityType<?> register(BiConsumer<ResourceLocation, BlockEntityType<?>> func, String name, BlockEntityType.Builder<T> builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ public TemperatureLevel getTemperatureAtPos(Level level, BlockPos pos)
return proximityModifier(level, pos, temperature);
}

public static TemperatureLevel getTemperatureAtPosWithoutProximity(Level level, BlockPos pos)
{
TemperatureLevel temperature = getBiomeTemperatureLevel(level, pos);

for (IPositionalTemperatureModifier modifier : positionalModifiers)
{
temperature = modifier.modify(level, pos, temperature);
}

return nightModifier(level, pos, temperature);
}

@Override
public ITemperature getPlayerTemperature(Player player)
{
Expand Down

0 comments on commit 1ca0037

Please sign in to comment.