Java Code Examples for net.minecraft.entity.player.EntityPlayer.setPosition()

The following are Jave code examples for showing how to use setPosition() of the net.minecraft.entity.player.EntityPlayer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: pnc-repressurized   File: DroneAIBlockInteract.java   View Source Code Vote up 4 votes
private boolean rightClick(BlockPos pos) {
    EnumFacing faceDir = ProgWidgetPlace.getDirForSides(((ISidedWidget) widget).getSides());
    EntityPlayer player = drone.getFakePlayer();
    World world = drone.world();
    ItemStack stack = player.getHeldItemMainhand();

    player.setPosition(pos.getX() + 0.5, pos.getY() + 0.5 - player.eyeHeight, pos.getZ() + 0.5);
    player.rotationPitch = faceDir.getFrontOffsetY() * -90;
    player.rotationYaw = PneumaticCraftUtils.getYawFromFacing(faceDir);

    float hitX = (float)(player.posX - pos.getX());
    float hitY = (float)(player.posY - pos.getY());
    float hitZ = (float)(player.posZ - pos.getZ());

    // this is adapted from PlayerInteractionManager#processRightClickBlock()
    try {
        PlayerInteractEvent.RightClickBlock event = ForgeHooks.onRightClickBlock(player, EnumHand.MAIN_HAND, pos, faceDir.getOpposite(),  ForgeHooks.rayTraceEyeHitVec(player, 2.0D));
        if (event.isCanceled() || event.getUseItem() == Event.Result.DENY) {
            return false;
        }

        EnumActionResult ret = stack.onItemUseFirst(player, world, pos, EnumHand.MAIN_HAND, faceDir, hitX, hitY, hitZ);
        if (ret != EnumActionResult.PASS) return false;

        boolean bypass = player.getHeldItemMainhand().doesSneakBypassUse(world, pos, player);
        EnumActionResult result = EnumActionResult.PASS;

        if (!player.isSneaking() || bypass || event.getUseBlock() == net.minecraftforge.fml.common.eventhandler.Event.Result.ALLOW) {
            IBlockState iblockstate = world.getBlockState(pos);
            if(event.getUseBlock() != net.minecraftforge.fml.common.eventhandler.Event.Result.DENY)
                if (iblockstate.getBlock().onBlockActivated(world, pos, iblockstate, player, EnumHand.MAIN_HAND, faceDir, hitX, hitY, hitZ)) {
                    result = EnumActionResult.SUCCESS;
                }
        }

        if (stack.isEmpty() || player.getCooldownTracker().hasCooldown(stack.getItem())) {
            return false;
        }

        if (stack.getItem() instanceof ItemBlock && !player.canUseCommandBlock()) {
            Block block = ((ItemBlock)stack.getItem()).getBlock();
            if (block instanceof BlockCommandBlock || block instanceof BlockStructure) {
                return false;
            }
        }

        if (result != EnumActionResult.SUCCESS && event.getUseItem() != net.minecraftforge.fml.common.eventhandler.Event.Result.DENY
                || result == EnumActionResult.SUCCESS && event.getUseItem() == net.minecraftforge.fml.common.eventhandler.Event.Result.ALLOW) {
            ItemStack copyBeforeUse = stack.copy();
            result = stack.onItemUse(player, world, pos, EnumHand.MAIN_HAND, faceDir, hitX, hitY, hitZ);
            if (result == EnumActionResult.PASS) {
                ActionResult<ItemStack> rightClickResult = stack.getItem().onItemRightClick(world, player, EnumHand.MAIN_HAND);
                player.setHeldItem(EnumHand.MAIN_HAND, rightClickResult.getResult());
            }
            if (player.getHeldItem(EnumHand.MAIN_HAND).isEmpty()) {
                net.minecraftforge.event.ForgeEventFactory.onPlayerDestroyItem(player, copyBeforeUse, EnumHand.MAIN_HAND);
            }
        }

        return false;
    } catch (Throwable e) {
        Log.error("DroneAIBlockInteract crashed! Stacktrace: ");
        e.printStackTrace();
        return false;
    }
}
 
Example 2
Project: SerenityCE   File: PredictionEngine.java   View Source Code Vote up 4 votes
public Vec3 predictPlayerLocation(EntityPlayer player, int ticksIntoFuture) {
    if (playerPositions.containsKey(player)) {
        List<Vec3> pastPositions = playerPositions.get(player);
        if (pastPositions.size() > 1) {
            Vec3 first = pastPositions.get(0);

            List<Vec3> deltas = new LinkedList<>();
            Vec3 previous = first;
            for (Vec3 position : pastPositions) {
                deltas.add(position.subtract(previous));
                previous = position;
            }

            Vec3 mean = Vec3.ORIGIN;
            for (Vec3 delta : deltas)
                mean = mean.add(delta);

            mean = mean.divideElements(deltas.size());

            EntityPlayer simulated = new EntityOtherPlayerMP(mc.theWorld, player.getGameProfile());
            simulated.noClip = false;
            simulated.setPosition(player.posX, player.posY, player.posZ);
            for (int i = 0; i < ticksIntoFuture; i++) {
                double x = mean.x, y = mean.y, z = mean.z;
                simulated.moveEntity(x, y, z);

                if (!player.capabilities.isFlying) {
                    y = y * 0.8 - 0.02;
                }
                if (simulated.onGround && y < 0) {
                    y = 0;
                }

                x *= 0.99;
                z *= 0.99;

                mean = new Vec3(x, y, z);
            }

            return new Vec3(simulated.posX, simulated.posY, simulated.posZ);
        }
    }
    return null;
}