RyanHub – file viewer
filename: common/src/main/java/rearth/oritech/block/blocks/augmenter/AugmentApplicationBlock.java
branch: 1.21
back to repo
package rearth.oritech.block.blocks.augmenter;

import com.mojang.serialization.MapCodec;
import dev.architectury.registry.menu.ExtendedMenuProvider;
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.block.enums.BlockFace;
import net.minecraft.entity.Entity;
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.server.world.ServerWorld;
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.Pair;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import rearth.oritech.OritechClient;
import rearth.oritech.block.entity.augmenter.AugmentApplicationEntity;
import rearth.oritech.client.ui.PlayerModifierScreenHandler;
import rearth.oritech.network.NetworkContent;
import rearth.oritech.util.Geometry;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static rearth.oritech.block.base.block.MultiblockMachine.ASSEMBLED;
import static rearth.oritech.util.TooltipHelper.addMachineTooltip;

public class AugmentApplicationBlock extends HorizontalFacingBlock implements BlockEntityProvider {
    
    private final VoxelShape[] HITBOXES = computeShapes();
    private final HashMap<PlayerEntity, Long> lastContact = new HashMap<>();
    
    public static Pair<Long, PlayerEntity> lastTeleportedPlayer;    // used to skip inv opening if a player just teleported in
    
    public AugmentApplicationBlock(Settings settings) {
        super(settings);
        setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH).with(ASSEMBLED, false));
    }
    
    @Override
    protected VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
        
        if (!state.get(ASSEMBLED)) {
            return super.getOutlineShape(state, world, pos, context);
        }
        
        var facing = state.get(Properties.HORIZONTAL_FACING);
        return HITBOXES[facing.ordinal()];
    }
    
    @Override
    protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
        builder.add(Properties.HORIZONTAL_FACING, ASSEMBLED);
    }
    
    private VoxelShape[] computeShapes() {
        
        var result = new VoxelShape[6];
        
        for (var facing : Properties.HORIZONTAL_FACING.getValues()) {
            
            result[facing.ordinal()] = VoxelShapes.union(
              Geometry.rotateVoxelShape(VoxelShapes.cuboid(0, 0, 0, 1, 2 / 16f, 1), facing, BlockFace.FLOOR),
              Geometry.rotateVoxelShape(VoxelShapes.cuboid(0, 3 / 16f, 14 / 16f, 1f, 1f, 1f), facing, BlockFace.FLOOR)
            );
        }
        
        return result;
        
    }
    
    @Nullable
    @Override
    public BlockState getPlacementState(ItemPlacementContext ctx) {
        return Objects.requireNonNull(super.getPlacementState(ctx)).with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite());
    }
    
    @Override
    protected BlockRenderType getRenderType(BlockState state) {
        return BlockRenderType.ENTITYBLOCK_ANIMATED;
    }
    
    @Override
    protected MapCodec<? extends HorizontalFacingBlock> getCodec() {
        return null;
    }
    
    @Override
    protected void onEntityCollision(BlockState state, World world, BlockPos pos, Entity entity) {
        
        if (world.isClient || !state.get(ASSEMBLED)) return;
        
        if (!(entity instanceof PlayerEntity player)) return;
        
        if (lastTeleportedPlayer != null) {
            var age = world.getTime() - lastTeleportedPlayer.getLeft();
            if (age < 20) {
                return;
            }
        }
        
        var centerPos = pos.toBottomCenterPos().add(0, 0.2, 0);
        
        var dist = entity.getPos().distanceTo(centerPos);
        
        if (dist < 0.45) {
            
            var ageWithoutContact = world.getTime() - lastContact.getOrDefault(player, 0L);
            
            var time = world.getTime();
            lastContact.put(player, time);
            
            if (ageWithoutContact > 15) {
                var locked = lockPlayer(player, centerPos, state);
                if (locked) {
                    var blockEntity = (AugmentApplicationEntity) world.getBlockEntity(pos);
                    blockEntity.loadAvailableStations(player);
                    
                    var handler = (ExtendedMenuProvider) world.getBlockEntity(pos);
                    MenuRegistry.openExtendedMenu((ServerPlayerEntity) player, handler);
                }
            }
        }
        
    }
    
    private boolean lockPlayer(PlayerEntity player, Vec3d lockPos, BlockState state) {
        
        var maxVelocity = Math.max(Math.max(Math.abs(player.getVelocity().x), Math.abs(player.getVelocity().y)), Math.abs(player.getVelocity().z));
        
        if (maxVelocity < 0.01 || player.currentScreenHandler instanceof PlayerModifierScreenHandler) return false;
        
        var facing = state.get(Properties.HORIZONTAL_FACING);
        var rotation = switch (facing) {
            case NORTH -> 180;
            case WEST -> 90;
            case EAST -> -90;
            default -> 0;
        };
        player.setVelocity(Vec3d.ZERO);
        player.teleport((ServerWorld) player.getWorld(), lockPos.x, lockPos.y, lockPos.z, Set.of(), rotation, 0);
        
        var dist = player.getPos().distanceTo(lockPos);
        
        return dist < 0.1;
    }
    
    @Override
    public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
        
        if (world.isClient)
            return ActionResult.SUCCESS;
        
        var entity = world.getBlockEntity(pos);
        if (!(entity instanceof AugmentApplicationEntity modifierEntity)) {
            return ActionResult.SUCCESS;
        }
        
        var wasAssembled = state.get(ASSEMBLED);
        
        if (!wasAssembled) {
            var corePlaced = modifierEntity.tryPlaceNextCore(player);
            if (corePlaced) return ActionResult.SUCCESS;
        }
        
        var isAssembled = modifierEntity.initMultiblock(state);
        
        // first time created
        if (isAssembled && !wasAssembled) {
            NetworkContent.MACHINE_CHANNEL.serverHandle(entity).send(new NetworkContent.MachineSetupEventPacket(pos));
            return ActionResult.SUCCESS;
        }
        
        if (!isAssembled) {
            player.sendMessage(Text.translatable("message.oritech.machine.missing_core"));
            return ActionResult.SUCCESS;
        }
        
        var blockEntity = (AugmentApplicationEntity) world.getBlockEntity(pos);
        blockEntity.loadAvailableStations(player);
        
        var handler = (ExtendedMenuProvider) world.getBlockEntity(pos);
        MenuRegistry.openExtendedMenu((ServerPlayerEntity) player, handler);
        
        return ActionResult.SUCCESS;
    }
    
    @Override
    public BlockState onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
        
        if (!world.isClient()) {
            
            var entity = world.getBlockEntity(pos);
            
            if (entity instanceof AugmentApplicationEntity storageBlock) {
                storageBlock.onControllerBroken();
                var stacks = storageBlock.inventory.heldStacks;
                for (var heldStack : stacks) {
                    if (!heldStack.isEmpty()) {
                        var itemEntity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), heldStack);
                        world.spawnEntity(itemEntity);
                    }
                }
            }
        }
        
        return super.onBreak(world, pos, state, player);
    }
    
    @Nullable
    @Override
    public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
        return new AugmentApplicationEntity(pos, state);
    }
    
    @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) {
        super.appendTooltip(stack, context, tooltip, options);
        var hotkey = OritechClient.AUGMENT_SELECTOR.boundKey.getLocalizedText();
        tooltip.add(Text.translatable("tooltip.oritech.augmenter.1").formatted(Formatting.GRAY));
        tooltip.add(Text.translatable("tooltip.oritech.augmenter.2", hotkey.getLiteralString()).formatted(Formatting.GRAY));
        addMachineTooltip(tooltip, this, this);
    }
}