filename:
common/src/main/java/rearth/oritech/block/blocks/interaction/ChargerBlock.java
branch:
1.21
back to repo
package rearth.oritech.block.blocks.interaction;
import com.mojang.serialization.MapCodec;
import dev.architectury.registry.menu.MenuRegistry;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityTicker;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.Properties;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import rearth.oritech.block.entity.interaction.ChargerBlockEntity;
import java.util.List;
import java.util.Objects;
public class ChargerBlock extends HorizontalFacingBlock implements BlockEntityProvider {
public ChargerBlock(Settings settings) {
super(settings);
setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(Properties.HORIZONTAL_FACING);
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return Objects.requireNonNull(super.getPlacementState(ctx)).with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite());
}
@Override
protected VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return Block.createCuboidShape(0, 0, 0, 16, 8, 16);
}
@Override
protected MapCodec<? extends HorizontalFacingBlock> getCodec() {
return null;
}
@Nullable
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new ChargerBlockEntity(pos, state);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
if (!world.isClient && world.getBlockEntity(pos) instanceof ChargerBlockEntity machine) {
MenuRegistry.openExtendedMenu((ServerPlayerEntity) player, machine);
}
return ActionResult.SUCCESS;
}
@Override
public BlockState onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
if (!world.isClient) {
var entity = (ChargerBlockEntity) world.getBlockEntity(pos);
var stacks = entity.inventory.heldStacks;
for (var stack : stacks) {
if (!stack.isEmpty()) {
var itemEntity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), stack);
world.spawnEntity(itemEntity);
}
}
}
return super.onBreak(world, pos, state, player);
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
return (world1, pos, state1, blockEntity) -> {
if (blockEntity instanceof BlockEntityTicker ticker)
ticker.tick(world1, pos, state1, blockEntity);
};
}
@Override
public void appendTooltip(ItemStack stack, Item.TooltipContext context, List<Text> tooltip, TooltipType options) {
var showExtra = Screen.hasControlDown();
if (!showExtra) {
tooltip.add(Text.translatable("tooltip.oritech.item_extra_info").formatted(Formatting.GRAY).formatted(Formatting.ITALIC));
} else {
tooltip.add(Text.translatable("tooltip.oritech.charger").formatted(Formatting.GRAY));
}
}
}