Search in sources :

Example 66 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Cyclic by Lothrazar.

the class EventRender method onRenderWorldLast.

@SubscribeEvent
public void onRenderWorldLast(RenderLevelLastEvent event) {
    Player player = Minecraft.getInstance().player;
    if (player == null) {
        return;
    }
    Level world = player.level;
    double range = 6F;
    float alpha = 0.125F * 2;
    Map<BlockPos, Color> renderCubes = new HashMap<>();
    // /////////////////// BuilderItem
    ItemStack stack = BuilderItem.getIfHeld(player);
    if (stack.getItem() instanceof BuilderItem) {
        BlockHitResult lookingAt = (BlockHitResult) player.pick(range, 0F, false);
        if (!world.isEmptyBlock(lookingAt.getBlockPos())) {
            BlockPos pos = lookingAt.getBlockPos();
            BuildStyle buildStyle = ((BuilderItem) stack.getItem()).style;
            if (buildStyle.isOffset() && lookingAt.getDirection() != null) {
                pos = pos.relative(lookingAt.getDirection());
            }
            alpha = 0.4F;
            // now the item has a build area
            List<BlockPos> coordinates = PacketSwapBlock.getSelectedBlocks(world, pos, BuilderItem.getActionType(stack), lookingAt.getDirection(), buildStyle);
            for (BlockPos coordinate : coordinates) {
                renderCubes.put(coordinate, ClientConfigCyclic.getColor(stack));
            }
        }
    }
    // /////////////////// RandomizerItem
    stack = RandomizerItem.getIfHeld(player);
    if (stack.getItem() instanceof RandomizerItem) {
        BlockHitResult lookingAt = UtilRender.getLookingAt(player, (int) range);
        if (player.level.getBlockState(lookingAt.getBlockPos()) == Blocks.AIR.defaultBlockState()) {
            return;
        }
        List<BlockPos> coords = RandomizerItem.getPlaces(lookingAt.getBlockPos(), lookingAt.getDirection());
        for (BlockPos e : coords) {
            renderCubes.put(e, RandomizerItem.canMove(player.level.getBlockState(e), player.level, e) ? ClientConfigCyclic.getColor(stack) : Color.RED);
        }
    }
    stack = OreProspector.getIfHeld(player);
    if (stack.getItem() instanceof OreProspector) {
        List<BlockPosDim> coords = OreProspector.getPosition(stack);
        for (BlockPosDim loc : coords) {
            if (loc != null) {
                if (loc.getDimension() == null || loc.getDimension().equalsIgnoreCase(UtilWorld.dimensionToString(world))) {
                    UtilRender.createBox(event.getPoseStack(), loc.getPos());
                }
            }
        }
    }
    // /////////////////// LocationGpsItem
    stack = player.getMainHandItem();
    if (stack.getItem() instanceof LocationGpsCard) {
        BlockPosDim loc = LocationGpsCard.getPosition(stack);
        if (loc != null) {
            if (loc.getDimension() == null || loc.getDimension().equalsIgnoreCase(UtilWorld.dimensionToString(world))) {
                renderCubes.put(loc.getPos(), ClientConfigCyclic.getColor(stack));
            }
        }
    }
    // /////////////////////////////////////ShapeCard
    if (stack.getItem() instanceof ShapeCard) {
        RelativeShape shape = RelativeShape.read(stack);
        if (shape != null) {
            BlockPos here = player.blockPosition();
            for (BlockPos s : shape.getShape()) {
                renderCubes.put(here.offset(s), ClientConfigCyclic.getColor(stack));
            }
        }
    }
    // render the pos->colour map
    if (renderCubes.keySet().size() > 0) {
        UtilRender.renderColourCubes(event, renderCubes, alpha);
    }
}
Also used : LocationGpsCard(com.lothrazar.cyclic.item.datacard.LocationGpsCard) Player(net.minecraft.world.entity.player.Player) UtilPlayer(com.lothrazar.cyclic.util.UtilPlayer) BlockPosDim(com.lothrazar.cyclic.data.BlockPosDim) RelativeShape(com.lothrazar.cyclic.data.RelativeShape) HashMap(java.util.HashMap) ShapeCard(com.lothrazar.cyclic.item.datacard.ShapeCard) Color(java.awt.Color) BuilderItem(com.lothrazar.cyclic.item.builder.BuilderItem) OreProspector(com.lothrazar.cyclic.item.OreProspector) RandomizerItem(com.lothrazar.cyclic.item.random.RandomizerItem) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) BuildStyle(com.lothrazar.cyclic.item.builder.BuildStyle) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 67 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Cyclic by Lothrazar.

the class TeleporterWandItem method releaseUsing.

@Override
public void releaseUsing(ItemStack stack, Level world, LivingEntity entity, int chargeTimer) {
    if (!(entity instanceof Player)) {
        return;
    }
    float percentageCharged = getChargedPercent(stack, chargeTimer);
    if (percentageCharged >= 0.98) {
        // full charge with a bit of buffer room
        Player player = (Player) entity;
        HitResult trace = player.pick(RANGE.get(), 0, true);
        if (trace.getType() == HitResult.Type.BLOCK) {
            BlockHitResult blockRayTraceResult = (BlockHitResult) trace;
            Direction face = blockRayTraceResult.getDirection();
            BlockPos newPos = blockRayTraceResult.getBlockPos().relative(face);
            BlockPos oldPos = player.blockPosition();
            if (UtilEntity.enderTeleportEvent(player, world, newPos)) {
                // && player.getPosition() != currentPlayerPos
                UtilItemStack.damageItem(player, stack);
                if (world.isClientSide) {
                    UtilParticle.spawnParticleBeam(world, ParticleTypes.PORTAL, oldPos, newPos, RANGE.get());
                    UtilSound.playSound(player, SoundRegistry.WARP_ECHO);
                }
            }
        }
    }
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) Player(net.minecraft.world.entity.player.Player) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Direction(net.minecraft.core.Direction)

Example 68 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Cyclic by Lothrazar.

the class MattockItem method onBlockStartBreak.

@Override
public boolean onBlockStartBreak(ItemStack stack, BlockPos pos, Player player) {
    Level world = player.level;
    // this.getTier()
    HitResult ray = getPlayerPOVHitResult(world, player, ClipContext.Fluid.NONE);
    int yoff = 0;
    if (radius == 2 && player.isCrouching()) {
        yoff = 1;
    }
    if (ray != null && ray.getType() == HitResult.Type.BLOCK) {
        BlockHitResult brt = (BlockHitResult) ray;
        Direction sideHit = brt.getDirection();
        List<BlockPos> shape;
        if (sideHit == Direction.UP || sideHit == Direction.DOWN) {
            shape = UtilShape.squareHorizontalHollow(pos, radius);
            if (radius == 2) {
                shape.addAll(UtilShape.squareHorizontalHollow(pos, radius - 1));
            }
        } else if (sideHit == Direction.EAST || sideHit == Direction.WEST) {
            int y = 1 + radius - yoff;
            int z = radius;
            shape = UtilShape.squareVerticalZ(pos, y, z);
        } else {
            // has to be NORTHSOUTH
            int x = radius;
            int y = 1 + radius - yoff;
            shape = UtilShape.squareVerticalX(pos, x, y);
        }
        for (BlockPos posCurrent : shape) {
            BlockState bsCurrent = world.getBlockState(posCurrent);
            if (bsCurrent.isAir()) {
                continue;
            }
            if (// -1 is unbreakable
            bsCurrent.destroySpeed >= 0 && player.mayUseItemAt(posCurrent, sideHit, stack) && ForgeEventFactory.doPlayerHarvestCheck(player, bsCurrent, true) && this.getDestroySpeed(stack, bsCurrent) > 1 && (bsCurrent.canHarvestBlock(world, pos, player) || bsCurrent.getBlock().builtInRegistryHolder().is(this.getTier().getTag()))) // end of OR
            {
                stack.mineBlock(world, bsCurrent, posCurrent, player);
                Block blockCurrent = bsCurrent.getBlock();
                if (world.isClientSide) {
                    world.levelEvent(2001, posCurrent, Block.getId(bsCurrent));
                    // removedByPlayer
                    if (blockCurrent.onDestroyedByPlayer(bsCurrent, world, posCurrent, player, true, bsCurrent.getFluidState())) {
                        blockCurrent.destroy(world, posCurrent, bsCurrent);
                    }
                // stack.onBlockDestroyed(world, bsCurrent, posCurrent, player);//update tool damage
                } else if (player instanceof ServerPlayer) {
                    // Server side, so this works
                    ServerPlayer mp = (ServerPlayer) player;
                    int xpGivenOnDrop = ForgeHooks.onBlockBreakEvent(world, ((ServerPlayer) player).gameMode.getGameModeForPlayer(), (ServerPlayer) player, posCurrent);
                    if (xpGivenOnDrop >= 0) {
                        if (blockCurrent.onDestroyedByPlayer(bsCurrent, world, posCurrent, player, true, bsCurrent.getFluidState()) && world instanceof ServerLevel) {
                            BlockEntity tile = world.getBlockEntity(posCurrent);
                            blockCurrent.destroy(world, posCurrent, bsCurrent);
                            blockCurrent.playerDestroy(world, player, posCurrent, bsCurrent, tile, stack);
                            blockCurrent.popExperience((ServerLevel) world, posCurrent, xpGivenOnDrop);
                        }
                        mp.connection.send(new ClientboundBlockUpdatePacket(world, posCurrent));
                    }
                }
            }
        }
    }
    return super.onBlockStartBreak(stack, pos, player);
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) Direction(net.minecraft.core.Direction) BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) ClientboundBlockUpdatePacket(net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket) BlockState(net.minecraft.world.level.block.state.BlockState) ServerPlayer(net.minecraft.server.level.ServerPlayer) Block(net.minecraft.world.level.block.Block) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 69 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Cyclic by Lothrazar.

the class FireEntity method onHit.

@Override
protected void onHit(HitResult result) {
    HitResult.Type type = result.getType();
    if (type == HitResult.Type.ENTITY) {
        // damage entity by zero
        // drop torch
        EntityHitResult entityRayTrace = (EntityHitResult) result;
        Entity target = entityRayTrace.getEntity();
        if (target.isAlive()) {
            target.hurt(DamageSource.thrown(this, this.getOwner()), Mth.nextInt(level.random, 2, 6));
            if (!target.level.isClientSide && target.isOnFire() == false && target instanceof LivingEntity) {
                target.hurt(DamageSource.IN_FIRE, Mth.nextInt(level.random, 3, 5));
                LivingEntity living = (LivingEntity) target;
                living.addEffect(new MobEffectInstance(PotionRegistry.PotionEffects.STUN, Const.TICKS_PER_SEC * 4, 1));
                living.setSecondsOnFire(Mth.nextInt(level.random, 1, 5));
            }
        }
    } else if (type == HitResult.Type.BLOCK) {
        BlockHitResult ray = (BlockHitResult) result;
        if (ray.getBlockPos() == null) {
            return;
        }
    // BlockPos pos = ray.getPos();//.offset(ray.getFace());
    // Block blockHere = world.getBlockState(pos).getBlock();
    // if (blockHere == Blocks.SNOW
    // || blockHere == Blocks.SNOW_BLOCK
    // || blockHere == Blocks.SNOW
    // || blockHere == Blocks.ICE) {
    // this.world.setBlockState(pos, Blocks.AIR.getDefaultState());
    // }
    }
    this.remove(RemovalReason.DISCARDED);
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) EntityHitResult(net.minecraft.world.phys.EntityHitResult) HitResult(net.minecraft.world.phys.HitResult) LivingEntity(net.minecraft.world.entity.LivingEntity) LivingEntity(net.minecraft.world.entity.LivingEntity) Entity(net.minecraft.world.entity.Entity) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) BlockHitResult(net.minecraft.world.phys.BlockHitResult) EntityHitResult(net.minecraft.world.phys.EntityHitResult)

Example 70 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Electrona-Project by Max094Reikeb.

the class NuclearBomb method use.

@Override
public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult hit) {
    if (!worldIn.isClientSide) {
        BlockEntity tile = worldIn.getBlockEntity(pos);
        if (player.isCrouching() && player.getItemInHand(handIn).isEmpty()) {
            player.setItemInHand(handIn, new ItemStack(this, 1));
            worldIn.removeBlock(pos, true);
            return InteractionResult.SUCCESS;
        } else if (player.getItemInHand(handIn).getItem() == Items.FLINT_AND_STEEL) {
            if (tile instanceof TileNuclearBomb) {
                if (((TileNuclearBomb) tile).isCharged() && worldIn.getLevelData().getGameRules().getBoolean(Gamerules.DO_NUCLEAR_BOMBS_EXPLODE)) {
                    if (!player.isCreative()) {
                        player.getItemInHand(handIn).hurtAndBreak(1, player, (p_220287_1_) -> {
                            p_220287_1_.broadcastBreakEvent(handIn);
                        });
                    }
                    new NuclearExplosion(worldIn, pos.getX(), pos.getY(), pos.getZ(), ((TileNuclearBomb) tile).getNuclearCharge());
                    return InteractionResult.SUCCESS;
                }
            }
        } else {
            if (tile instanceof TileNuclearBomb) {
                NetworkHooks.openGui((ServerPlayer) player, (MenuProvider) tile, pos);
                return InteractionResult.SUCCESS;
            }
        }
    }
    return InteractionResult.SUCCESS;
}
Also used : TileNuclearBomb(net.reikeb.electrona.tileentities.TileNuclearBomb) FallingBlockEntity(net.minecraft.world.entity.item.FallingBlockEntity) CollisionContext(net.minecraft.world.phys.shapes.CollisionContext) Items(net.minecraft.world.item.Items) Direction(net.minecraft.core.Direction) BlockState(net.minecraft.world.level.block.state.BlockState) Random(java.util.Random) ServerLevel(net.minecraft.server.level.ServerLevel) ElectronaUtils(net.reikeb.electrona.utils.ElectronaUtils) NuclearExplosion(net.reikeb.electrona.world.NuclearExplosion) ServerPlayer(net.minecraft.server.level.ServerPlayer) BlockGetter(net.minecraft.world.level.BlockGetter) LootContext(net.minecraft.world.level.storage.loot.LootContext) Gamerules(net.reikeb.electrona.world.Gamerules) net.minecraft.world.level.block(net.minecraft.world.level.block) BombFallingEntity(net.reikeb.electrona.entity.BombFallingEntity) StateDefinition(net.minecraft.world.level.block.state.StateDefinition) BlockHitResult(net.minecraft.world.phys.BlockHitResult) InteractionResult(net.minecraft.world.InteractionResult) TileNuclearBomb(net.reikeb.electrona.tileentities.TileNuclearBomb) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) Explosion(net.minecraft.world.level.Explosion) Material(net.minecraft.world.level.material.Material) Player(net.minecraft.world.entity.player.Player) CustomShapes(net.reikeb.electrona.misc.vm.CustomShapes) List(java.util.List) BlockPos(net.minecraft.core.BlockPos) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) DirectionProperty(net.minecraft.world.level.block.state.properties.DirectionProperty) MenuProvider(net.minecraft.world.MenuProvider) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) VoxelShape(net.minecraft.world.phys.shapes.VoxelShape) Collections(java.util.Collections) NetworkHooks(net.minecraftforge.network.NetworkHooks) NuclearExplosion(net.reikeb.electrona.world.NuclearExplosion) ServerPlayer(net.minecraft.server.level.ServerPlayer) ItemStack(net.minecraft.world.item.ItemStack) MenuProvider(net.minecraft.world.MenuProvider) FallingBlockEntity(net.minecraft.world.entity.item.FallingBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Aggregations

BlockHitResult (net.minecraft.world.phys.BlockHitResult)181 BlockPos (net.minecraft.core.BlockPos)117 Vec3 (net.minecraft.world.phys.Vec3)79 BlockState (net.minecraft.world.level.block.state.BlockState)66 ItemStack (net.minecraft.world.item.ItemStack)63 HitResult (net.minecraft.world.phys.HitResult)51 Direction (net.minecraft.core.Direction)46 Level (net.minecraft.world.level.Level)41 ClipContext (net.minecraft.world.level.ClipContext)33 Player (net.minecraft.world.entity.player.Player)32 ServerLevel (net.minecraft.server.level.ServerLevel)23 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)20 Entity (net.minecraft.world.entity.Entity)19 InteractionHand (net.minecraft.world.InteractionHand)18 BlockPlaceContext (net.minecraft.world.item.context.BlockPlaceContext)18 ServerPlayer (net.minecraft.server.level.ServerPlayer)17 LivingEntity (net.minecraft.world.entity.LivingEntity)17 AABB (net.minecraft.world.phys.AABB)17 EntityHitResult (net.minecraft.world.phys.EntityHitResult)17 Minecraft (net.minecraft.client.Minecraft)16