PrettyPipes/src/main/java/de/ellpeck/prettypipes/pressurizer/PressurizerBlock.java

82 lines
3.1 KiB
Java
Raw Normal View History

2020-10-13 18:11:40 +02:00
package de.ellpeck.prettypipes.pressurizer;
import de.ellpeck.prettypipes.Registry;
2020-10-13 18:11:40 +02:00
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
2021-12-02 16:55:04 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
2021-12-02 12:31:04 +01:00
import net.minecraft.world.item.ItemStack;
2021-12-02 16:55:04 +01:00
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
2021-12-02 16:55:04 +01:00
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.network.NetworkHooks;
2020-10-13 18:11:40 +02:00
import javax.annotation.Nullable;
import java.util.List;
2021-12-02 16:55:04 +01:00
public class PressurizerBlock extends BaseEntityBlock {
2020-10-13 18:11:40 +02:00
public PressurizerBlock() {
2021-12-02 16:55:04 +01:00
super(BlockBehaviour.Properties.of(Material.STONE).strength(3).sound(SoundType.STONE));
2020-10-13 18:11:40 +02:00
}
@Override
2021-12-02 16:55:04 +01:00
public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult result) {
2021-12-02 17:46:56 +01:00
var tile = Utility.getBlockEntity(PressurizerBlockEntity.class, worldIn, pos);
2020-10-13 18:11:40 +02:00
if (tile == null)
2021-12-02 16:55:04 +01:00
return InteractionResult.PASS;
if (!worldIn.isClientSide)
NetworkHooks.openGui((ServerPlayer) player, tile, pos);
return InteractionResult.SUCCESS;
2020-10-13 18:11:40 +02:00
}
2021-12-02 16:55:04 +01:00
@org.jetbrains.annotations.Nullable
2020-10-13 18:11:40 +02:00
@Override
2021-12-02 16:55:04 +01:00
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new PressurizerBlockEntity(pos, state);
2020-10-13 18:11:40 +02:00
}
@Override
2021-12-02 16:55:04 +01:00
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
2020-10-13 18:11:40 +02:00
}
@Override
2021-12-02 16:55:04 +01:00
public void appendHoverText(ItemStack stack, @Nullable BlockGetter worldIn, List<Component> tooltip, TooltipFlag flagIn) {
2020-10-13 18:11:40 +02:00
Utility.addTooltip(this.getRegistryName().getPath(), tooltip);
}
@org.jetbrains.annotations.Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
return level.isClientSide ? null : createTickerHelper(type, Registry.pressurizerBlockEntity, PressurizerBlockEntity::tick);
}
@Override
public boolean hasAnalogOutputSignal(BlockState state) {
return true;
}
@Override
public int getAnalogOutputSignal(BlockState state, Level world, BlockPos pos) {
var pipe = Utility.getBlockEntity(PressurizerBlockEntity.class, world, pos);
if (pipe == null)
return 0;
return (int) (pipe.getEnergy() / (float) pipe.getMaxEnergy() * 15);
}
2020-10-13 18:11:40 +02:00
}