Search in sources :

Example 1 with ModuleTeleportationUnit

use of mekanism.common.content.gear.mekatool.ModuleTeleportationUnit in project Mekanism by mekanism.

the class ItemMekaTool method use.

@Nonnull
@Override
public ActionResult<ItemStack> use(World world, PlayerEntity player, @Nonnull Hand hand) {
    ItemStack stack = player.getItemInHand(hand);
    if (!world.isClientSide()) {
        IModule<ModuleTeleportationUnit> module = getModule(stack, MekanismModules.TELEPORTATION_UNIT);
        if (module != null && module.isEnabled()) {
            BlockRayTraceResult result = MekanismUtils.rayTrace(player, MekanismConfig.gear.mekaToolMaxTeleportReach.get());
            // If we don't require a block target or are not a miss, allow teleporting
            if (!module.getCustomInstance().requiresBlockTarget() || result.getType() != RayTraceResult.Type.MISS) {
                BlockPos pos = result.getBlockPos();
                // make sure we fit
                if (isValidDestinationBlock(world, pos.above()) && isValidDestinationBlock(world, pos.above(2))) {
                    double distance = player.distanceToSqr(pos.getX(), pos.getY(), pos.getZ());
                    if (distance < 5) {
                        return new ActionResult<>(ActionResultType.PASS, stack);
                    }
                    IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
                    FloatingLong energyNeeded = MekanismConfig.gear.mekaToolEnergyUsageTeleport.get().multiply(distance / 10D);
                    if (energyContainer == null || energyContainer.getEnergy().smallerThan(energyNeeded)) {
                        return new ActionResult<>(ActionResultType.FAIL, stack);
                    }
                    energyContainer.extract(energyNeeded, Action.EXECUTE, AutomationType.MANUAL);
                    if (player.isPassenger()) {
                        player.stopRiding();
                    }
                    player.teleportTo(pos.getX() + 0.5, pos.getY() + 1.5, pos.getZ() + 0.5);
                    player.fallDistance = 0.0F;
                    Mekanism.packetHandler.sendToAllTracking(new PacketPortalFX(pos.above()), world, pos);
                    world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ENDERMAN_TELEPORT, SoundCategory.PLAYERS, 1.0F, 1.0F);
                    return new ActionResult<>(ActionResultType.SUCCESS, stack);
                }
            }
        }
    }
    return new ActionResult<>(ActionResultType.PASS, stack);
}
Also used : FloatingLong(mekanism.api.math.FloatingLong) PacketPortalFX(mekanism.common.network.to_client.PacketPortalFX) IEnergyContainer(mekanism.api.energy.IEnergyContainer) ActionResult(net.minecraft.util.ActionResult) ModuleTeleportationUnit(mekanism.common.content.gear.mekatool.ModuleTeleportationUnit) BlockPos(net.minecraft.util.math.BlockPos) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Aggregations

Nonnull (javax.annotation.Nonnull)1 IEnergyContainer (mekanism.api.energy.IEnergyContainer)1 FloatingLong (mekanism.api.math.FloatingLong)1 ModuleTeleportationUnit (mekanism.common.content.gear.mekatool.ModuleTeleportationUnit)1 PacketPortalFX (mekanism.common.network.to_client.PacketPortalFX)1 ItemStack (net.minecraft.item.ItemStack)1 ActionResult (net.minecraft.util.ActionResult)1 BlockPos (net.minecraft.util.math.BlockPos)1 BlockRayTraceResult (net.minecraft.util.math.BlockRayTraceResult)1